GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4ObjClient.cxx
Go to the documentation of this file.
1 // $Id: TGo4ObjClient.cxx 999 2013-07-25 11:58:59Z linev $
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum f�r Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4ObjClient.h"
15 
16 #include <signal.h>
17 #include "Riostream.h"
18 
19 #include "TROOT.h"
20 #include "TClass.h"
21 #include "TDirectory.h"
22 
23 #include "TGo4Log.h"
24 #include "TGo4LockGuard.h"
25 #include "TGo4Socket.h"
27 #include "TGo4TaskHandler.h"
29 #include "TGo4HistogramServer.h"
30 
32  TNamed(),
33  fxServerName(),
34  fxServerPass(),
35  fxHostname(),
36  fiPort(0),
37  fxTransport(0)
38 {
39 }
40 
42  const char* base,
43  const char* passwd,
44  const char* host,
45  Int_t port) :
46  TNamed(name, "This is a Go4 object client"),
47  fxServerName(),
48  fxServerPass(),
49  fxHostname(),
50  fiPort(0),
51  fxTransport(0)
52 {
53  SetParms(base,passwd,host,port);
54  fxTransport = new TGo4Socket(kTRUE);
55 }
56 
58 {
59  delete fxTransport;
60 }
61 
62 void TGo4ObjClient::SetParms(const char* base,
63  const char* passwd,
64  const char* host,
65  Int_t port)
66 {
67  if(base) SetBase(base);
68  if(passwd) SetPasswd(passwd);
69  if(host) SetHost(host);
70  if(port) SetPort(port);
71 }
72 
73 
74 
76  const char* passwd,
77  const char* host,
78  Int_t port)
79 {
80  //
81  TGo4AnalysisObjectNames* nameslist=0;
82  SetParms(base,passwd,host,port);
83  if(ConnectServer()==0)
84  {
85  // connection successful, go on
86  SendCommand(TGo4HistogramServer::fgcCOMGETLIST); // send nameslist request
87  TObject* obj=ReceiveObject(); // get incoming buffer and stream object
88  if(obj && obj->InheritsFrom(TGo4AnalysisObjectNames::Class()))
89  {
90  nameslist=dynamic_cast<TGo4AnalysisObjectNames*>(obj);
91  }
92  else
93  {
94  TGo4Log::Error("error receiving nameslist !!!");
95  }
96  }
97  else
98  {
99  TGo4Log::Error("error on connection in RequestNamesList");
100  nameslist=0;
101  }
103  return nameslist;
104 }
105 
106 
107 TObject* TGo4ObjClient::RequestObject(const char* objectname,
108  const char* base,
109  const char* passwd,
110  const char* host,
111  Int_t port)
112 {
113  TObject* obj=0;
114  SetParms(base,passwd,host,port);
115  if(ConnectServer()==0)
116  {
117  // connection successful, go on
118  SendCommand(objectname); // command is name of object to get
119  obj=ReceiveObject(); // get incoming buffer and stream object
120  }
121  else
122  {
123  TGo4Log::Error("error on connection in RequestObject");
124  obj=0;
125  }
127  return obj;
128 }
129 
130 
132 {
133  TObject* obj=0;
134  // check for OK signal (object is existing on server)
135  char* recvchar=fxTransport->RecvRaw("dummy");
136  if(recvchar && !strcmp(recvchar,TGo4TaskHandler::Get_fgcOK()))
137  {
138  TBuffer* buffer=ReceiveBuffer();
139  if(buffer)
140  {
141  // reconstruct object from buffer
142  {
143  TGo4LockGuard mainguard;
144  // lock go4 main mutex for streaming
145  TDirectory* savdir=gDirectory;
146  gROOT->cd(); // be sure to be in the top directory when creating histo
147  buffer->SetReadMode();
148  //std::cout << "Reading object from buffer..."<< std::endl;
149  buffer->Reset();
150  obj=buffer->ReadObject(0); // ReadObject(cl)
151  if(obj) std::cout <<"read object of class"<<obj->ClassName() << std::endl;
152  savdir->cd();
153  } // TGo4LockGuard
154  }
155  else
156  {
157  TGo4Log::Error("error receiving object");
158  } // if(buffer)
159  }
160  else
161  {
162  TGo4Log::Error("Server did not send object, probably unknown!");
163  } // if(!strcmp(recvchar,TGo4TaskHandler::Get_fgcOK()))
165  return obj;
166 }
167 
168 void TGo4ObjClient::SendCommand(const char* com)
169 {
170  if (fxTransport) fxTransport->Send(com);
171 }
172 
173 
175 {
176 char* recvchar=0;
177 Int_t openok=fxTransport->Open(GetHost(),GetPort());
178 if(openok!=0) return 1;
179 // send basename:
181 recvchar=fxTransport->RecvRaw("dummy");
182 if(recvchar==0) return 1;
183 if(strcmp(recvchar,TGo4TaskHandler::Get_fgcOK()))
184 {
185  TGo4Log::Debug(" Wrong basename for object server (host %s ,port %d)", GetHost(), GetPort());
186  return 2;
187 }
188 // send password:
190 recvchar=fxTransport->RecvRaw("dummy");
191 if(recvchar==0) return 1;
192 if(strcmp(recvchar,TGo4TaskHandler::Get_fgcOK()))
193 {
194  TGo4Log::Debug(" Wrong password for object server (host %s ,port %d)", GetHost(), GetPort());
195  return 4;
196 }
197 return 0;
198 }
199 
201 {
202  fxTransport->Close();
203 return 0;
204 }
205 
207 {
208 
209 TBuffer* rev=0;
210 Int_t state=fxTransport->ReceiveBuffer();
211 if(state>=0)
212  {
213  rev=const_cast<TBuffer*> (fxTransport->GetBuffer());
214  // currently, we pass out the internal buffer of the TGo4Socket
215  // maybe we have to put a queue of length 1 in between?
216  //fxBufferQueue->AddBuffer(buf, kTRUE);
217  }
218 else
219  {
220  // error
222  {
223  // TSocket error because of window resize, do not abort!
224  TGo4Log::Debug(" %s: caught SIGWINCH ",GetName());
226  rev = 0;
227  }
228  else
229  {
230  TGo4Log::Debug(" !!!Receive Error in Object Client %s!!!",GetName());
231  rev=0; // here we might throw some exception later....
232  }
233  } // end if(rev>=0)
234 return rev;
235 }
void SetBase(const char *serverbase)
Definition: TGo4ObjClient.h:60
virtual Int_t Close(Option_t *opt="")
Definition: TGo4Socket.cxx:226
void SetPasswd(const char *serverpasswd)
Definition: TGo4ObjClient.h:61
const TBuffer * GetBuffer() const
Definition: TGo4Socket.h:58
virtual Int_t Send(TObject *obj)
Definition: TGo4Socket.cxx:352
TObject * ReceiveObject()
TGo4Socket * fxTransport
Definition: TGo4ObjClient.h:93
TGo4AnalysisObjectNames * RequestNamesList(const char *base=0, const char *passwd=0, const char *host=0, Int_t port=0)
static const char * Get_fgcOK()
virtual Int_t Open(const char *host, Int_t port, Bool_t keepservsock=kFALSE)
Definition: TGo4Socket.cxx:106
static void SetLastSignal(Int_t v=0)
const char * GetHost() const
Definition: TGo4ObjClient.h:67
void SetHost(const char *serverhost)
Definition: TGo4ObjClient.h:62
void SetParms(const char *base, const char *passwd, const char *host, Int_t port)
Int_t ReceiveBuffer()
Definition: TGo4Socket.cxx:289
const char * GetPasswd() const
Definition: TGo4ObjClient.h:66
void SetPort(Int_t port)
Definition: TGo4ObjClient.h:63
Int_t DisconnectServer()
Int_t GetPort() const
Definition: TGo4ObjClient.h:68
TObject * RequestObject(const char *objectname, const char *base=0, const char *passwd=0, const char *host=0, Int_t port=0)
virtual ~TGo4ObjClient()
const char * GetBase() const
Definition: TGo4ObjClient.h:65
Int_t ConnectServer()
static const char * fgcCOMGETLIST
virtual char * RecvRaw(const char *name=0)
Definition: TGo4Socket.cxx:421
static void Error(const char *text,...)
Definition: TGo4Log.cxx:309
void SendCommand(const char *com)
static void Debug(const char *text,...)
Definition: TGo4Log.cxx:270
TBuffer * ReceiveBuffer()