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

/Go4TaskHandler/TGo4ClientTask.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 "TGo4ClientTask.h"
00017 
00018 #include <iostream.h>
00019 
00020 #include "TFile.h"
00021 
00022 #include "Go4Log/TGo4Log.h"
00023 #include "Go4LockGuard/TGo4LockGuard.h"
00024 #include "Go4Queue/TGo4BufferQueue.h"
00025 #include "Go4CommandsBase/TGo4Command.h"
00026 #include "Go4CommandsBase/TGo4CommandInvoker.h"
00027 #include "TGo4ClientStatus.h"
00028 #include "TGo4TaskHandler.h"
00029 #include "TGo4TaskStatus.h"
00030 #include <Rtypes.h>
00031 
00032 
00033 
00034 TGo4ClientTask::TGo4ClientTask(const char* name,
00035                                const char* serverhost,
00036                                UInt_t negotiationport,
00037                                Bool_t blockingmode,
00038                                Bool_t standalone,
00039                                Bool_t autostart,
00040                                Bool_t autocreate,
00041                                Bool_t ismaster,
00042                                Bool_t autoconnect)
00043    : TGo4Task(name,blockingmode,autostart,autocreate,ismaster),
00044             fxTaskHandler(0),fbAutoConnect(autoconnect),fbServerConnected(kFALSE)
00045 {
00046    Text_t nomen[TGo4ThreadManager::fguTEXTLENGTH];
00047    fxServerHostname=serverhost;
00048    snprintf(nomen, TGo4ThreadManager::fguTEXTLENGTH -1, "TaskHandler of %s", name);
00049    fxTaskHandler=new TGo4TaskHandler(nomen,this,kTRUE, IsMaster(),negotiationport);
00050    fxCommandQ=dynamic_cast<TGo4BufferQueue*> (GetTaskHandler()->GetCommandQueue());
00051    fxStatusQ=dynamic_cast<TGo4BufferQueue*> (GetTaskHandler()->GetStatusQueue());
00052    fxDataQ=dynamic_cast<TGo4BufferQueue*> (GetTaskHandler()->GetDataQueue());
00053    if(standalone)
00054       {
00055          Launch(); // create threads, start application control timer
00056       }
00057    else
00058       {
00059          // subclass must call Launch at end of its ctor
00060       }
00061 }
00062 
00063 TGo4ClientTask::~TGo4ClientTask()
00064 
00065 {
00066    DisconnectServer();
00067    delete fxTaskHandler;
00068 }
00069 
00070 TGo4TaskHandler* TGo4ClientTask::GetTaskHandler()
00071 {
00072    return fxTaskHandler;
00073 }
00074 
00075 Int_t TGo4ClientTask::Initialization()
00076 {
00077    Int_t rev=-1;
00078    if(fbInitDone)
00079       // already initialized, return ok value
00080       {
00081          return 0;
00082       } 
00083    else
00084       {
00085          rev=TGo4Task::Initialization(); // this will launch threads, etc.
00086          if(rev==0)
00087             {
00088                // success: then try to connect (all threads are up now)
00089              if(fbAutoConnect)
00090                {
00091                   if(!ConnectServer(GetServerHostName(),0))
00092                      Terminate();
00093                }
00094             }
00095          else
00096             {
00097                // init failed: tell timer return value
00098                // return rev; // "rev=rev"
00099             }
00100       }// else if(fbInitDone)
00101     return rev;
00102 }
00103 
00104 Bool_t TGo4ClientTask::ConnectServer(const char* node, UInt_t negport, 
00105                             Go4CommandMode_t role, 
00106                             const char* passwd)
00107 {
00108 if(fbServerConnected)
00109    {
00110       TGo4Log::Warn(" ClientTask::ConnectServer ''%s'': ServerTask already connected",
00111                GetName());
00112       return kTRUE;  
00113    }
00114 
00115 if(negport)
00116    fxTaskHandler->SetNegotiationPort(negport); // null negport will use the default port
00117 if(IsMaster())
00118    fxTaskHandler->ClearQueues(); // do not clear queues on slave side,
00119                                  // because analysis status might be already in it!
00120 
00121 //cout <<"TGo4ClientTask::ConnectServer with role "<<role<<" and passwd "<<passwd << endl;
00122 if(passwd)
00123    {
00124       fxTaskHandler->SetRole(role);
00125       if(role==kGo4ComModeObserver)
00126          {
00127             TGo4TaskHandler::SetObservAccount(0,passwd);  
00128          }  
00129       else if (role==kGo4ComModeController)
00130          {
00131             TGo4TaskHandler::SetCtrlAccount(0,passwd);    
00132          }
00133       else if (role==kGo4ComModeAdministrator)
00134          {
00135             TGo4TaskHandler::SetAdminAccount(0,passwd);    
00136          }    
00137       
00138    }
00139 if(fxTaskHandler->Connect(node,0))
00140    {
00141 //      TGo4Log::Info(" ClientTask ''%s'': connected successfully to ServerTask at node %s (port %d) ",
00142 //               GetName(),node, negport);
00143       fbServerConnected=kTRUE;
00144       fxWorkHandler->StartAll(); // all transports are there, then start waiting threads
00145       SendStatusMessage(1,kTRUE,"Client %s connected successfully to Server task at node %s",
00146          GetName(),node); // note that negport does not contain actual portnumber here
00147       return kTRUE;
00148     }
00149 else
00150    {
00151       TGo4Log::Error(" ClientTask ''%s'': FAILED connection to ServerTask at node %s",
00152                GetName(),node);
00153       return kFALSE;
00154    } 
00155 
00156    
00157 }
00158 
00159 Bool_t TGo4ClientTask::DisconnectServer(Bool_t isterminating)
00160 {
00161 Bool_t rev=kTRUE;
00162 if(fbServerConnected)
00163    {
00164      StopWorkThreads();
00165      if(IsMaster())
00166       {
00167 //         fxTaskHandler->ClearQueues(); // prevent monitor commands to interfere with disconnect
00168            TGo4Queue* cq=GetCommandQueue();
00169            if(cq) cq->Clear(); // only clear command queue on master side,
00170                                // otherwise we lose status messages from server 
00171       }
00172       WakeCommandQueue(TGo4Task::fgiTERMID); // will stop local command thread, and remote 
00173       SendStopBuffers();
00174       rev=fxTaskHandler->DisConnect();  
00175       if(rev) fbServerConnected=kFALSE;
00176       if(!isterminating) StartWorkThreads();
00177    }
00178 return rev;
00179 }
00180 
00181 void TGo4ClientTask::Quit()
00182 {
00183    TGo4Log::Debug(" ClientTask''%s'' is quitting... ",GetName());
00184    SendStatusMessage(2,kTRUE,"ClientTask %s  is terminating...",GetName());
00185    DisconnectServer(kTRUE);
00186    Terminate();
00187 }
00188 
00189 TGo4TaskStatus* TGo4ClientTask::CreateStatus()
00190 {
00191    TGo4ClientStatus* stat= new TGo4ClientStatus((Text_t*) GetName());
00192    UpdateStatus(stat); // set the internals
00193    return stat;
00194 }
00195 
00196 
00197 void TGo4ClientTask::UpdateStatus(TGo4TaskStatus* state)
00198 {
00199    TGo4Task::UpdateStatus(state);
00200    TGo4ClientStatus* clstate=dynamic_cast<TGo4ClientStatus*>(state);
00201    if(clstate)
00202       clstate->SetNames(GetServerHostName());
00203 }
00204 
00205 void TGo4ClientTask::AddLocalCommand(TGo4Command * com)
00206 {
00207 if(com==0) return;
00208 if(fxCommandQ)
00209    {
00210       fxCommandQ->AddBufferFromObject(com);
00211    }
00212 else
00213    {
00214       TGo4Log::Debug(" !!! ClientTask - ERROR adding local command - no command queue !!! ");
00215    }
00216 }
00217 
00218 TGo4BufferQueue* TGo4ClientTask::GetCommandQueue(const char*)
00219 {
00220    return fxCommandQ;
00221 }
00222 
00223 TGo4BufferQueue * TGo4ClientTask::GetStatusQueue(const char*)
00224 {
00225   return fxStatusQ;
00226 }
00227 TGo4BufferQueue * TGo4ClientTask::GetDataQueue(const char*)
00228 {
00229    return fxDataQ;   
00230 }
00231 
00232 ClassImp(TGo4ClientTask)
00233 
00234 
00235 //----------------------------END OF GO4 SOURCE FILE ---------------------

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