GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4ClientTask.cxx
Go to the documentation of this file.
1 // $Id: TGo4ClientTask.cxx 999 2013-07-25 11:58:59Z 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 für 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 #include "TGo4LockGuard.h"
18 
19 #include "TGo4BufferQueue.h"
20 #include "TGo4Command.h"
21 #include "TGo4ClientStatus.h"
22 #include "TGo4ThreadHandler.h"
23 #include "TGo4TaskHandler.h"
24 
26  const char* serverhost,
27  UInt_t negotiationport,
28  Bool_t blockingmode,
29  Bool_t standalone,
30  Bool_t autostart,
31  Bool_t autocreate,
32  Bool_t ismaster,
33  Bool_t autoconnect) :
34  TGo4Task(name,blockingmode,autostart,autocreate,ismaster),
35  fxTaskHandler(0),
36  fbAutoConnect(autoconnect),
37  fbServerConnected(kFALSE)
38 {
39  fxServerHostname=serverhost;
40  TString nomen("TaskHandler of "); nomen+=name;
41  fxTaskHandler=new TGo4TaskHandler(nomen.Data(),this,kTRUE, IsMaster(),negotiationport);
44  fxDataQ=dynamic_cast<TGo4BufferQueue*> (GetTaskHandler()->GetDataQueue());
45  if(standalone) {
46  Launch(); // create threads, start application control timer
47  } else {
48  // subclass must call Launch at end of its ctor
49  }
50 }
51 
53 {
55  delete fxTaskHandler;
56 }
57 
59 {
60  return fxTaskHandler;
61 }
62 
64 {
65  Int_t rev=-1;
66  if(fbInitDone)
67  // already initialized, return ok value
68  {
69  return 0;
70  }
71  else
72  {
73  rev=TGo4Task::Initialization(); // this will launch threads, etc.
74  if(rev==0)
75  {
76  // success: then try to connect (all threads are up now)
77  if(fbAutoConnect)
78  {
80  Terminate();
81  }
82  }
83  else
84  {
85  // init failed: tell timer return value
86  // return rev; // "rev=rev"
87  }
88  }// else if(fbInitDone)
89  return rev;
90 }
91 
92 Bool_t TGo4ClientTask::ConnectServer(const char* node, UInt_t negport,
93  Go4CommandMode_t role,
94  const char* passwd)
95 {
97  {
98  TGo4Log::Warn(" ClientTask::ConnectServer ''%s'': ServerTask already connected",
99  GetName());
100  return kTRUE;
101  }
102 
103 if(negport)
104  fxTaskHandler->SetNegotiationPort(negport); // null negport will use the default port
105 if(IsMaster())
106  fxTaskHandler->ClearQueues(); // do not clear queues on slave side,
107  // because analysis status might be already in it!
108 
109 //std::cout <<"TGo4ClientTask::ConnectServer with role "<<role<<" and passwd "<<passwd << std::endl;
110 if(passwd)
111  {
112  fxTaskHandler->SetRole(role);
113  if(role==kGo4ComModeObserver)
114  {
116  }
117  else if (role==kGo4ComModeController)
118  {
120  }
121  else if (role==kGo4ComModeAdministrator)
122  {
124  }
125 
126  }
127 if(fxTaskHandler->Connect(node,0))
128  {
129 // TGo4Log::Info(" ClientTask ''%s'': connected successfully to ServerTask at node %s (port %d) ",
130 // GetName(),node, negport);
131  fbServerConnected=kTRUE;
132  fxWorkHandler->StartAll(); // all transports are there, then start waiting threads
133  SendStatusMessage(1,kTRUE,"Client %s connected successfully to Server task at node %s",
134  GetName(),node); // note that negport does not contain actual portnumber here
135  return kTRUE;
136  }
137 else
138  {
139  TGo4Log::Error(" ClientTask ''%s'': FAILED connection to ServerTask at node %s",
140  GetName(),node);
141  return kFALSE;
142  }
143 
144 
145 }
146 
147 Bool_t TGo4ClientTask::DisconnectServer(Bool_t isterminating)
148 {
149 Bool_t rev=kTRUE;
151  {
152  StopWorkThreads();
153  if(IsMaster())
154  {
155 // fxTaskHandler->ClearQueues(); // prevent monitor commands to interfere with disconnect
157  if(cq) cq->Clear(); // only clear command queue on master side,
158  // otherwise we lose status messages from server
159  }
161  WakeCommandQueue(TGo4Task::Get_fgiTERMID()); // will stop local command thread, and remote
162  SendStopBuffers(); // note: this should only be done after disconnect login was succesfull JA
163  rev=fxTaskHandler->DisConnect();
165  if(rev) fbServerConnected=kFALSE;
166  if(!isterminating) StartWorkThreads();
167  }
168 return rev;
169 }
170 
172 {
173  TGo4Log::Debug(" ClientTask''%s'' is quitting... ",GetName());
174  SendStatusMessage(2,kTRUE,"ClientTask %s is terminating...",GetName());
175  TGo4Task::Quit();
176  DisconnectServer(kTRUE);
177 
178  Terminate(!IsMaster()); // never terminate master process
179 }
180 
182 {
183  TGo4ClientStatus* stat= new TGo4ClientStatus(GetName());
184  UpdateStatus(stat); // set the internals
185  return stat;
186 }
187 
188 
190 {
191  TGo4Task::UpdateStatus(state);
192  TGo4ClientStatus* clstate=dynamic_cast<TGo4ClientStatus*>(state);
193  if(clstate)
194  clstate->SetNames(GetServerHostName());
195 }
196 
198 {
199 if(com==0) return;
200 if(fxCommandQ)
201  {
203  }
204 else
205  {
206  TGo4Log::Debug(" !!! ClientTask - ERROR adding local command - no command queue !!! ");
207  }
208 }
209 
211 {
212  return fxCommandQ;
213 }
214 
216 {
217  return fxStatusQ;
218 }
220 {
221  return fxDataQ;
222 }
virtual void UpdateStatus(TGo4TaskStatus *state)
Definition: TGo4Task.cxx:424
Bool_t DisConnect(Bool_t waitforclient=kTRUE)
virtual Int_t StartWorkThreads()
Definition: TGo4Task.cxx:589
TGo4BufferQueue * fxStatusQ
void WakeCommandQueue(Int_t id=0)
Definition: TGo4Task.cxx:560
Bool_t DisconnectServer(Bool_t isterminating=kFALSE)
virtual Int_t Initialization()
Definition: TGo4Task.cxx:393
Go4CommandMode_t
Definition: TGo4Command.h:28
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:296
static void SetCtrlAccount(const char *name, const char *passwd)
TGo4BufferQueue * fxCommandQ
void SendStopBuffers(const char *taskname=0)
Definition: TGo4Task.cxx:601
TGo4TaskHandler * fxTaskHandler
Bool_t fbServerConnected
TGo4BufferQueue * GetDataQueue(const char *task=0)
TGo4Queue * GetCommandQueue() const
virtual void Quit()
virtual Int_t StopWorkThreads()
Definition: TGo4Task.cxx:595
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:303
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:130
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:115
void AddLocalCommand(TGo4Command *com)
static Int_t Get_fgiTERMID()
Definition: TGo4Task.cxx:635
TString fxServerHostname
static void Error(const char *text,...)
Definition: TGo4Log.cxx:309
void AddBufferFromObject(TObject *object)
virtual void Clear(Option_t *opt="")
Definition: TGo4Queue.cxx:45
Bool_t Connect(const char *host="localhost", TGo4Socket *negotiator=0)
static void Debug(const char *text,...)
Definition: TGo4Log.cxx:270
Bool_t IsMaster()
Definition: TGo4Task.h:94
virtual void UpdateStatus(TGo4TaskStatus *state)