00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TGo4CommandInvoker.h"
00015
00016 #include "Riostream.h"
00017
00018 #include "TMutex.h"
00019 #include "TString.h"
00020 #include "TObjArray.h"
00021
00022 #include "TGo4Log.h"
00023 #include "TGo4LockGuard.h"
00024 #include "TGo4Command.h"
00025 #include "TGo4CommandProtoList.h"
00026 #include "TGo4RemoteCommand.h"
00027
00028 class TGo4Pair : public TObject {
00029 public:
00030 TGo4Pair(const char* name, TGo4CommandReceiver* p) :
00031 TObject(),
00032 fxName(name),
00033 fxReceiver(p)
00034 {
00035 }
00036
00037 virtual ~TGo4Pair() {}
00038
00039 virtual const char* GetName() const { return fxName.Data(); }
00040
00041 TGo4CommandReceiver* GetReceiver() const { return fxReceiver; }
00042
00043 private:
00044 TString fxName;
00045 TGo4CommandReceiver* fxReceiver;
00046 };
00047
00048 TMutex * TGo4CommandInvoker::fxMutex = 0;
00049 TGo4CommandInvoker * TGo4CommandInvoker::fxInstance = 0;
00050 TGo4CommandProtoList * TGo4CommandInvoker::fxCommandList = 0;
00051 TObjArray * TGo4CommandInvoker::fxArray = 0;
00052
00053 TGo4CommandInvoker::TGo4CommandInvoker() :
00054 TObject(),
00055 TGo4CommandReceiver()
00056 {
00057 TRACE((12,"TGo4CommandInvoker::TGo4CommandInvoker()", __LINE__, __FILE__));
00058 fxCommand = 0;
00059 fxArray = new TObjArray(10);
00060 fxMutex = new TMutex(kTRUE);
00061 fxCommandList=new TGo4CommandProtoList("Go4 base commandlist");
00062 Register("CommandInvoker",this);
00063 }
00064
00065 TGo4CommandInvoker::~TGo4CommandInvoker()
00066 {
00067 TRACE((12,"TGo4CommandInvoker::~TGo4CommandInvoker()", __LINE__, __FILE__));
00068 delete fxCommand;
00069 delete fxCommandList;
00070 delete fxMutex;
00071 fxArray->Delete();
00072 delete fxArray;
00073 }
00074
00075
00076 TGo4CommandInvoker * TGo4CommandInvoker::Instance()
00077 {
00078 TRACE((10,"TGo4CommandInvoker * TGo4CommandInvoker::Instance()", __LINE__, __FILE__));
00079 if (fxInstance == 0)
00080 fxInstance = new TGo4CommandInvoker();
00081 return fxInstance;
00082 }
00083
00084 void TGo4CommandInvoker::Register(const char* name, TGo4CommandReceiver *p)
00085 {
00086 TRACE((12,"static void TGo4CommandInvoker::Register(const char* name, TGo4CommandReceiver *p)", __LINE__, __FILE__));
00087 TGo4LockGuard lockguard(fxMutex);
00088 fxArray->Add(new TGo4Pair(name, p));
00089 }
00090
00091 void TGo4CommandInvoker::UnRegister(TGo4CommandReceiver *p)
00092 {
00093 TRACE((12,"static void TGo4CommandInvoker::UnRegister(TGo4CommandReceiver *p)", __LINE__, __FILE__));
00094 if (fxArray==0) return;
00095 TGo4LockGuard lockguard(fxMutex);
00096 TIter riter(fxArray);
00097 TObject* ob=0;
00098 while((ob=riter())!=0) {
00099 TGo4Pair* pair = dynamic_cast<TGo4Pair*>(ob);
00100 if(pair==0) {
00101 cerr<<"NEVER COME HERE: TGo4CommandInvoker::UnRegister - receiver list with no receiver"<<endl;
00102 break;
00103 }
00104 if(pair->GetReceiver()==p) {
00105
00106 fxArray->Remove(pair);
00107 delete pair;
00108 break;
00109 }
00110 }
00111 fxArray->Compress();
00112 }
00113
00114
00115 TGo4CommandReceiver* TGo4CommandInvoker::Lookup(const char* name)
00116 {
00117 TRACE((10,"static TGo4CommandReceiver * TGo4CommandInvoker::Lookup(const char* name)", __LINE__, __FILE__));
00118 TGo4Pair* pair = (TGo4Pair*) fxArray->FindObject(name);
00119
00120 return pair!=0 ? pair->GetReceiver() : 0;
00121 }
00122
00123 void TGo4CommandInvoker::Invoke(TGo4Command * com)
00124 {
00125 TRACE((12,"void TGo4CommandInvoker::Invoke(TGo4Command * com)", __LINE__, __FILE__));
00126 if(com==0) return;
00127 TGo4LockGuard lockguard(fxMutex);
00128
00129 TGo4CommandReceiver *rec = Lookup(com->GetReceiverName());
00130 if(rec!=0) {
00131 com->SetReceiver(rec);
00132 if(com->GetMode()>=com->GetProtection())
00133 com->ExeCom();
00134 else
00135 com->RefuseCom();
00136 } else
00137 TGo4Log::Debug(" CommandInvoker: UNKNOWN receiver");
00138 }
00139
00140 void TGo4CommandInvoker::Invoke()
00141 {
00142 TRACE((12,"void TGo4CommandInvoker::Invoke()", __LINE__, __FILE__));
00143 if(fxCommand==0) return;
00144
00145 TGo4LockGuard lockguard(fxMutex);
00146
00147 TGo4CommandReceiver* rcv = Lookup(fxCommand->GetReceiverName());
00148
00149 if(rcv!=0) {
00150 fxCommand->SetReceiver(rcv);
00151 fxCommand->ExeCom();
00152 }
00153 delete fxCommand;
00154 fxCommand = 0;
00155 }
00156
00157 void TGo4CommandInvoker::SetCommandList(TGo4CommandProtoList* list)
00158 {
00159 delete fxCommandList;
00160 fxCommandList=list;
00161 }
00162
00163 Int_t TGo4CommandInvoker::ExecuteFromRemote(TGo4RemoteCommand* remcom)
00164 {
00165 if(fxCommandList==0) return -1;
00166 TGo4Command* realcommand=fxCommandList->MakeCommand(remcom);
00167 realcommand->SetTaskName(remcom->GetTaskName());
00168 realcommand->SetMode(remcom->GetMode());
00169 Invoke(realcommand);
00170 delete realcommand;
00171 return 0;
00172 }