GSI Object Oriented Online Offline (Go4)  GO4-6.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4ClientTask.cxx
Go to the documentation of this file.
1 // $Id: TGo4ClientTask.cxx 2757 2020-04-16 11:45:21Z linev $
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(0),
34  fbAutoConnect(autoconnect),
35  fbServerConnected(kFALSE)
36 {
37  fxServerHostname=serverhost;
38  TString nomen("TaskHandler of "); nomen+=name;
39  fxTaskHandler=new TGo4TaskHandler(nomen.Data(),this,kTRUE, IsMaster(),negotiationport);
42  fxDataQ=dynamic_cast<TGo4BufferQueue*> (GetTaskHandler()->GetDataQueue());
43  if(standalone) {
44  Launch(); // create threads, start application control timer
45  } else {
46  // subclass must call Launch at end of its ctor
47  }
48 }
49 
51 {
53  delete fxTaskHandler;
54 }
55 
57 {
58  return fxTaskHandler;
59 }
60 
62 {
63  Int_t rev=-1;
64  if(fbInitDone)
65  // already initialized, return ok value
66  {
67  return 0;
68  }
69  else
70  {
71  rev=TGo4Task::Initialization(); // this will launch threads, etc.
72  if(rev==0)
73  {
74  // success: then try to connect (all threads are up now)
75  if(fbAutoConnect)
76  {
78  Terminate();
79  }
80  }
81  else
82  {
83  // init failed: tell timer return value
84  // return rev; // "rev=rev"
85  }
86  }// else if(fbInitDone)
87  return rev;
88 }
89 
90 Bool_t TGo4ClientTask::ConnectServer(const char* node, UInt_t negport,
91  Go4CommandMode_t role,
92  const char* passwd)
93 {
95  {
96  TGo4Log::Warn(" ClientTask::ConnectServer ''%s'': ServerTask already connected",
97  GetName());
98  return kTRUE;
99  }
100 
101 if(negport)
102  fxTaskHandler->SetNegotiationPort(negport); // null negport will use the default port
103 if(IsMaster())
104  fxTaskHandler->ClearQueues(); // do not clear queues on slave side,
105  // because analysis status might be already in it!
106 
107 //std::cout <<"TGo4ClientTask::ConnectServer with role "<<role<<" and passwd "<<passwd << std::endl;
108 if(passwd)
109  {
110  fxTaskHandler->SetRole(role);
111  if(role==kGo4ComModeObserver)
112  {
114  }
115  else if (role==kGo4ComModeController)
116  {
118  }
119  else if (role==kGo4ComModeAdministrator)
120  {
122  }
123 
124  }
125 if(fxTaskHandler->Connect(node,0))
126  {
127 // TGo4Log::Info(" ClientTask ''%s'': connected successfully to ServerTask at node %s (port %d) ",
128 // GetName(),node, negport);
129  fbServerConnected=kTRUE;
130  fxWorkHandler->StartAll(); // all transports are there, then start waiting threads
131  SendStatusMessage(1,kTRUE,"Client %s connected successfully to Server task at node %s",
132  GetName(),node); // note that negport does not contain actual portnumber here
133  return kTRUE;
134  }
135 else
136  {
137  TGo4Log::Error(" ClientTask ''%s'': FAILED connection to ServerTask at node %s",
138  GetName(),node);
139  return kFALSE;
140  }
141 
142 
143 }
144 
145 Bool_t TGo4ClientTask::DisconnectServer(Bool_t isterminating)
146 {
147 Bool_t rev=kTRUE;
149  {
150  StopWorkThreads();
151  if(IsMaster())
152  {
153 // fxTaskHandler->ClearQueues(); // prevent monitor commands to interfere with disconnect
155  if(cq) cq->Clear(); // only clear command queue on master side,
156  // otherwise we lose status messages from server
157  }
159  WakeCommandQueue(TGo4Task::Get_fgiTERMID()); // will stop local command thread, and remote
160  SendStopBuffers(); // note: this should only be done after disconnect login was successful JA
161  rev=fxTaskHandler->DisConnect();
163  if(rev) fbServerConnected=kFALSE;
164  if(!isterminating) StartWorkThreads();
165  }
166 return rev;
167 }
168 
170 {
171  TGo4Log::Debug(" ClientTask''%s'' is quitting... ",GetName());
172  SendStatusMessage(2,kTRUE,"ClientTask %s is terminating...",GetName());
173  TGo4Task::Quit();
174  DisconnectServer(kTRUE);
175 
176  Terminate(!IsMaster()); // never terminate master process
177 }
178 
180 {
181  TGo4ClientStatus* stat= new TGo4ClientStatus(GetName());
182  UpdateStatus(stat); // set the internals
183  return stat;
184 }
185 
186 
188 {
189  TGo4Task::UpdateStatus(state);
190  TGo4ClientStatus* clstate=dynamic_cast<TGo4ClientStatus*>(state);
191  if(clstate)
192  clstate->SetNames(GetServerHostName());
193 }
194 
196 {
197 if(com==0) return;
198 if(fxCommandQ)
199  {
201  }
202 else
203  {
204  TGo4Log::Debug(" !!! ClientTask - ERROR adding local command - no command queue !!! ");
205  }
206 }
207 
209 {
210  return fxCommandQ;
211 }
212 
214 {
215  return fxStatusQ;
216 }
218 {
219  return fxDataQ;
220 }
virtual void UpdateStatus(TGo4TaskStatus *state)
Definition: TGo4Task.cxx:407
Bool_t DisConnect(Bool_t waitforclient=kTRUE)
virtual Int_t StartWorkThreads()
Definition: TGo4Task.cxx:569
TGo4BufferQueue * fxStatusQ
void WakeCommandQueue(Int_t id=0)
Definition: TGo4Task.cxx:543
Bool_t DisconnectServer(Bool_t isterminating=kFALSE)
virtual Int_t Initialization()
Definition: TGo4Task.cxx:376
Go4CommandMode_t
Definition: TGo4Command.h:27
TGo4ThreadHandler * fxWorkHandler
TGo4BufferQueue * GetStatusQueue(const char *task=0)
static void SetObservAccount(const char *name, const char *passwd)
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)
TGo4Queue * GetStatusQueue() const
virtual TGo4TaskStatus * CreateStatus()
virtual ~TGo4ClientTask()
static void Warn(const char *text,...)
Definition: TGo4Log.cxx:310
static void SetCtrlAccount(const char *name, const char *passwd)
TGo4BufferQueue * fxCommandQ
void SendStopBuffers(const char *taskname=0)
Definition: TGo4Task.cxx:581
TGo4TaskHandler * fxTaskHandler
Bool_t fbServerConnected
TGo4BufferQueue * GetDataQueue(const char *task=0)
TGo4Queue * GetCommandQueue() const
virtual void Quit()
virtual Int_t StopWorkThreads()
Definition: TGo4Task.cxx:575
TGo4BufferQueue * GetCommandQueue(const char *task=0)
TGo4Queue * GetDataQueue() const
TGo4BufferQueue * fxDataQ
virtual Int_t Initialization()
void SetNames(const char *serverhost)
void SetNegotiationPort(UInt_t port)
void SendStatusMessage(Int_t level, Bool_t printout, const char *text,...)
Definition: TGo4Task.cxx:285
Bool_t ConnectServer(const char *node, UInt_t negport, Go4CommandMode_t role=kGo4ComModeRefused, const char *passwd=0)
virtual void Terminate(Bool_t termapp=kTRUE)
Definition: TGo4Task.cxx:131
const char * GetServerHostName()
TGo4TaskHandler * GetTaskHandler()
void SetRole(Go4CommandMode_t role)
static void SetAdminAccount(const char *name, const char *passwd)
virtual void Quit()
Definition: TGo4Task.cxx:116
void AddLocalCommand(TGo4Command *com)
static Int_t Get_fgiTERMID()
Definition: TGo4Task.cxx:611
TString fxServerHostname
static void Error(const char *text,...)
Definition: TGo4Log.cxx:323
void AddBufferFromObject(TObject *object)
virtual void Clear(Option_t *opt="")
Definition: TGo4Queue.cxx:44
Bool_t Connect(const char *host="localhost", TGo4Socket *negotiator=0)
static void Debug(const char *text,...)
Definition: TGo4Log.cxx:284
Bool_t IsMaster()
Definition: TGo4Task.h:93
virtual void UpdateStatus(TGo4TaskStatus *state)