GSI Object Oriented Online Offline (Go4) GO4-6.4.0
Loading...
Searching...
No Matches
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"
22#include "TGo4RemoteCommand.h"
23
24class 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
42TMutex *TGo4CommandInvoker::fxMutex = nullptr;
45TObjArray *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
135 TGo4CommandReceiver *rcv = Lookup(fxCommand->GetReceiverName());
136
137 if(rcv) {
138 fxCommand->SetReceiver(rcv);
139 fxCommand->ExeCom();
140 }
141 delete fxCommand;
142 fxCommand = nullptr;
143}
144
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}
#define TGo4LockGuard
#define GO4TRACE(X)
Definition TGo4Log.h:25
Class that knows Command Object; When Client gets the Command it is passed to the Invoker who sets th...
static TGo4CommandInvoker * Instance()
static void SetCommandList(TGo4CommandProtoList *list)
Application may exchange standard command list by own subclass with specialized commands,...
static TGo4CommandReceiver * Lookup(const char *name)
static TMutex * fxMutex
TGo4Command * fxCommand
aggregation
static void UnRegister(TGo4CommandReceiver *p)
static TGo4CommandInvoker * fxInstance
Int_t ExecuteFromRemote(TGo4RemoteCommand *remcom)
Create real command from remote command and invoke it.
static TObjArray * fxArray
static TGo4CommandProtoList * fxCommandList
For remote commands, invoker will create the requested slave commands from own command list.
static void Register(const char *name, TGo4CommandReceiver *p)
This is the Go4 Abstract Command Class; part of the Base command pattern.
Definition TGo4Command.h:35
virtual Int_t RefuseCom()
Optional action if command is not allowed (warning message e.g.)
Go4CommandMode_t GetProtection() const
Definition TGo4Command.h:75
virtual Int_t ExeCom()
Do the command action.
void SetMode(Go4CommandMode_t m)
Definition TGo4Command.h:83
virtual void SetReceiver(TGo4CommandReceiver *rec)
const char * GetTaskName() const
Go4CommandMode_t GetMode() const
Definition TGo4Command.h:76
void SetTaskName(const char *)
const char * GetReceiverName() const
static void Debug(const char *text,...) GO4_PRINTF_ARGS
User shortcut for message with prio 0.
Definition TGo4Log.cxx:281
static void Error(const char *text,...) GO4_PRINTF_ARGS
User shortcut for message with prio 3.
Definition TGo4Log.cxx:320
const char * GetName() const override
TGo4CommandReceiver * fxReceiver
TGo4Pair(const char *name, TGo4CommandReceiver *p)
TGo4CommandReceiver * GetReceiver() const
This command is a container for commands to be send from master to slave via inter-process transport.