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