GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4CommandInvoker.cxx
Go to the documentation of this file.
1 // $Id: TGo4CommandInvoker.cxx 999 2013-07-25 11:58:59Z linev $
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum für Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4CommandInvoker.h"
15 
16 #include "TMutex.h"
17 #include "TString.h"
18 #include "TObjArray.h"
19 
20 #include "TGo4Log.h"
21 #include "TGo4LockGuard.h"
22 #include "TGo4Command.h"
23 #include "TGo4CommandProtoList.h"
24 #include "TGo4RemoteCommand.h"
25 
26 class TGo4Pair : public TObject {
27  public:
28  TGo4Pair(const char* name, TGo4CommandReceiver* p) :
29  TObject(),
30  fxName(name),
31  fxReceiver(p)
32  {
33  }
34 
35  virtual ~TGo4Pair() {}
36 
37  virtual const char* GetName() const { return fxName.Data(); }
38 
40 
41  private:
42  TString fxName;
44 };
45 
46 TMutex * TGo4CommandInvoker::fxMutex = 0;
49 TObjArray * TGo4CommandInvoker::fxArray = 0;
50 
52  TObject(),
54 {
55  GO4TRACE((12,"TGo4CommandInvoker::TGo4CommandInvoker()", __LINE__, __FILE__));
56  fxCommand = 0;
57  fxArray = new TObjArray(10);
58  fxMutex = new TMutex(kTRUE);
59  fxCommandList=new TGo4CommandProtoList("Go4 base commandlist");
60  Register("CommandInvoker",this); // we are ourselves receiver for remote commands
61 }
62 
64 {
65  GO4TRACE((12,"TGo4CommandInvoker::~TGo4CommandInvoker()", __LINE__, __FILE__));
66  delete fxCommand;
67  delete fxCommandList;
68  delete fxMutex;
69  fxArray->Delete();
70  delete fxArray;
71 }
72 
73 
75 {
76  GO4TRACE((10,"TGo4CommandInvoker * TGo4CommandInvoker::Instance()", __LINE__, __FILE__));
77  if (fxInstance == 0)
79  return fxInstance;
80 }
81 
83 {
84  GO4TRACE((12,"static void TGo4CommandInvoker::Register(const char* name, TGo4CommandReceiver *p)", __LINE__, __FILE__));
85  TGo4LockGuard lockguard(fxMutex);
86  fxArray->Add(new TGo4Pair(name, p));
87 }
88 
90 {
91  GO4TRACE((12,"static void TGo4CommandInvoker::UnRegister(TGo4CommandReceiver *p)", __LINE__, __FILE__));
92  if (fxArray==0) return;
93  TGo4LockGuard lockguard(fxMutex);
94  TIter riter(fxArray);
95  TObject* ob=0;
96  while((ob=riter())!=0) {
97  TGo4Pair* pair = dynamic_cast<TGo4Pair*>(ob);
98  if(pair==0) {
99  TGo4Log::Error("NEVER COME HERE: TGo4CommandInvoker::UnRegister - receiver list with no receiver");
100  break;
101  }
102  if(pair->GetReceiver()==p) {
103  //std::cout <<"TGo4CommandInvoker::UnRegister removed receiver "<<pair->ReceiverName()<< std::endl;
104  fxArray->Remove(pair);
105  delete pair;
106  break;
107  }
108  }
109  fxArray->Compress();
110 }
111 
112 
114 {
115  GO4TRACE((10,"static TGo4CommandReceiver * TGo4CommandInvoker::Lookup(const char* name)", __LINE__, __FILE__));
116  TGo4Pair* pair = (TGo4Pair*) fxArray->FindObject(name);
117 
118  return pair!=0 ? pair->GetReceiver() : 0;
119 }
120 
122 {
123  GO4TRACE((12,"void TGo4CommandInvoker::Invoke(TGo4Command * com)", __LINE__, __FILE__));
124  if(com==0) return;
125  TGo4LockGuard lockguard(fxMutex);
126  //std::cout <<"Invoker has command "<<com->GetName() << std::endl;
128  if(rec!=0) {
129  com->SetReceiver(rec);
130  if(com->GetMode()>=com->GetProtection())
131  com->ExeCom();
132  else
133  com->RefuseCom();
134  } else
135  TGo4Log::Debug(" CommandInvoker: UNKNOWN receiver");
136 }
137 
139 {
140  GO4TRACE((12,"void TGo4CommandInvoker::Invoke()", __LINE__, __FILE__));
141  if(fxCommand==0) return;
142  //std::cout <<"TGo4CommandInvoker::Invoke() PLAIN NEVER COME HERE" << std::endl;
143  TGo4LockGuard lockguard(fxMutex);
144 
146 
147  if(rcv!=0) {
148  fxCommand->SetReceiver(rcv);
149  fxCommand->ExeCom();
150  }
151  delete fxCommand;
152  fxCommand = 0;
153 }
154 
156 {
157  delete fxCommandList;
158  fxCommandList=list;
159 }
160 
162 {
163  if(fxCommandList==0) return -1;
164  TGo4Command* realcommand=fxCommandList->MakeCommand(remcom);
165  realcommand->SetTaskName(remcom->GetTaskName());
166  realcommand->SetMode(remcom->GetMode());
167  Invoke(realcommand);
168  delete realcommand;
169  return 0;
170 }
TGo4Pair(const char *name, TGo4CommandReceiver *p)
Go4CommandMode_t GetProtection()
Definition: TGo4Command.h:76
TGo4CommandReceiver * fxReceiver
static TGo4CommandInvoker * fxInstance
static void SetCommandList(TGo4CommandProtoList *list)
static void UnRegister(TGo4CommandReceiver *p)
void SetTaskName(const char *)
const char * GetTaskName() const
Definition: TGo4Command.cxx:98
static TGo4CommandProtoList * fxCommandList
const char * GetReceiverName() const
Definition: TGo4Command.cxx:86
virtual Int_t ExeCom()
virtual const char * GetName() const
static TMutex * fxMutex
static void Register(const char *name, TGo4CommandReceiver *p)
virtual void SetReceiver(TGo4CommandReceiver *rec)
TGo4CommandReceiver * GetReceiver() const
static TGo4CommandReceiver * Lookup(const char *name)
Int_t ExecuteFromRemote(TGo4RemoteCommand *remcom)
static TGo4CommandInvoker * Instance()
virtual ~TGo4Pair()
static TObjArray * fxArray
TGo4Command * fxCommand
#define GO4TRACE(X)
Definition: TGo4Log.h:26
TGo4Command * MakeCommand(const char *name)
Go4CommandMode_t GetMode()
Definition: TGo4Command.h:77
void SetMode(Go4CommandMode_t m)
Definition: TGo4Command.h:84
virtual Int_t RefuseCom()
static void Error(const char *text,...)
Definition: TGo4Log.cxx:309
static void Debug(const char *text,...)
Definition: TGo4Log.cxx:270