DABC (Data Acquisition Backbone Core)  2.9.9
ConnectionManager.h
Go to the documentation of this file.
1 // $Id: ConnectionManager.h 4468 2020-04-15 12:47:38Z 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_ConnectionManager
17 #define DABC_ConnectionManager
18 
19 #ifndef DABC_ModuleAsync
20 #include "dabc/ModuleAsync.h"
21 #endif
22 
23 #ifndef DABC_ReferencesVector
24 #include "dabc/ReferencesVector.h"
25 #endif
26 
27 namespace dabc {
28 
36 
38 
39  // these states are representing connection state during connection establishing
40 
42 
44 
45  std::string GetServerId() const { GET_PAR_FIELD(fServerId,"") }
46  void SetServerId(const std::string &id) { SET_PAR_FIELD(fServerId, id) }
47 
48  std::string GetClientId() const { GET_PAR_FIELD(fClientId,"") }
49  void SetClientId(const std::string &id) { SET_PAR_FIELD(fClientId, id) }
50 
51  int GetInlineDataSize() const { GET_PAR_FIELD(fInlineDataSize,0) }
52  void SetInlineDataSize(int size) { SET_PAR_FIELD(fInlineDataSize, size) }
53 
54  Reference TakeCustomData() { GET_PAR_FIELD(fCustomData,0) }
55  void SetCustomData(Reference ref) { SET_PAR_FIELD(fCustomData, ref) }
56 
57  std::string GetConnId() const { GET_PAR_FIELD(fConnId,"") }
58  void SetConnId(const std::string &id) { SET_PAR_FIELD(fConnId, id) }
59 
60  int progress() const { GET_PAR_FIELD(fProgress,0) }
61  void SetProgress(int pr) { SET_PAR_FIELD(fProgress, pr) }
62 
63  void SetRemoteCommand(dabc::Command cmd) { SET_PAR_FIELD(fRemoteCmd, cmd) }
64  void ReplyRemoteCommand(bool res)
65  {
66  Command cmd;
67  if (GetObject()) {
68  LockGuard lock(GetObject()->ObjectMutex());
69  cmd = GetObject()->fRemoteCmd;
70  GetObject()->fRemoteCmd.Release();
71  }
72  cmd.ReplyBool(res);
73  }
74 
75  bool match(const std::string localurl, const std::string remoteurl)
76  {
77  return (localurl == GetLocalUrl()) && (remoteurl == GetRemoteUrl());
78  }
79 
80  std::string GetConnInfo()
81  {
82  return IsServerSide() ? (GetLocalUrl() + " --> " + GetRemoteUrl()) : (GetRemoteUrl() + " --> " + GetLocalUrl());
83  }
84 
86  {
87  SetProgress(0);
88  SetConnTimeout(10.);
89 
90  // FIXME: should we protect fields here with mutex when clear ???
91  SetServerId("");
92  SetClientId("");
94  SetConnId("");
95 
96  ReplyRemoteCommand(false);
97 
98  if (GetObject())
99  GetObject()->SetDelay(0, true);
100  }
101  };
102 
104 
105  DABC_COMMAND(CmdConnectionManagerHandle, "CmdConnectionManager")
106 
107  static const char* ReqArg() { return "#REQ"; }
108 
110 
111  std::string GetReq() const { return GetStr(ReqArg()); }
112  };
113 
114 
115  class CmdGlobalConnect : public Command {
116 
117  DABC_COMMAND(CmdGlobalConnect, "CmdGlobalConnect")
118 
119  void SetUrl1(const std::string &url1) { SetStr("Url1", url1); }
120  void SetUrl2(const std::string &url2) { SetStr("Url2", url2); }
121 
122  std::string GetUrl1() const { return GetStr("Url1"); }
123  std::string GetUrl2() const { return GetStr("Url2"); }
124  };
125 
126  // ____________________________________________________________________________________
127 
138  protected:
140 
144 
147 
150 
154 
160  void CheckConnectionRecs(bool finish_command = false);
161 
163  virtual void ModuleCleanup();
164 
166  virtual void ProcessParameterEvent(const ParameterEvent& evnt);
167 
168  virtual int ExecuteCommand(Command cmd);
169  virtual bool ReplyCommand(Command cmd);
170 
172  virtual double ProcessTimeout(double last_diff);
173 
174  public:
175 
183  progrFailed
184  };
185 
186  ConnectionManager(const std::string &name, Command cmd = nullptr);
187  virtual ~ConnectionManager();
188 
189  ConnectionRequestFull FindConnection(const std::string &url1, const std::string &url2);
190  };
191 
192 }
193 
194 
195 #endif
#define DABC_COMMAND(cmd_class, cmd_name)
Definition: Command.h:282
#define SET_PAR_FIELD(field_name, value)
#define GET_PAR_FIELD(field_name, defvalue)
#define DABC_REFERENCE(RefClass, ParentClass, T)
Definition: Reference.h:222
CmdConnectionManagerHandle(ConnectionRequestFull &req)
Here is description how connection between two nodes are build and which states are used.
std::string GetUrl2() const
void SetUrl2(const std::string &url2)
void SetUrl1(const std::string &url1)
std::string GetUrl1() const
Represents command with its arguments.
Definition: Command.h:99
void ReplyBool(bool res)
Reply on the command with true or false value.
Definition: Command.h:238
bool SetStr(const std::string &name, const char *value)
Definition: Command.h:134
std::string GetStr(const std::string &name, const std::string &dflt="") const
Definition: Command.h:136
Connections manager class.
ConnectionRequestFull FindConnection(const std::string &url1, const std::string &url2)
virtual bool ReplyCommand(Command cmd)
Reimplement this method to react on command reply Return true if command can be destroyed by framewor...
int fConnCounter
counter for issued connections
@ progrDoingConnect
at this state device should drive connection itself and inform about completion or failure
@ progrWaitReply
connection request is send to the remote side and waiting for reply
@ progrFailed
fail state, request will be ignored forever
@ progrConnected
this is normal state when connection is active
@ progrPending
state when record is prepared and can try connect
@ progrDoingInit
state when record should be prepared by device
void HandleCmdGlobalConnectReply(CmdGlobalConnect cmd)
React on the reply of global connect command.
virtual double ProcessTimeout(double last_diff)
Check status of connections establishing.
void CheckConnectionRecs(bool finish_command=false)
Check current situation with connections.
int fDoingConnection
flag indicates that about connection activity: 0 - nothing, -1 - closing, 1 - establishing
virtual int ExecuteCommand(Command cmd)
Main method where commands are executed.
Command fConnCmd
actual global command for connections establishing/closing
virtual void ProcessParameterEvent(const ParameterEvent &evnt)
Process changes in connection recs.
bool FillAnswerOnRemoteConnectCmd(Command cmd, ConnectionRequestFull &req)
Fill answer on remote connection request and invoke device to start connection When device confirms t...
ConnectionManager(const std::string &name, Command cmd=nullptr)
virtual void ModuleCleanup()
Destroy all connections, if necessary - request to cleanup custom data by device.
void HandleConnectRequestCmdReply(CmdConnectionManagerHandle cmd)
Analyze reply of the command, send to the device.
Container for connection parameters.
Full description of connection request.
std::string GetConnId() const
void SetCustomData(Reference ref)
void SetRemoteCommand(dabc::Command cmd)
bool match(const std::string localurl, const std::string remoteurl)
std::string GetServerId() const
void SetConnTimeoutDirectly(double tm)
std::string GetClientId() const
void SetClientId(const std::string &id)
void SetServerId(const std::string &id)
void SetConnId(const std::string &id)
Connection request.
void SetUseAckn(bool on=true)
std::string GetLocalUrl() const
void SetAllowedField(const std::string &name="")
std::string GetRemoteUrl() const
Return url of data source to which connection should be established.
void SetConnTimeout(double tm)
bool IsServerSide() const
Indicates if local node in connection is server or client.
Lock guard for posix mutex.
Definition: threads.h:127
Base class for user-derived code, implementing event-processing.
Definition: ModuleAsync.h:32
Reference on the arbitrary object
Definition: Reference.h:73
void Release()
Releases reference on the object.
Definition: Reference.cxx:138
Object * GetObject() const
Return pointer on the object.
Definition: Reference.h:129
Mutex * ObjectMutex() const
Definition: Reference.cxx:183
Vector of dabc::Reference objects.
Event manipulation API.
Definition: api.h:23
const char * xmlTimeoutAttr
Definition: ConfigBase.cxx:42
const char * xmlUseacknAttr
Definition: ConfigBase.cxx:39