00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TGo4ObjClient.h"
00017
00018 #include <signal.h>
00019 #include <iostream.h>
00020
00021 #include "TROOT.h"
00022 #include "TClass.h"
00023 #include "TBuffer.h"
00024 #include "TDirectory.h"
00025
00026 #include "Go4Log/TGo4Log.h"
00027 #include "Go4LockGuard/TGo4LockGuard.h"
00028 #include "Go4Socket/TGo4Socket.h"
00029 #include "Go4Socket/TGo4SocketSignalHandler.h"
00030 #include "Go4TaskHandler/TGo4TaskHandler.h"
00031 #include "Go4StatusAnalysis/TGo4AnalysisObjectNames.h"
00032 #include "TGo4HistogramServer.h"
00033
00034 TGo4ObjClient::TGo4ObjClient(const Text_t* name,
00035 const Text_t* base,
00036 const Text_t* passwd,
00037 const Text_t* host,
00038 Int_t port)
00039 : TNamed(name, "This is a Go4 object client")
00040 {
00041 SetParms(base,passwd,host,port);
00042 fxTransport=new TGo4Socket(kTRUE);
00043 }
00044
00045 TGo4ObjClient::~TGo4ObjClient()
00046 {
00047 delete fxTransport;
00048
00049 }
00050
00051 void TGo4ObjClient::SetParms(const Text_t* base,
00052 const Text_t* passwd,
00053 const Text_t* host,
00054 Int_t port)
00055 {
00056 if(base) SetBase(base);
00057 if(passwd) SetPasswd(passwd);
00058 if(host) SetHost(host);
00059 if(port) SetPort(port);
00060 }
00061
00062
00063
00064 TGo4AnalysisObjectNames * TGo4ObjClient::RequestNamesList(const Text_t* base,
00065 const Text_t* passwd,
00066 const Text_t* host,
00067 Int_t port)
00068 {
00069
00070 TGo4AnalysisObjectNames* nameslist=0;
00071 SetParms(base,passwd,host,port);
00072 if(ConnectServer()==0)
00073 {
00074
00075 SendCommand(TGo4HistogramServer::fgcCOMGETLIST);
00076 TObject* obj=ReceiveObject();
00077 if(obj && obj->InheritsFrom("TGo4AnalysisObjectNames"))
00078 {
00079 nameslist=dynamic_cast<TGo4AnalysisObjectNames*>(obj);
00080 }
00081 else
00082 {
00083 cout <<"error receiving nameslist !!!" << endl;
00084 }
00085 }
00086 else
00087 {
00088 cout <<"error on connection in RequestNamesList" << endl;
00089 nameslist=0;
00090 }
00091 DisconnectServer();
00092 return nameslist;
00093 }
00094
00095
00096 TObject* TGo4ObjClient::RequestObject(const Text_t* objectname,
00097 const Text_t* base,
00098 const Text_t* passwd,
00099 const Text_t* host,
00100 Int_t port)
00101 {
00102 TObject* obj=0;
00103 SetParms(base,passwd,host,port);
00104 if(ConnectServer()==0)
00105 {
00106
00107 SendCommand(objectname);
00108 obj=ReceiveObject();
00109 }
00110 else
00111 {
00112 cout <<"error on connection in RequestObject" << endl;
00113 obj=0;
00114 }
00115 DisconnectServer();
00116 return obj;
00117 }
00118
00119
00120 TObject* TGo4ObjClient::ReceiveObject()
00121 {
00122 TObject* obj=0;
00123
00124 Text_t* recvchar=fxTransport->RecvRaw("dummy");
00125 if(recvchar && !strcmp(recvchar,TGo4TaskHandler::fgcOK))
00126 {
00127 TBuffer* buffer=ReceiveBuffer();
00128 if(buffer)
00129 {
00130
00131 {
00132 TGo4LockGuard mainguard;
00133
00134 TDirectory* savdir=gDirectory;
00135 gROOT->cd();
00136 buffer->SetReadMode();
00137
00138 buffer->Reset();
00139 obj=buffer->ReadObject(0);
00140 if(obj) cout <<"read object of class"<<obj->IsA()->GetName() << endl;
00141 savdir->cd();
00142 }
00143 }
00144 else
00145 {
00146 cout <<"error receiving object" << endl;
00147 }
00148 }
00149 else
00150 {
00151 cout <<"Server did not send object, probably unknown!" << endl;
00152 }
00153 fxTransport->Send(TGo4TaskHandler::fgcOK);
00154 return obj;
00155 }
00156
00157 void TGo4ObjClient::SendCommand(const Text_t* com)
00158 {
00159 if (fxTransport) fxTransport->Send(com);
00160 }
00161
00162
00163 Int_t TGo4ObjClient::ConnectServer()
00164 {
00165
00166 Text_t* recvchar=0;
00167 Int_t openok=fxTransport->Open(GetHost(),GetPort());
00168 if(openok!=0) return 1;
00169
00170 fxTransport->Send(GetBase());
00171 recvchar=fxTransport->RecvRaw("dummy");
00172 if(recvchar==0) return 1;
00173 if(strcmp(recvchar,TGo4TaskHandler::fgcOK))
00174 {
00175 TGo4Log::Debug(" Wrong basename for object server (host %s ,port %d)", GetHost(), GetPort());
00176 return 2;
00177 }
00178
00179 fxTransport->Send(GetPasswd());
00180 recvchar=fxTransport->RecvRaw("dummy");
00181 if(recvchar==0) return 1;
00182 if(strcmp(recvchar,TGo4TaskHandler::fgcOK))
00183 {
00184 TGo4Log::Debug(" Wrong password for object server (host %s ,port %d)", GetHost(), GetPort());
00185 return 4;
00186 }
00187 return 0;
00188 }
00189
00190 Int_t TGo4ObjClient::DisconnectServer()
00191 {
00192 fxTransport->Close();
00193 return 0;
00194 }
00195
00196 TBuffer* TGo4ObjClient::ReceiveBuffer()
00197 {
00198
00199 TBuffer* rev=0;
00200 Int_t state=fxTransport->ReceiveBuffer();
00201 if(state>=0)
00202 {
00203 rev=const_cast<TBuffer*> (fxTransport->GetBuffer());
00204
00205
00206
00207 }
00208 else
00209 {
00210
00211 if (TGo4SocketSignalHandler::fgiLastSignal == SIGWINCH)
00212 {
00213
00214 TGo4Log::Debug(" %s: caught SIGWINCH ",GetName());
00215 TGo4SocketSignalHandler::fgiLastSignal = 0;
00216 rev = 0;
00217 }
00218 else
00219 {
00220 TGo4Log::Debug(" !!!Receive Error in Object Client %s!!!",GetName());
00221 rev=0;
00222 }
00223 }
00224 return rev;
00225 }
00226
00227 ClassImp(TGo4ObjClient)
00228
00229