GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TGo4CommandInvoker.cxx
Go to the documentation of this file.
1 // $Id$
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  const char *GetName() const override { return fxName.Data(); }
34 
36 
37  private:
38  TString fxName;
40 };
41 
42 TMutex *TGo4CommandInvoker::fxMutex = nullptr;
45 TObjArray *TGo4CommandInvoker::fxArray = nullptr;
46 
48  TObject(),
50 {
51  GO4TRACE((12,"TGo4CommandInvoker::TGo4CommandInvoker()", __LINE__, __FILE__));
52  fxCommand = nullptr;
53  fxArray = new TObjArray(10);
54  fxMutex = new TMutex(kTRUE);
55  fxCommandList = new TGo4CommandProtoList("Go4 base commandlist");
56  Register("CommandInvoker",this); // we are ourselves receiver for remote commands
57 }
58 
60 {
61  GO4TRACE((12,"TGo4CommandInvoker::~TGo4CommandInvoker()", __LINE__, __FILE__));
62  delete fxCommand;
63  delete fxCommandList;
64  delete fxMutex;
65  fxArray->Delete();
66  delete fxArray;
67 }
68 
70 {
71  GO4TRACE((10,"TGo4CommandInvoker *TGo4CommandInvoker::Instance()", __LINE__, __FILE__));
72  if (!fxInstance)
74  return fxInstance;
75 }
76 
78 {
79  GO4TRACE((12,"static void TGo4CommandInvoker::Register(const char *name, TGo4CommandReceiver *p)", __LINE__, __FILE__));
80  TGo4LockGuard lockguard(fxMutex);
81  fxArray->Add(new TGo4Pair(name, p));
82 }
83 
85 {
86  GO4TRACE((12,"static void TGo4CommandInvoker::UnRegister(TGo4CommandReceiver *p)", __LINE__, __FILE__));
87  if (!fxArray) return;
88  TGo4LockGuard lockguard(fxMutex);
89  TIter riter(fxArray);
90  while(auto ob = riter()) {
91  TGo4Pair* pair = dynamic_cast<TGo4Pair*>(ob);
92  if(!pair) {
93  TGo4Log::Error("NEVER COME HERE: TGo4CommandInvoker::UnRegister - receiver list with no receiver");
94  break;
95  }
96  if(pair->GetReceiver() == p) {
97  fxArray->Remove(pair);
98  delete pair;
99  break;
100  }
101  }
102  fxArray->Compress();
103 }
104 
106 {
107  GO4TRACE((10,"static TGo4CommandReceiver *TGo4CommandInvoker::Lookup(const char *name)", __LINE__, __FILE__));
108  TGo4Pair* pair = (TGo4Pair*) fxArray->FindObject(name);
109 
110  return pair ? pair->GetReceiver() : nullptr;
111 }
112 
114 {
115  GO4TRACE((12,"void TGo4CommandInvoker::Invoke(TGo4Command *com)", __LINE__, __FILE__));
116  if(!com) return;
117  TGo4LockGuard lockguard(fxMutex);
119  if(rec) {
120  com->SetReceiver(rec);
121  if(com->GetMode()>=com->GetProtection())
122  com->ExeCom();
123  else
124  com->RefuseCom();
125  } else
126  TGo4Log::Debug(" CommandInvoker: UNKNOWN receiver");
127 }
128 
130 {
131  GO4TRACE((12,"void TGo4CommandInvoker::Invoke()", __LINE__, __FILE__));
132  if(!fxCommand) return;
133  TGo4LockGuard lockguard(fxMutex);
134 
136 
137  if(rcv) {
138  fxCommand->SetReceiver(rcv);
139  fxCommand->ExeCom();
140  }
141  delete fxCommand;
142  fxCommand = nullptr;
143 }
144 
146 {
147  delete fxCommandList;
148  fxCommandList = list;
149 }
150 
152 {
153  if(!fxCommandList) return -1;
154  TGo4Command *realcommand = fxCommandList->MakeCommand(remcom);
155  realcommand->SetTaskName(remcom->GetTaskName());
156  realcommand->SetMode(remcom->GetMode());
157  Invoke(realcommand);
158  delete realcommand;
159  return 0;
160 }
TGo4Pair(const char *name, TGo4CommandReceiver *p)
TGo4CommandReceiver * fxReceiver
static TGo4CommandInvoker * fxInstance
static void SetCommandList(TGo4CommandProtoList *list)
static void UnRegister(TGo4CommandReceiver *p)
void SetTaskName(const char *)
static TGo4CommandProtoList * fxCommandList
Go4CommandMode_t GetProtection() const
Definition: TGo4Command.h:75
virtual Int_t ExeCom()
static TMutex * fxMutex
static void Register(const char *name, TGo4CommandReceiver *p)
virtual void SetReceiver(TGo4CommandReceiver *rec)
Go4CommandMode_t GetMode() const
Definition: TGo4Command.h:76
const char * GetName() const override
static void Debug(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:281
static TGo4CommandReceiver * Lookup(const char *name)
const char * GetTaskName() const
Definition: TGo4Command.cxx:99
Int_t ExecuteFromRemote(TGo4RemoteCommand *remcom)
static TGo4CommandInvoker * Instance()
static TObjArray * fxArray
TGo4CommandReceiver * GetReceiver() const
TGo4Command * fxCommand
static void Error(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:320
const char * GetReceiverName() const
Definition: TGo4Command.cxx:87
#define GO4TRACE(X)
Definition: TGo4Log.h:25
TGo4Command * MakeCommand(const char *name)
void SetMode(Go4CommandMode_t m)
Definition: TGo4Command.h:83
virtual Int_t RefuseCom()