Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Go4HistogramServer/TGo4ObjClient.cxx

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

Generated on Tue Nov 8 10:56:04 2005 for Go4-v2.10-5 by doxygen1.2.15