00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "TGo4ClientTask.h"
00015
00016 #include "Riostream.h"
00017
00018 #include "TGo4Log.h"
00019 #include "TGo4LockGuard.h"
00020
00021 #include "TGo4BufferQueue.h"
00022 #include "TGo4Command.h"
00023 #include "TGo4ClientStatus.h"
00024 #include "TGo4ThreadHandler.h"
00025 #include "TGo4TaskHandler.h"
00026
00027 TGo4ClientTask::TGo4ClientTask(const char* name,
00028 const char* serverhost,
00029 UInt_t negotiationport,
00030 Bool_t blockingmode,
00031 Bool_t standalone,
00032 Bool_t autostart,
00033 Bool_t autocreate,
00034 Bool_t ismaster,
00035 Bool_t autoconnect) :
00036 TGo4Task(name,blockingmode,autostart,autocreate,ismaster),
00037 fxTaskHandler(0),
00038 fbAutoConnect(autoconnect),
00039 fbServerConnected(kFALSE)
00040 {
00041 fxServerHostname=serverhost;
00042 TString nomen("TaskHandler of "); nomen+=name;
00043 fxTaskHandler=new TGo4TaskHandler(nomen.Data(),this,kTRUE, IsMaster(),negotiationport);
00044 fxCommandQ=dynamic_cast<TGo4BufferQueue*> (GetTaskHandler()->GetCommandQueue());
00045 fxStatusQ=dynamic_cast<TGo4BufferQueue*> (GetTaskHandler()->GetStatusQueue());
00046 fxDataQ=dynamic_cast<TGo4BufferQueue*> (GetTaskHandler()->GetDataQueue());
00047 if(standalone) {
00048 Launch();
00049 } else {
00050
00051 }
00052 }
00053
00054 TGo4ClientTask::~TGo4ClientTask()
00055 {
00056 DisconnectServer();
00057 delete fxTaskHandler;
00058 }
00059
00060 TGo4TaskHandler* TGo4ClientTask::GetTaskHandler()
00061 {
00062 return fxTaskHandler;
00063 }
00064
00065 Int_t TGo4ClientTask::Initialization()
00066 {
00067 Int_t rev=-1;
00068 if(fbInitDone)
00069
00070 {
00071 return 0;
00072 }
00073 else
00074 {
00075 rev=TGo4Task::Initialization();
00076 if(rev==0)
00077 {
00078
00079 if(fbAutoConnect)
00080 {
00081 if(!ConnectServer(GetServerHostName(),0))
00082 Terminate();
00083 }
00084 }
00085 else
00086 {
00087
00088
00089 }
00090 }
00091 return rev;
00092 }
00093
00094 Bool_t TGo4ClientTask::ConnectServer(const char* node, UInt_t negport,
00095 Go4CommandMode_t role,
00096 const char* passwd)
00097 {
00098 if(fbServerConnected)
00099 {
00100 TGo4Log::Warn(" ClientTask::ConnectServer ''%s'': ServerTask already connected",
00101 GetName());
00102 return kTRUE;
00103 }
00104
00105 if(negport)
00106 fxTaskHandler->SetNegotiationPort(negport);
00107 if(IsMaster())
00108 fxTaskHandler->ClearQueues();
00109
00110
00111
00112 if(passwd)
00113 {
00114 fxTaskHandler->SetRole(role);
00115 if(role==kGo4ComModeObserver)
00116 {
00117 TGo4TaskHandler::SetObservAccount(0,passwd);
00118 }
00119 else if (role==kGo4ComModeController)
00120 {
00121 TGo4TaskHandler::SetCtrlAccount(0,passwd);
00122 }
00123 else if (role==kGo4ComModeAdministrator)
00124 {
00125 TGo4TaskHandler::SetAdminAccount(0,passwd);
00126 }
00127
00128 }
00129 if(fxTaskHandler->Connect(node,0))
00130 {
00131
00132
00133 fbServerConnected=kTRUE;
00134 fxWorkHandler->StartAll();
00135 SendStatusMessage(1,kTRUE,"Client %s connected successfully to Server task at node %s",
00136 GetName(),node);
00137 return kTRUE;
00138 }
00139 else
00140 {
00141 TGo4Log::Error(" ClientTask ''%s'': FAILED connection to ServerTask at node %s",
00142 GetName(),node);
00143 return kFALSE;
00144 }
00145
00146
00147 }
00148
00149 Bool_t TGo4ClientTask::DisconnectServer(Bool_t isterminating)
00150 {
00151 Bool_t rev=kTRUE;
00152 if(fbServerConnected)
00153 {
00154 StopWorkThreads();
00155 if(IsMaster())
00156 {
00157
00158 TGo4Queue* cq=GetCommandQueue();
00159 if(cq) cq->Clear();
00160
00161 }
00163 WakeCommandQueue(TGo4Task::Get_fgiTERMID());
00164 SendStopBuffers();
00165 rev=fxTaskHandler->DisConnect();
00167 if(rev) fbServerConnected=kFALSE;
00168 if(!isterminating) StartWorkThreads();
00169 }
00170 return rev;
00171 }
00172
00173 void TGo4ClientTask::Quit()
00174 {
00175 TGo4Log::Debug(" ClientTask''%s'' is quitting... ",GetName());
00176 SendStatusMessage(2,kTRUE,"ClientTask %s is terminating...",GetName());
00177 TGo4Task::Quit();
00178 DisconnectServer(kTRUE);
00179
00180 Terminate(!IsMaster());
00181 }
00182
00183 TGo4TaskStatus* TGo4ClientTask::CreateStatus()
00184 {
00185 TGo4ClientStatus* stat= new TGo4ClientStatus(GetName());
00186 UpdateStatus(stat);
00187 return stat;
00188 }
00189
00190
00191 void TGo4ClientTask::UpdateStatus(TGo4TaskStatus* state)
00192 {
00193 TGo4Task::UpdateStatus(state);
00194 TGo4ClientStatus* clstate=dynamic_cast<TGo4ClientStatus*>(state);
00195 if(clstate)
00196 clstate->SetNames(GetServerHostName());
00197 }
00198
00199 void TGo4ClientTask::AddLocalCommand(TGo4Command * com)
00200 {
00201 if(com==0) return;
00202 if(fxCommandQ)
00203 {
00204 fxCommandQ->AddBufferFromObject(com);
00205 }
00206 else
00207 {
00208 TGo4Log::Debug(" !!! ClientTask - ERROR adding local command - no command queue !!! ");
00209 }
00210 }
00211
00212 TGo4BufferQueue* TGo4ClientTask::GetCommandQueue(const char*)
00213 {
00214 return fxCommandQ;
00215 }
00216
00217 TGo4BufferQueue * TGo4ClientTask::GetStatusQueue(const char*)
00218 {
00219 return fxStatusQ;
00220 }
00221 TGo4BufferQueue * TGo4ClientTask::GetDataQueue(const char*)
00222 {
00223 return fxDataQ;
00224 }