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