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