Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

TGo4ObjClient.cxx

Go to the documentation of this file.
00001 //-------------------------------------------------------------
00002 //        Go4 Release Package v3.04-01 (build 30401)
00003 //                      28-November-2008
00004 //---------------------------------------------------------------
00005 //   The GSI Online Offline Object Oriented (Go4) Project
00006 //   Experiment Data Processing at EE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 #include "TGo4ObjClient.h"
00017 
00018 #include <signal.h>
00019 #include "Riostream.h"
00020 
00021 #include "TROOT.h"
00022 #include "TClass.h"
00023 #include "TDirectory.h"
00024 
00025 #include "TGo4Log.h"
00026 #include "TGo4LockGuard.h"
00027 #include "TGo4Socket.h"
00028 #include "TGo4SocketSignalHandler.h"
00029 #include "TGo4TaskHandler.h"
00030 #include "TGo4AnalysisObjectNames.h"
00031 #include "TGo4HistogramServer.h"
00032 
00033 TGo4ObjClient::TGo4ObjClient() :
00034    TNamed(),
00035    fxServerName(),
00036    fxServerPass(),
00037    fxHostname(),
00038    fiPort(0),
00039    fxTransport(0)
00040 {
00041 }
00042 
00043 TGo4ObjClient::TGo4ObjClient(const char* name,
00044                                  const char* base,
00045                                  const char* passwd,
00046                                  const char* host,
00047                                  Int_t port) :
00048    TNamed(name, "This is a Go4 object client"),
00049    fxServerName(),
00050    fxServerPass(),
00051    fxHostname(),
00052    fiPort(0),
00053    fxTransport(0)
00054 {
00055    SetParms(base,passwd,host,port);
00056    fxTransport = new TGo4Socket(kTRUE);
00057 }
00058 
00059 TGo4ObjClient::~TGo4ObjClient()
00060 {
00061    delete fxTransport;
00062 }
00063 
00064 void TGo4ObjClient::SetParms(const char* base,
00065                const char* passwd,
00066                const char* host,
00067                Int_t port)
00068 {
00069    if(base) SetBase(base);
00070    if(passwd) SetPasswd(passwd);
00071    if(host) SetHost(host);
00072    if(port) SetPort(port);
00073 }
00074 
00075 
00076 
00077 TGo4AnalysisObjectNames * TGo4ObjClient::RequestNamesList(const char* base,
00078                                                const char* passwd,
00079                                                const char* host,
00080                                                Int_t port)
00081 {
00082 //
00083 TGo4AnalysisObjectNames* nameslist=0;
00084 SetParms(base,passwd,host,port);
00085 if(ConnectServer()==0)
00086    {
00087       // connection successful, go on
00088       SendCommand(TGo4HistogramServer::fgcCOMGETLIST); // send nameslist request
00089       TObject* obj=ReceiveObject(); // get incoming buffer and stream object
00090       if(obj && obj->InheritsFrom(TGo4AnalysisObjectNames::Class()))
00091         {
00092           nameslist=dynamic_cast<TGo4AnalysisObjectNames*>(obj);
00093         }
00094       else
00095         {
00096           cout <<"error receiving nameslist !!!" << endl;
00097         }
00098    }
00099 else
00100    {
00101       cout <<"error on connection in RequestNamesList" << endl;
00102       nameslist=0;
00103    }
00104 DisconnectServer();
00105 return nameslist;
00106 }
00107 
00108 
00109 TObject* TGo4ObjClient::RequestObject(const char* objectname,
00110                                        const char* base,
00111                                        const char* passwd,
00112                                        const char* host,
00113                                        Int_t port)
00114 {
00115 TObject* obj=0;
00116 SetParms(base,passwd,host,port);
00117 if(ConnectServer()==0)
00118    {
00119       // connection successful, go on
00120       SendCommand(objectname); // command is name of object to get
00121       obj=ReceiveObject(); // get incoming buffer and stream object
00122    }
00123 else
00124    {
00125       cout <<"error on connection in RequestObject" << endl;
00126       obj=0;
00127    }
00128 DisconnectServer();
00129 return obj;
00130 }
00131 
00132 
00133 TObject* TGo4ObjClient::ReceiveObject()
00134 {
00135 TObject* obj=0;
00136 // check for OK signal (object is existing on server)
00137 char* recvchar=fxTransport->RecvRaw("dummy");
00138 if(recvchar && !strcmp(recvchar,TGo4TaskHandler::Get_fgcOK()))
00139    {
00140       TBuffer* buffer=ReceiveBuffer();
00141       if(buffer)
00142          {
00143              // reconstruct object from buffer
00144              {
00145                TGo4LockGuard mainguard;
00146                // lock go4 main mutex for streaming
00147                TDirectory* savdir=gDirectory;
00148                gROOT->cd(); // be sure to be in the top directory when creating histo
00149                buffer->SetReadMode();
00150                //cout << "Reading object from buffer..."<< endl;
00151                buffer->Reset();
00152                obj=buffer->ReadObject(0); // ReadObject(cl)
00153                if(obj) cout <<"read object of class"<<obj->ClassName() << endl;
00154                savdir->cd();
00155             } //  TGo4LockGuard
00156          }
00157       else
00158          {
00159             cout <<"error receiving object" << endl;
00160          } // if(buffer)
00161    }
00162 else
00163    {
00164       cout <<"Server did not send object, probably unknown!" << endl;
00165    } // if(!strcmp(recvchar,TGo4TaskHandler::Get_fgcOK()))
00166   fxTransport->Send(TGo4TaskHandler::Get_fgcOK()); // final OK
00167 return obj;
00168 }
00169 
00170 void TGo4ObjClient::SendCommand(const char* com)
00171 {
00172    if (fxTransport) fxTransport->Send(com);
00173 }
00174 
00175 
00176 Int_t TGo4ObjClient::ConnectServer()
00177 {
00178 char* recvchar=0;
00179 Int_t openok=fxTransport->Open(GetHost(),GetPort());
00180 if(openok!=0) return 1;
00181 // send basename:
00182 fxTransport->Send(GetBase());
00183 recvchar=fxTransport->RecvRaw("dummy");
00184 if(recvchar==0) return 1;
00185 if(strcmp(recvchar,TGo4TaskHandler::Get_fgcOK()))
00186 {
00187    TGo4Log::Debug(" Wrong basename for object server (host %s ,port %d)", GetHost(), GetPort());
00188    return 2;
00189 }
00190 // send password:
00191 fxTransport->Send(GetPasswd());
00192 recvchar=fxTransport->RecvRaw("dummy");
00193 if(recvchar==0) return 1;
00194 if(strcmp(recvchar,TGo4TaskHandler::Get_fgcOK()))
00195 {
00196    TGo4Log::Debug(" Wrong password for object server (host %s ,port %d)", GetHost(), GetPort());
00197    return 4;
00198 }
00199 return 0;
00200 }
00201 
00202 Int_t TGo4ObjClient::DisconnectServer()
00203 {
00204    fxTransport->Close();
00205 return 0;
00206 }
00207 
00208 TBuffer* TGo4ObjClient::ReceiveBuffer()
00209 {
00210 
00211 TBuffer* rev=0;
00212 Int_t state=fxTransport->ReceiveBuffer();
00213 if(state>=0)
00214    {
00215       rev=const_cast<TBuffer*> (fxTransport->GetBuffer());
00216       // currently, we pass out the internal buffer of the TGo4Socket
00217       // maybe we have to put a queue of length 1 in between?
00218       //fxBufferQueue->AddBuffer(buf, kTRUE);
00219    } 
00220 else
00221    {
00222       // error
00223       if (TGo4SocketSignalHandler::GetLastSignal() == SIGWINCH)
00224         {
00225             // TSocket error because of window resize, do not abort!
00226             TGo4Log::Debug(" %s: caught SIGWINCH ",GetName());
00227             TGo4SocketSignalHandler::SetLastSignal(0); // reset
00228             rev = 0;
00229          }
00230       else
00231          {
00232             TGo4Log::Debug(" !!!Receive Error in Object Client %s!!!",GetName());
00233             rev=0;  // here we might throw some exception later....
00234           }
00235    } // end if(rev>=0)
00236 return rev;
00237 }
00238 
00239 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Fri Nov 28 12:59:26 2008 for Go4-v3.04-1 by  doxygen 1.4.2