GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TGo4ClientTask.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 "TGo4ClientTask.h"
15 
16 #include "TGo4Log.h"
17 
18 #include "TGo4BufferQueue.h"
19 #include "TGo4ClientStatus.h"
20 #include "TGo4ThreadHandler.h"
21 #include "TGo4TaskHandler.h"
22 
24  const char *serverhost,
25  UInt_t negotiationport,
26  Bool_t blockingmode,
27  Bool_t standalone,
28  Bool_t autostart,
29  Bool_t autocreate,
30  Bool_t ismaster,
31  Bool_t autoconnect) :
32  TGo4Task(name,blockingmode,autostart,autocreate,ismaster),
33  fxTaskHandler(nullptr),
34  fbAutoConnect(autoconnect),
35  fbServerConnected(kFALSE)
36 {
37  fxServerHostname = serverhost;
38  TString nomen("TaskHandler of ");
39  nomen += name;
40  fxTaskHandler = new TGo4TaskHandler(nomen.Data(), this, kTRUE, IsMaster(), negotiationport);
43  fxDataQ = dynamic_cast<TGo4BufferQueue *>(GetTaskHandler()->GetDataQueue());
44  if (standalone) {
45  Launch(); // create threads, start application control timer
46  } else {
47  // subclass must call Launch at end of its ctor
48  }
49 }
50 
52 {
54  delete fxTaskHandler;
55 }
56 
58 {
59  return fxTaskHandler;
60 }
61 
63 {
64  // already initialized, return ok value
65  if (fbInitDone)
66  return 0;
67 
68  Int_t rev = TGo4Task::Initialization(); // this will launch threads, etc.
69  if (rev == 0) {
70  // success: then try to connect (all threads are up now)
71  if (fbAutoConnect) {
73  Terminate();
74  }
75  }
76  return rev;
77 }
78 
79 Bool_t TGo4ClientTask::ConnectServer(const char *node, UInt_t negport,
80  Go4CommandMode_t role,
81  const char *passwd)
82 {
83  if (fbServerConnected) {
84  TGo4Log::Warn(" ClientTask::ConnectServer ''%s'': ServerTask already connected", GetName());
85  return kTRUE;
86  }
87 
88  if (negport)
89  fxTaskHandler->SetNegotiationPort(negport); // null negport will use the default port
90  if (IsMaster())
91  fxTaskHandler->ClearQueues(); // do not clear queues on slave side,
92  // because analysis status might be already in it!
93 
94  TGo4Log::Info("TGo4ClientTask::ConnectServer with role %d and passwd %s", role, passwd);
95  if (passwd) {
96  fxTaskHandler->SetRole(role);
97  if (role == kGo4ComModeObserver) {
98  TGo4TaskHandler::SetObservAccount(nullptr, passwd);
99  } else if (role == kGo4ComModeController) {
100  TGo4TaskHandler::SetCtrlAccount(nullptr, passwd);
101  } else if (role == kGo4ComModeAdministrator) {
102  TGo4TaskHandler::SetAdminAccount(nullptr, passwd);
103  }
104  }
105  if (fxTaskHandler->Connect(node, nullptr)) {
106  // TGo4Log::Info(" ClientTask ''%s'': connected successfully to ServerTask at node %s (port %d) ",
107  // GetName(),node, negport);
108  fbServerConnected = kTRUE;
109  fxWorkHandler->StartAll(); // all transports are there, then start waiting threads
110  SendStatusMessage(1, kTRUE, "Client %s connected successfully to Server task at node %s", GetName(),
111  node); // note that negport does not contain actual portnumber here
112  return kTRUE;
113  } else {
114  TGo4Log::Error(" ClientTask ''%s'': FAILED connection to ServerTask at node %s", GetName(), node);
115  return kFALSE;
116  }
117 }
118 
119 Bool_t TGo4ClientTask::DisconnectServer(Bool_t isterminating)
120 {
121  Bool_t rev = kTRUE;
122  if (fbServerConnected) {
123  StopWorkThreads();
124  if (IsMaster()) {
125  // fxTaskHandler->ClearQueues(); // prevent monitor commands to interfere with disconnect
126  TGo4Queue *cq = GetCommandQueue();
127  if (cq)
128  cq->Clear(); // only clear command queue on master side,
129  // otherwise we lose status messages from server
130  }
132  WakeCommandQueue(TGo4Task::Get_fgiTERMID()); // will stop local command thread, and remote
133  SendStopBuffers(); // note: this should only be done after disconnect login was successful JA
134  rev = fxTaskHandler->DisConnect();
136  if (rev)
137  fbServerConnected = kFALSE;
138  if (!isterminating)
140  }
141  return rev;
142 }
143 
145 {
146  TGo4Log::Debug(" ClientTask''%s'' is quitting... ",GetName());
147  SendStatusMessage(2,kTRUE,"ClientTask %s is terminating...",GetName());
148  TGo4Task::Quit();
149  DisconnectServer(kTRUE);
150 
151  Terminate(!IsMaster()); // never terminate master process
152 }
153 
155 {
156  TGo4ClientStatus* stat= new TGo4ClientStatus(GetName());
157  UpdateStatus(stat); // set the internals
158  return stat;
159 }
160 
162 {
163  TGo4Task::UpdateStatus(state);
164  TGo4ClientStatus* clstate=dynamic_cast<TGo4ClientStatus*>(state);
165  if(clstate)
166  clstate->SetNames(GetServerHostName());
167 }
168 
170 {
171  if (!com)
172  return;
173  if (fxCommandQ) {
175  } else {
176  TGo4Log::Debug(" !!! ClientTask - ERROR adding local command - no command queue !!! ");
177  }
178 }
179 
181 {
182  return fxCommandQ;
183 }
184 
186 {
187  return fxStatusQ;
188 }
190 {
191  return fxDataQ;
192 }
virtual void UpdateStatus(TGo4TaskStatus *state)
Definition: TGo4Task.cxx:393
Bool_t DisConnect(Bool_t waitforclient=kTRUE)
void UpdateStatus(TGo4TaskStatus *state) override
virtual Int_t StartWorkThreads()
Definition: TGo4Task.cxx:548
Int_t Initialization() override
TGo4BufferQueue * GetCommandQueue(const char *task=nullptr) override
Bool_t Connect(const char *host="localhost", TGo4Socket *negotiator=nullptr)
TGo4BufferQueue * fxStatusQ
void WakeCommandQueue(Int_t id=0)
Definition: TGo4Task.cxx:522
TGo4BufferQueue * GetDataQueue(const char *task=nullptr) override
Bool_t DisconnectServer(Bool_t isterminating=kFALSE)
Go4CommandMode_t
Definition: TGo4Command.h:27
TGo4ThreadHandler * fxWorkHandler
static void SetObservAccount(const char *name, const char *passwd)
static void Info(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:294
TGo4ClientTask(const char *name, const char *serverhost="localhost", UInt_t negotiationport=0, Bool_t blockingmode=kTRUE, Bool_t standalone=kFALSE, Bool_t autostart=kFALSE, Bool_t autocreate=kTRUE, Bool_t ismaster=kFALSE, Bool_t autoconnect=kTRUE)
const char * GetServerHostName() const
void SendStopBuffers(const char *taskname=nullptr)
Definition: TGo4Task.cxx:560
virtual ~TGo4ClientTask()
TGo4TaskHandler * GetTaskHandler() override
void Terminate(Bool_t termapp=kTRUE) override
Definition: TGo4Task.cxx:130
static void SetCtrlAccount(const char *name, const char *passwd)
TGo4BufferQueue * fxCommandQ
void Quit() override
TGo4TaskHandler * fxTaskHandler
Bool_t fbServerConnected
Bool_t ConnectServer(const char *node, UInt_t negport, Go4CommandMode_t role=kGo4ComModeRefused, const char *passwd=nullptr)
Int_t Initialization() override
Definition: TGo4Task.cxx:362
virtual Int_t StopWorkThreads()
Definition: TGo4Task.cxx:554
static void Debug(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:281
TGo4BufferQueue * fxDataQ
void Clear(Option_t *opt="") override
Definition: TGo4Queue.cxx:43
void SetNames(const char *serverhost)
TGo4Queue * GetStatusQueue() const
void SetNegotiationPort(UInt_t port)
void SendStatusMessage(Int_t level, Bool_t printout, const char *text,...)
Definition: TGo4Task.cxx:272
static void Error(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:320
TGo4Queue * GetCommandQueue() const
void SetRole(Go4CommandMode_t role)
Bool_t IsMaster() const
Definition: TGo4Task.h:93
static void SetAdminAccount(const char *name, const char *passwd)
virtual void Quit()
Definition: TGo4Task.cxx:115
void AddLocalCommand(TGo4Command *com)
static Int_t Get_fgiTERMID()
Definition: TGo4Task.cxx:588
TGo4Queue * GetDataQueue() const
TString fxServerHostname
TGo4BufferQueue * GetStatusQueue(const char *task=nullptr) override
TGo4TaskStatus * CreateStatus() override
static void Warn(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:307
void AddBufferFromObject(TObject *object)