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