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