GSI Object Oriented Online Offline (Go4) GO4-6.4.0
Loading...
Searching...
No Matches
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(),
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"),
48 fxHostname(),
49 fiPort(0),
50 fxTransport(nullptr)
51{
52 SetParms(base,passwd,host,port);
53 fxTransport = new TGo4Socket(kTRUE);
54}
55
60
61void 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
106TObject *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()))
162 fxTransport->Send(TGo4TaskHandler::Get_fgcOK()); // final OK
163 return obj;
164}
165
166void 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:
179 fxTransport->Send(GetBase());
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:
188 fxTransport->Send(GetPasswd());
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}
#define TGo4LockGuard
Contains the name (key) list of all objects in the analysis scope.
static const char * fgcCOMGETLIST
command string for object server nameslist request
static void Debug(const char *text,...) GO4_PRINTF_ARGS
User shortcut for message with prio 0.
Definition TGo4Log.cxx:281
static void Error(const char *text,...) GO4_PRINTF_ARGS
User shortcut for message with prio 3.
Definition TGo4Log.cxx:320
TBuffer * ReceiveBuffer()
Receive root buffer containing requested object.
void SendCommand(const char *com)
Send (command) string to server.
TObject * ReceiveObject()
Receive root object streamed from server.
void SetBase(const char *serverbase)
void SetPort(Int_t port)
void SetParms(const char *base, const char *passwd, const char *host, Int_t port)
Set server parameters if necessary.
const char * GetBase() const
TGo4Socket * fxTransport
transport socket for connection to server
TObject * RequestObject(const char *objectname, const char *base=nullptr, const char *passwd=nullptr, const char *host=nullptr, Int_t port=0)
Get object by name from client.
Int_t DisconnectServer()
void SetHost(const char *serverhost)
Int_t GetPort() const
const char * GetPasswd() const
virtual ~TGo4ObjClient()
Int_t fiPort
serversocket port number
TString fxServerPass
contains api password
TString fxServerName
contains api basename
TGo4AnalysisObjectNames * RequestNamesList(const char *base=nullptr, const char *passwd=nullptr, const char *host=nullptr, Int_t port=0)
Get nameslist object from client.
const char * GetHost() const
TString fxHostname
hostname of the server to connect to
void SetPasswd(const char *serverpasswd)
static void SetLastSignal(Int_t v=0)
static const char * Get_fgcOK()