Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

TGo4CommandInvoker.cxx

Go to the documentation of this file.
00001 //-------------------------------------------------------------
00002 //        Go4 Release Package v3.04-01 (build 30401)
00003 //                      28-November-2008
00004 //---------------------------------------------------------------
00005 //   The GSI Online Offline Object Oriented (Go4) Project
00006 //   Experiment Data Processing at EE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 #include "TGo4CommandInvoker.h"
00017 
00018 #include "Riostream.h"
00019 
00020 #include "TMutex.h"
00021 #include "TString.h"
00022 #include "TObjArray.h"
00023 
00024 #include "TGo4Log.h"
00025 #include "TGo4LockGuard.h"
00026 #include "TGo4Command.h"
00027 #include "TGo4CommandProtoList.h"
00028 #include "TGo4RemoteCommand.h"
00029 
00030 class TGo4Pair : public TObject {
00031   public:
00032      TGo4Pair(const char* name, TGo4CommandReceiver* p) :
00033         TObject(),
00034         fxName(name),
00035         fxReceiver(p)
00036      {
00037      }
00038 
00039      virtual ~TGo4Pair() {}
00040 
00041      virtual const char* GetName() const { return fxName.Data(); }
00042 
00043      TGo4CommandReceiver* GetReceiver() const { return fxReceiver; }
00044 
00045   private:
00046      TString fxName;         
00047      TGo4CommandReceiver* fxReceiver; 
00048 };
00049 
00050 TMutex * TGo4CommandInvoker::fxMutex = 0;
00051 TGo4CommandInvoker * TGo4CommandInvoker::fxInstance = 0;
00052 TGo4CommandProtoList * TGo4CommandInvoker::fxCommandList = 0;
00053 TObjArray * TGo4CommandInvoker::fxArray = 0;
00054 
00055 TGo4CommandInvoker::TGo4CommandInvoker() :
00056    TObject(),
00057    TGo4CommandReceiver()
00058 {
00059    TRACE((12,"TGo4CommandInvoker::TGo4CommandInvoker()", __LINE__, __FILE__));
00060    fxCommand = 0;
00061    fxArray = new TObjArray(10);
00062    fxMutex = new TMutex(kTRUE);
00063    fxCommandList=new TGo4CommandProtoList("Go4 base commandlist");
00064    Register("CommandInvoker",this); // we are ourselves receiver for remote commands
00065 }
00066 
00067 TGo4CommandInvoker::~TGo4CommandInvoker()
00068 {
00069    TRACE((12,"TGo4CommandInvoker::~TGo4CommandInvoker()", __LINE__, __FILE__));
00070    delete fxCommand;
00071    delete fxCommandList;
00072    delete fxMutex;
00073    fxArray->Delete();
00074    delete fxArray;
00075 }
00076 
00077 
00078 TGo4CommandInvoker * TGo4CommandInvoker::Instance()
00079 {
00080     TRACE((10,"TGo4CommandInvoker * TGo4CommandInvoker::Instance()", __LINE__, __FILE__));
00081     if (fxInstance == 0)
00082       fxInstance = new TGo4CommandInvoker();
00083     return fxInstance;
00084 }
00085 
00086 void TGo4CommandInvoker::Register(const char* name, TGo4CommandReceiver *p)
00087 {
00088     TRACE((12,"static void TGo4CommandInvoker::Register(Text_t * name, TGo4CommandReceiver *p)", __LINE__, __FILE__));
00089     TGo4LockGuard lockguard(fxMutex);
00090     fxArray->Add(new TGo4Pair(name, p));
00091 }
00092 
00093 void TGo4CommandInvoker::UnRegister(TGo4CommandReceiver *p)
00094 {
00095     TRACE((12,"static void TGo4CommandInvoker::UnRegister(Text_t * name, TGo4CommandReceiver *p)", __LINE__, __FILE__));
00096     if (fxArray==0) return;
00097     TGo4LockGuard lockguard(fxMutex);
00098     TIter riter(fxArray);
00099     TObject* ob=0;
00100     while((ob=riter())!=0) {
00101        TGo4Pair* pair = dynamic_cast<TGo4Pair*>(ob);
00102        if(pair==0) {
00103           cerr<<"NEVER COME HERE: TGo4CommandInvoker::UnRegister - receiver list with no receiver"<<endl;
00104           break;
00105        }
00106        if(pair->GetReceiver()==p) {
00107           //cout <<"TGo4CommandInvoker::UnRegister removed receiver "<<pair->ReceiverName()<< endl;
00108           fxArray->Remove(pair);
00109           delete pair;
00110           break;
00111        }
00112    }
00113    fxArray->Compress();
00114 }
00115 
00116 
00117 TGo4CommandReceiver* TGo4CommandInvoker::Lookup(const char* name)
00118 {
00119   TRACE((10,"static TGo4CommandReceiver * TGo4CommandInvoker::Lookup(Text_t * name)", __LINE__, __FILE__));
00120   TGo4Pair* pair = (TGo4Pair*) fxArray->FindObject(name);
00121 
00122   return pair!=0 ? pair->GetReceiver() : 0;
00123 }
00124 
00125 void TGo4CommandInvoker::Invoke(TGo4Command * com)
00126 {
00127     TRACE((12,"void TGo4CommandInvoker::Invoke(TGo4Command * com)", __LINE__, __FILE__));
00128     if(com==0) return;
00129     TGo4LockGuard lockguard(fxMutex);
00130      //cout <<"Invoker has command "<<com->GetName() << endl;
00131     TGo4CommandReceiver *rec = Lookup(com->GetReceiverName());
00132     if(rec!=0) {
00133        com->SetReceiver(rec);
00134        if(com->GetMode()>=com->GetProtection())
00135            com->ExeCom();
00136        else
00137            com->RefuseCom();
00138     } else
00139        TGo4Log::Debug(" CommandInvoker: UNKNOWN receiver");
00140 }
00141 
00142 void TGo4CommandInvoker::Invoke()
00143 {
00144    TRACE((12,"void TGo4CommandInvoker::Invoke()", __LINE__, __FILE__));
00145    if(fxCommand==0) return;
00146    //cout <<"TGo4CommandInvoker::Invoke() PLAIN NEVER COME HERE" << endl;
00147    TGo4LockGuard lockguard(fxMutex);
00148 
00149    TGo4CommandReceiver* rcv = Lookup(fxCommand->GetReceiverName());
00150 
00151    if(rcv!=0) {
00152       fxCommand->SetReceiver(rcv);
00153       fxCommand->ExeCom();
00154    }
00155    delete fxCommand;
00156    fxCommand = 0;
00157 }
00158 
00159 void TGo4CommandInvoker::SetCommandList(TGo4CommandProtoList* list)
00160 {
00161    delete fxCommandList;
00162    fxCommandList=list;
00163 }
00164 
00165 Int_t TGo4CommandInvoker::ExecuteFromRemote(TGo4RemoteCommand* remcom)
00166 {
00167    if(fxCommandList==0) return -1;
00168    TGo4Command* realcommand=fxCommandList->MakeCommand(remcom);
00169    realcommand->SetTaskName(remcom->GetTaskName());
00170    realcommand->SetMode(remcom->GetMode());
00171    Invoke(realcommand);
00172    delete realcommand;
00173    return 0;
00174 }
00175 
00176 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Fri Nov 28 12:59:05 2008 for Go4-v3.04-1 by  doxygen 1.4.2