DABC (Data Acquisition Backbone Core)  2.9.9
SocketCommandChannel.h
Go to the documentation of this file.
1 // $Id: SocketCommandChannel.h 4482 2020-04-15 14:47:18Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef DABC_SocketCommandChannel
17 #define DABC_SocketCommandChannel
18 
19 #ifndef DABC_SocketThread
20 #include "dabc/SocketThread.h"
21 #endif
22 
23 namespace dabc {
24 
25 
30  struct SocketCmdPacket {
31 
32  uint32_t dabc_header;
33 
34  uint32_t data_kind;
35 
36  uint32_t data_timeout;
37 
38  uint32_t data_size;
39 
40  uint32_t data_cmdsize;
41 
42  uint32_t data_rawsize;
43 
45  };
46 
47 
49 
50  // ____________________________________________________________________________________
51 
57  class SocketCommandClient : public Worker {
58  protected:
59 
60  friend class SocketCommandChannel;
61 
62  enum EEVent {
64  };
65 
66  enum {
67  headerDabc = 123707321
68  };
69 
72  kindReply = 2,
73  kindCancel = 3,
74  kindDisconnect = 4
75  };
76 
77 
78  enum EState {
82  stFailure
83  };
84 
85  enum ERecvState {
86  recvInit, // receiver ready to accept new command
87  recvHeader, // command is assigned and executing
88  recvDevConnect, // special case of socket device connect via command channel
89  recvData // command is sends back, one need to release command only when it is replied
90  };
91 
92  std::string fRemoteHostName;
94 
96 
98 
104  char* fRecvBuf;
105  unsigned fRecvBufSize;
106 
109 
111 
112  // these are fields, used to manage information about remote node
114  std::string fRemoteName;
115 
116  // indicate that this is special connection to master node
117  // each time it is reconnected, it will append special command to register itself in remote master
119  std::string fClientNameSufix;
120 
121  virtual void OnThreadAssigned();
122 
123  virtual int ExecuteCommand(Command cmd);
124 
125  virtual bool ReplyCommand(Command cmd);
126 
127  virtual void ProcessEvent(const EventId&);
128 
129  virtual double ProcessTimeout(double last_diff);
130 
131  bool EnsureRecvBuffer(unsigned strsize);
132 
134  void ProcessRecvPacket();
135 
137  void AddCommand(dabc::Command cmd, bool asreply = false);
138 
140  void SendSubmittedCommands();
141 
143  void SendCommand(dabc::Command cmd, bool asreply = false);
144 
146  void CloseClient(bool iserr = false, const char* msg = 0);
147 
148  double ExtraClientTimeout() const { return 1.0; }
149  double ExtraServerTimeout() const { return 0.1; }
150 
152 
153  public:
154  SocketCommandClient(Reference parent, const std::string &name,
155  SocketAddon* addon,
156  const std::string &hostname = "",
157  double reconnect = 0.);
158  virtual ~SocketCommandClient();
159  };
160 
162 
164  };
165 
166  // ____________________________________________________________________________________
167 
168 
174  class SocketCommandChannel : public Worker {
175 
176  friend class SocketCommandClient;
177 
178  protected:
179  int fNodeId;
182  std::string fRedirectDevice;
183 
184  virtual int PreviewCommand(Command cmd);
185  virtual int ExecuteCommand(Command cmd);
186 
188  std::string GetRemoteNode(const std::string &url);
189 
192  SocketCommandClientRef ProvideWorker(const std::string &remnodename, double conn_tmout = -1);
193 
195  virtual double ProcessTimeout(double last_diff);
196 
197  public:
198  SocketCommandChannel(const std::string &name, SocketServerAddon* connaddon, Command cmd);
200 
202  virtual std::string RequiredThrdClass() const { return typeSocketThread; }
203 
204  };
205 
206 
207 }
208 
209 #endif
#define DABC_REFERENCE(RefClass, ParentClass, T)
Definition: Reference.h:222
Reference on memory from memory pool.
Definition: Buffer.h:135
Represents command with its arguments.
Definition: Command.h:99
Queue of commands
Definition: CommandsQueue.h:39
Reference on the arbitrary object
Definition: Reference.h:73
Special addon class for handling of socket and socket events.
Definition: SocketThread.h:52
@ evntSocketLastInfo
last system event, used by sockets
Definition: SocketThread.h:99
Provides command channel to the dabc process.
SocketCommandClientRef ProvideWorker(const std::string &remnodename, double conn_tmout=-1)
Provide client for remote node.
SocketCommandChannel(const std::string &name, SocketServerAddon *connaddon, Command cmd)
bool fClientsAllowed
when true, incomming clients are allowed
virtual std::string RequiredThrdClass() const
As name said, command channel requires socket thread for the work.
virtual int PreviewCommand(Command cmd)
This method called before command will be executed.
std::string fRedirectDevice
name of socket device, which can get redirection
int fClientCnt
counter for new clients
std::string GetRemoteNode(const std::string &url)
Provide string for connection to remote node.
virtual double ProcessTimeout(double last_diff)
timeout used in channel to update node hierarchy, which than can be requested from remote
virtual int ExecuteCommand(Command cmd)
Main method where commands are executed.
Client side of command connection between two nodes.
dabc::Buffer fSendRawData
raw data, which should be send with command
void SendSubmittedCommands()
Send submitted commands to remote.
bool EnsureRecvBuffer(unsigned strsize)
CommandsQueue fSendQueue
queue to keep commands which should be send
CommandsQueue fWaitQueue
commands which should be replied from remote
std::string fRemoteName
name of connection, appeared in the browser
void ProcessRecvPacket()
Method called, when complete packet (header + raw data) is received.
virtual int ExecuteCommand(Command cmd)
Main method where commands are executed.
bool ExecuteCommandByItself(Command cmd)
@ stConnecting
initial state, worker waits until channel is connected
@ stWorking
normal state when execution of different commands are done
@ stFailure
failure is detected and connection will be close
@ stClosing
socket connection going normally down
virtual double ProcessTimeout(double last_diff)
unsigned fRecvBufSize
currently allocated size of recv buffer
dabc::Buffer fSendBuf
content of transported command
EState fState
current state of the worker
void AddCommand(dabc::Command cmd, bool asreply=false)
Submit command to send queue, if queue is empty start sending immediately.
bool fRemoteObserver
if true, channel automatically used to update information from remote
SocketCommandClient(Reference parent, const std::string &name, SocketAddon *addon, const std::string &hostname="", double reconnect=0.)
virtual void ProcessEvent(const EventId &)
SocketCmdPacket fSendHdr
header for send command
@ kindReply
reply (server -> client)
@ kindDisconnect
close of connection (client -> server)
@ kindCommand
command (client -> server)
@ kindCancel
cancel cmd execution (server -> client)
ERecvState fRecvState
state that happens with receiver (server)
CommandsQueue fCmds
commands to be submitted to the remote
void CloseClient(bool iserr=false, const char *msg=0)
Called when connection must be closed due to the error.
virtual bool ReplyCommand(Command cmd)
Reimplement this method to react on command reply Return true if command can be destroyed by framewor...
char * fRecvBuf
raw buffer for receiving command
double fReconnectPeriod
interval how often reconnect will be tried
std::string fClientNameSufix
name suffix, which will be append to name seen on the server side
void SendCommand(dabc::Command cmd, bool asreply=false)
Send next command to the remote.
SocketCmdPacket fRecvHdr
buffer for receiving header
bool fSendingActive
indicate if currently send active
std::string fRemoteHostName
host name and port number
Socket addon for handling connection requests on server side.
Definition: SocketThread.h:285
Reference on dabc::Worker
Definition: Worker.h:466
Active object, which is working inside dabc::Thread.
Definition: Worker.h:116
bool reconnect
Definition: hldprint.cxx:994
Event manipulation API.
Definition: api.h:23
const char * typeSocketThread
Definition: Object.cxx:80
Event structure, exchanged between DABC threads.
Definition: Thread.h:70
Defines syntax of raw packet, transformed on the command channels.
uint32_t data_kind
kind of data inside packet
uint32_t data_timeout
timeout in milliseconds, 0 - not timeout at all
uint32_t data_cmdsize
which part of data is for command in xml format
uint32_t dabc_header
constant, which identifies dabc packet
uint32_t data_size
total length of the information in the packet
uint32_t data_rawsize
which part of data at the end is raw data