GSI Object Oriented Online Offline (Go4)  GO4-6.1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4CommandInvoker.cxx
Go to the documentation of this file.
1 // $Id: TGo4CommandInvoker.cxx 2745 2020-04-16 08:55:15Z 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 fuer 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 "TObjArray.h"
18 
19 #include "TGo4Log.h"
20 #include "TGo4LockGuard.h"
21 #include "TGo4CommandProtoList.h"
22 #include "TGo4RemoteCommand.h"
23 
24 class TGo4Pair : public TObject {
25  public:
26  TGo4Pair(const char* name, TGo4CommandReceiver* p) :
27  TObject(),
28  fxName(name),
29  fxReceiver(p)
30  {
31  }
32 
33  virtual ~TGo4Pair() {}
34 
35  virtual const char* GetName() const { return fxName.Data(); }
36 
38 
39  private:
40  TString fxName;
42 };
43 
44 TMutex * TGo4CommandInvoker::fxMutex = 0;
47 TObjArray * TGo4CommandInvoker::fxArray = 0;
48 
50  TObject(),
52 {
53  GO4TRACE((12,"TGo4CommandInvoker::TGo4CommandInvoker()", __LINE__, __FILE__));
54  fxCommand = 0;
55  fxArray = new TObjArray(10);
56  fxMutex = new TMutex(kTRUE);
57  fxCommandList = new TGo4CommandProtoList("Go4 base commandlist");
58  Register("CommandInvoker",this); // we are ourselves receiver for remote commands
59 }
60 
62 {
63  GO4TRACE((12,"TGo4CommandInvoker::~TGo4CommandInvoker()", __LINE__, __FILE__));
64  delete fxCommand;
65  delete fxCommandList;
66  delete fxMutex;
67  fxArray->Delete();
68  delete fxArray;
69 }
70 
71 
73 {
74  GO4TRACE((10,"TGo4CommandInvoker * TGo4CommandInvoker::Instance()", __LINE__, __FILE__));
75  if (fxInstance == 0)
77  return fxInstance;
78 }
79 
81 {
82  GO4TRACE((12,"static void TGo4CommandInvoker::Register(const char* name, TGo4CommandReceiver *p)", __LINE__, __FILE__));
83  TGo4LockGuard lockguard(fxMutex);
84  fxArray->Add(new TGo4Pair(name, p));
85 }
86 
88 {
89  GO4TRACE((12,"static void TGo4CommandInvoker::UnRegister(TGo4CommandReceiver *p)", __LINE__, __FILE__));
90  if (fxArray==0) return;
91  TGo4LockGuard lockguard(fxMutex);
92  TIter riter(fxArray);
93  TObject* ob=0;
94  while((ob=riter())!=0) {
95  TGo4Pair* pair = dynamic_cast<TGo4Pair*>(ob);
96  if(pair==0) {
97  TGo4Log::Error("NEVER COME HERE: TGo4CommandInvoker::UnRegister - receiver list with no receiver");
98  break;
99  }
100  if(pair->GetReceiver()==p) {
101  //std::cout <<"TGo4CommandInvoker::UnRegister removed receiver "<<pair->ReceiverName()<< std::endl;
102  fxArray->Remove(pair);
103  delete pair;
104  break;
105  }
106  }
107  fxArray->Compress();
108 }
109 
110 
112 {
113  GO4TRACE((10,"static TGo4CommandReceiver * TGo4CommandInvoker::Lookup(const char* name)", __LINE__, __FILE__));
114  TGo4Pair* pair = (TGo4Pair*) fxArray->FindObject(name);
115 
116  return pair!=0 ? pair->GetReceiver() : 0;
117 }
118 
120 {
121  GO4TRACE((12,"void TGo4CommandInvoker::Invoke(TGo4Command * com)", __LINE__, __FILE__));
122  if(com==0) return;
123  TGo4LockGuard lockguard(fxMutex);
124  //std::cout <<"Invoker has command "<<com->GetName() << std::endl;
126  if(rec!=0) {
127  com->SetReceiver(rec);
128  if(com->GetMode()>=com->GetProtection())
129  com->ExeCom();
130  else
131  com->RefuseCom();
132  } else
133  TGo4Log::Debug(" CommandInvoker: UNKNOWN receiver");
134 }
135 
137 {
138  GO4TRACE((12,"void TGo4CommandInvoker::Invoke()", __LINE__, __FILE__));
139  if(fxCommand==0) return;
140  //std::cout <<"TGo4CommandInvoker::Invoke() PLAIN NEVER COME HERE" << std::endl;
141  TGo4LockGuard lockguard(fxMutex);
142 
144 
145  if(rcv!=0) {
146  fxCommand->SetReceiver(rcv);
147  fxCommand->ExeCom();
148  }
149  delete fxCommand;
150  fxCommand = 0;
151 }
152 
154 {
155  delete fxCommandList;
156  fxCommandList=list;
157 }
158 
160 {
161  if(fxCommandList==0) return -1;
162  TGo4Command* realcommand=fxCommandList->MakeCommand(remcom);
163  realcommand->SetTaskName(remcom->GetTaskName());
164  realcommand->SetMode(remcom->GetMode());
165  Invoke(realcommand);
166  delete realcommand;
167  return 0;
168 }
TGo4Pair(const char *name, TGo4CommandReceiver *p)
Go4CommandMode_t GetProtection()
Definition: TGo4Command.h:75
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:99
static TGo4CommandProtoList * fxCommandList
const char * GetReceiverName() const
Definition: TGo4Command.cxx:87
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:76
void SetMode(Go4CommandMode_t m)
Definition: TGo4Command.h:83
virtual Int_t RefuseCom()
static void Error(const char *text,...)
Definition: TGo4Log.cxx:313
static void Debug(const char *text,...)
Definition: TGo4Log.cxx:274