GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TGo4ObjClient.cxx
Go to the documentation of this file.
1 // $Id$
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(nullptr)
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(nullptr)
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 = nullptr;
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 = nullptr;
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 = nullptr;
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 = nullptr;
124  }
126  return obj;
127 }
128 
129 
131 {
132  TObject *obj = nullptr;
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  buffer->Reset();
148  obj = buffer->ReadObject(nullptr); // ReadObject(cl)
149  if(obj) std::cout <<"read object of class"<<obj->ClassName() << std::endl;
150  savdir->cd();
151  } // TGo4LockGuard
152  }
153  else
154  {
155  TGo4Log::Error("error receiving object");
156  } // if(buffer)
157  }
158  else
159  {
160  TGo4Log::Error("Server did not send object, probably unknown!");
161  } // if(!strcmp(recvchar,TGo4TaskHandler::Get_fgcOK()))
163  return obj;
164 }
165 
166 void TGo4ObjClient::SendCommand(const char *com)
167 {
168  if (fxTransport) fxTransport->Send(com);
169 }
170 
171 
173 {
174  char *recvchar = nullptr;
175  Int_t openok = fxTransport->Open(GetHost(), GetPort());
176  if (openok != 0)
177  return 1;
178  // send basename:
180  recvchar = fxTransport->RecvRaw("dummy");
181  if (!recvchar)
182  return 1;
183  if (strcmp(recvchar, TGo4TaskHandler::Get_fgcOK())) {
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)
191  return 1;
192  if (strcmp(recvchar, TGo4TaskHandler::Get_fgcOK())) {
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  TBuffer *rev = nullptr;
208  Int_t state = fxTransport->ReceiveBuffer();
209  if (state >= 0) {
210  rev = const_cast<TBuffer *>(fxTransport->GetBuffer());
211  // currently, we pass out the internal buffer of the TGo4Socket
212  // maybe we have to put a queue of length 1 in between?
213  // fxBufferQueue->AddBuffer(buf, kTRUE);
214  }
215  else {
216  // error
218  // TSocket error because of window resize, do not abort!
219  TGo4Log::Debug(" %s: caught SIGWINCH ", GetName());
221  rev = nullptr;
222  } else {
223  TGo4Log::Debug(" !!!Receive Error in Object Client %s!!!", GetName());
224  rev = nullptr; // here we might throw some exception later....
225  }
226  }
227  return rev;
228 }
void SetBase(const char *serverbase)
Definition: TGo4ObjClient.h:59
virtual Int_t Close(Option_t *opt="")
Definition: TGo4Socket.cxx:210
void SetPasswd(const char *serverpasswd)
Definition: TGo4ObjClient.h:60
TString fxServerName
Definition: TGo4ObjClient.h:80
virtual Int_t Send(TObject *obj)
Definition: TGo4Socket.cxx:332
const char * GetHost() const
Definition: TGo4ObjClient.h:66
TObject * ReceiveObject()
TString fxHostname
Definition: TGo4ObjClient.h:86
TGo4Socket * fxTransport
Definition: TGo4ObjClient.h:92
const char * GetBase() const
Definition: TGo4ObjClient.h:64
Int_t GetPort() const
Definition: TGo4ObjClient.h:67
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)
void SetHost(const char *serverhost)
Definition: TGo4ObjClient.h:61
void SetParms(const char *base, const char *passwd, const char *host, Int_t port)
static void Debug(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:281
Int_t ReceiveBuffer()
Definition: TGo4Socket.cxx:273
void SetPort(Int_t port)
Definition: TGo4ObjClient.h:62
virtual char * RecvRaw(const char *name=nullptr)
Definition: TGo4Socket.cxx:401
TString fxServerPass
Definition: TGo4ObjClient.h:83
Int_t DisconnectServer()
TGo4AnalysisObjectNames * RequestNamesList(const char *base=nullptr, const char *passwd=nullptr, const char *host=nullptr, Int_t port=0)
static void Error(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:320
virtual ~TGo4ObjClient()
TObject * RequestObject(const char *objectname, const char *base=nullptr, const char *passwd=nullptr, const char *host=nullptr, Int_t port=0)
Int_t ConnectServer()
const char * GetPasswd() const
Definition: TGo4ObjClient.h:65
static const char * fgcCOMGETLIST
void SendCommand(const char *com)
TBuffer * ReceiveBuffer()
const TBuffer * GetBuffer() const
Definition: TGo4Socket.h:59