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