GSI Object Oriented Online Offline (Go4)  GO4-6.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4ObjClient.cxx
Go to the documentation of this file.
1 // $Id: TGo4ObjClient.cxx 2750 2020-04-16 10:29:58Z 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 fuer 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 <iostream>
17 
18 #include "TROOT.h"
19 #include "TDirectory.h"
20 #include "TBuffer.h"
21 
22 #include "TGo4Log.h"
23 #include "TGo4LockGuard.h"
24 #include "TGo4Socket.h"
26 #include "TGo4TaskHandler.h"
28 #include "TGo4HistogramServer.h"
29 
31  TNamed(),
32  fxServerName(),
33  fxServerPass(),
34  fxHostname(),
35  fiPort(0),
36  fxTransport(0)
37 {
38 }
39 
41  const char* base,
42  const char* passwd,
43  const char* host,
44  Int_t port) :
45  TNamed(name, "This is a Go4 object client"),
46  fxServerName(),
47  fxServerPass(),
48  fxHostname(),
49  fiPort(0),
50  fxTransport(0)
51 {
52  SetParms(base,passwd,host,port);
53  fxTransport = new TGo4Socket(kTRUE);
54 }
55 
57 {
58  delete fxTransport;
59 }
60 
61 void TGo4ObjClient::SetParms(const char* base,
62  const char* passwd,
63  const char* host,
64  Int_t port)
65 {
66  if(base) SetBase(base);
67  if(passwd) SetPasswd(passwd);
68  if(host) SetHost(host);
69  if(port) SetPort(port);
70 }
71 
72 
73 
75  const char* passwd,
76  const char* host,
77  Int_t port)
78 {
79  //
80  TGo4AnalysisObjectNames* nameslist=0;
81  SetParms(base,passwd,host,port);
82  if(ConnectServer()==0)
83  {
84  // connection successful, go on
85  SendCommand(TGo4HistogramServer::fgcCOMGETLIST); // send nameslist request
86  TObject* obj=ReceiveObject(); // get incoming buffer and stream object
87  if(obj && obj->InheritsFrom(TGo4AnalysisObjectNames::Class()))
88  {
89  nameslist=dynamic_cast<TGo4AnalysisObjectNames*>(obj);
90  }
91  else
92  {
93  TGo4Log::Error("error receiving nameslist !!!");
94  }
95  }
96  else
97  {
98  TGo4Log::Error("error on connection in RequestNamesList");
99  nameslist=0;
100  }
102  return nameslist;
103 }
104 
105 
106 TObject* TGo4ObjClient::RequestObject(const char* objectname,
107  const char* base,
108  const char* passwd,
109  const char* host,
110  Int_t port)
111 {
112  TObject* obj=0;
113  SetParms(base,passwd,host,port);
114  if(ConnectServer()==0)
115  {
116  // connection successful, go on
117  SendCommand(objectname); // command is name of object to get
118  obj=ReceiveObject(); // get incoming buffer and stream object
119  }
120  else
121  {
122  TGo4Log::Error("error on connection in RequestObject");
123  obj=0;
124  }
126  return obj;
127 }
128 
129 
131 {
132  TObject* obj=0;
133  // check for OK signal (object is existing on server)
134  char* recvchar=fxTransport->RecvRaw("dummy");
135  if(recvchar && !strcmp(recvchar,TGo4TaskHandler::Get_fgcOK()))
136  {
137  TBuffer* buffer=ReceiveBuffer();
138  if(buffer)
139  {
140  // reconstruct object from buffer
141  {
142  TGo4LockGuard mainguard;
143  // lock go4 main mutex for streaming
144  TDirectory* savdir=gDirectory;
145  gROOT->cd(); // be sure to be in the top directory when creating histo
146  buffer->SetReadMode();
147  //std::cout << "Reading object from buffer..."<< std::endl;
148  buffer->Reset();
149  obj=buffer->ReadObject(0); // ReadObject(cl)
150  if(obj) std::cout <<"read object of class"<<obj->ClassName() << std::endl;
151  savdir->cd();
152  } // TGo4LockGuard
153  }
154  else
155  {
156  TGo4Log::Error("error receiving object");
157  } // if(buffer)
158  }
159  else
160  {
161  TGo4Log::Error("Server did not send object, probably unknown!");
162  } // if(!strcmp(recvchar,TGo4TaskHandler::Get_fgcOK()))
164  return obj;
165 }
166 
167 void TGo4ObjClient::SendCommand(const char* com)
168 {
169  if (fxTransport) fxTransport->Send(com);
170 }
171 
172 
174 {
175 char* recvchar=0;
176 Int_t openok=fxTransport->Open(GetHost(),GetPort());
177 if(openok!=0) return 1;
178 // send basename:
180 recvchar=fxTransport->RecvRaw("dummy");
181 if(recvchar==0) return 1;
182 if(strcmp(recvchar,TGo4TaskHandler::Get_fgcOK()))
183 {
184  TGo4Log::Debug(" Wrong basename for object server (host %s ,port %d)", GetHost(), GetPort());
185  return 2;
186 }
187 // send password:
189 recvchar=fxTransport->RecvRaw("dummy");
190 if(recvchar==0) return 1;
191 if(strcmp(recvchar,TGo4TaskHandler::Get_fgcOK()))
192 {
193  TGo4Log::Debug(" Wrong password for object server (host %s ,port %d)", GetHost(), GetPort());
194  return 4;
195 }
196 return 0;
197 }
198 
200 {
201  fxTransport->Close();
202 return 0;
203 }
204 
206 {
207 
208 TBuffer* rev=0;
209 Int_t state=fxTransport->ReceiveBuffer();
210 if(state>=0)
211  {
212  rev=const_cast<TBuffer*> (fxTransport->GetBuffer());
213  // currently, we pass out the internal buffer of the TGo4Socket
214  // maybe we have to put a queue of length 1 in between?
215  //fxBufferQueue->AddBuffer(buf, kTRUE);
216  }
217 else
218  {
219  // error
221  {
222  // TSocket error because of window resize, do not abort!
223  TGo4Log::Debug(" %s: caught SIGWINCH ",GetName());
225  rev = 0;
226  }
227  else
228  {
229  TGo4Log::Debug(" !!!Receive Error in Object Client %s!!!",GetName());
230  rev=0; // here we might throw some exception later....
231  }
232  } // end if(rev>=0)
233 return rev;
234 }
void SetBase(const char *serverbase)
Definition: TGo4ObjClient.h:59
virtual Int_t Close(Option_t *opt="")
Definition: TGo4Socket.cxx:212
void SetPasswd(const char *serverpasswd)
Definition: TGo4ObjClient.h:60
const TBuffer * GetBuffer() const
Definition: TGo4Socket.h:58
virtual Int_t Send(TObject *obj)
Definition: TGo4Socket.cxx:338
TObject * ReceiveObject()
TGo4Socket * fxTransport
Definition: TGo4ObjClient.h:92
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:92
static void SetLastSignal(Int_t v=0)
const char * GetHost() const
Definition: TGo4ObjClient.h:66
void SetHost(const char *serverhost)
Definition: TGo4ObjClient.h:61
void SetParms(const char *base, const char *passwd, const char *host, Int_t port)
Int_t ReceiveBuffer()
Definition: TGo4Socket.cxx:275
const char * GetPasswd() const
Definition: TGo4ObjClient.h:65
void SetPort(Int_t port)
Definition: TGo4ObjClient.h:62
Int_t DisconnectServer()
Int_t GetPort() const
Definition: TGo4ObjClient.h:67
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:64
Int_t ConnectServer()
static const char * fgcCOMGETLIST
virtual char * RecvRaw(const char *name=0)
Definition: TGo4Socket.cxx:407
static void Error(const char *text,...)
Definition: TGo4Log.cxx:323
void SendCommand(const char *com)
static void Debug(const char *text,...)
Definition: TGo4Log.cxx:284
TBuffer * ReceiveBuffer()