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

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

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