MIRA
RemoteModule.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_REMOTEMODULE_H_
48 #define _MIRA_REMOTEMODULE_H_
49 
50 #include <thread/CyclicRunnable.h>
51 #include <serialization/adapters/std/list>
52 #include <serialization/adapters/boost/optional.hpp>
53 #include <utils/UUID.h>
54 #include <security/RSAKey.h>
55 
56 #include <fw/DiscoverService.h>
58 #include <fw/RemoteConnection.h>
59 #include <fw/RemoteServer.h>
61 #include <fw/ServiceLevel.h>
62 
63 namespace mira {
64 
65 class RemoteServer;
66 
68 
127 {
128 public:
129  typedef boost::shared_ptr<RemoteServer> RemoteServerPtr;
130 
131  typedef std::list<RemoteConnectionProxy> ConnectionList;
132  typedef std::map<UUID, RemoteConnectionProxy> ConnectionMap;
133 
134  typedef std::list<KnownFramework> KnownFrameworkList;
135  typedef std::list<ServiceLevel> ServiceLevelList;
136  typedef std::map<std::string, ServiceLevel> ServiceLevelMap;
137  typedef std::list<TypeServiceLevel> TypeServiceLevelList;
138  typedef std::map<Typename, TypeServiceLevel> TypeServiceLevelMap;
139 
140  friend class RemoteConnection;
143  friend class DiscoverService;
144  friend class RemoteServer;
145 
147  enum AuthMode
148  {
149  AUTH_NONE=0,
150  AUTH_PASSWORD=1,
151  AUTH_KEY=2
152  };
153 
164  {
165  std::string group;
166 
167  template<typename Reflector>
168  void reflect(Reflector& r)
169  {
170  r.member("Group", group,
171  "The workgroup, this framework belongs to", "");
172  r.member("Password", mPassword, setter(&AuthSettings::setPassword, this),
173  "Password that is used for the weak authentication",
175  r.member("Key", mKeyFile, setter(&AuthSettings::setKeyFile, this),
176  "Path to the file containing the private key "
177  "for strong authentication.",
179  }
180 
181  AuthMode getMode() const {
182  if(mKey)
183  return AUTH_KEY;
184  else if(mPassword)
185  return AUTH_PASSWORD;
186  else
187  return AUTH_NONE;
188  }
189 
191  void clearAuth();
192 
197  const std::string& getPassword() const;
198 
200  void setPassword(const boost::optional<std::string>& passwd);
201 
206  const RSAKey& getKey() const;
207 
209  void setKey(const boost::optional<std::string>& str);
210 
212  void setKeyFile(const boost::optional<std::string>& file);
213 
214  private:
215 
216  boost::optional<std::string> mPassword;
217  boost::optional<std::string> mKeyFile;
218  boost::optional<RSAKey> mKey;
219  };
220 
221  RemoteModule();
222 
223  template<typename Reflector>
224  void reflect(Reflector& r)
225  {
226  serialization::VersionType version = r.version(2, this);
227  r.member("Port", mPort,
228  "The port where the framework server runs on. 0 means auto port", 0);
229  r.member("IOThreadCount", mIOThreadCount,
230  "How many threads are used for communication", 1);
231  r.member("CycleTime", mCyclicRunnable.cycleTime,
232  "The cycle time of remote framework thread that observes connections",
233  Duration::seconds(1));
234 
235  r.property("EnablePTPSync", mEnablePTPSync, "Enable/Disable synchronization of clocks via PTP (effective for new connections)", true);
236  r.property("EnablePingTimeout", mEnablePingTimeout, "Enable/Disable ping timeout (effective for new connections)", false);
237  r.member("PingInterval", mPingInterval,
238  "The interval of sending ping messages.", Duration::seconds(1));
239  r.member("PingTimeout", mPingTimeout,
240  "The maximum time before a connection is assumed as dead "
241  "when no ping messages are received.", Duration::seconds(10));
242 
243  r.member("ExitOnDisconnectTimeout", mExitOnDisconnectTimeout,
244  "Exits/Closes the framework if any connection could not be established or is closed for a certain time",
246 
247  r.member("Authentication", mAuthSettings, "Authentication Settings",
248  AuthSettings());
249 
250  if (version >= 2)
251  r.member("KnownFrameworks", mKnownFrameworks,
252  "The list of remote frameworks", KnownFrameworkList());
253  else {
254  // old KnownFramework serialization = delegate to just address
255  std::vector<std::string> tmpFWs;
256  r.member("KnownFrameworks", tmpFWs, "",
257  std::vector<std::string>(), REFLECT_CTRLFLAG_TEMP_TRACKING);
258  mKnownFrameworks.clear();
259  foreach (const std::string& a, tmpFWs) {
260  mKnownFrameworks.emplace_back();
261  mKnownFrameworks.back().address = a;
262  }
263  }
264 
265  r.member("ServiceLevels", mServiceLevels,
266  "The service level agreement per channel", ServiceLevelList());
267  r.member("ServiceLevelsByType", mTypeServiceLevels,
268  "The service level agreement per channel type", TypeServiceLevelList());
269 
270  r.property("FrameworkConnections", mConnections,
271  "The list of active remote framework connections", serialization::IgnoreMissing());
272 
273  r.interface("IRemoteModule");
274  r.method("migrateUnitToThisFramework", &RemoteModule::migrateUnitToThisFramework, this,
275  "Requests a unit from an other framework to migrate into this framework",
276  "id", "id of the unit to migrate", "/navigation/Pilot");
277  r.method("isFrameworkConnected", &RemoteModule::isFrameworkConnected, this,
278  "Returns if a framework with ID is connected to this framework",
279  "frameworkID", "framework ID to check", "Framework_81ab60d5-0e9a-43d6-aced-7e33a3cd875a");
280  r.method("isConnectedTo", &RemoteModule::isConnectedTo, this,
281  "Returns if a framework with address is connected to this framework",
282  "address", "address to check", "127.0.0.1:1234");
283  r.method("disconnectFramework", &RemoteModule::disconnectFramework, this,
284  "Disconnects a framework with ID (if connected)",
285  "frameworkID", "framework ID to disconnect", "Framework_81ab60d5-0e9a-43d6-aced-7e33a3cd875a",
286  "autoReconnect", "if true, keep as known framework (thus try reconnecting)", false);
287  r.method("disconnectFrom", &RemoteModule::disconnectFrom, this,
288  "Disconnects from a framework with address",
289  "address", "address to disconnect", "127.0.0.1:1234",
290  "autoReconnect", "if true, keep as known framework (thus try reconnecting)", false);
291  r.method("connectTo",
292  (void(RemoteModule::*)(const std::string&))(&RemoteModule::addKnownFramework), this,
293  "Add an address to the list of known remote frameworks and try to connect",
294  "address", "Address of the remote framework as Host:Port", "127.0.0.1:1234");
295  r.method("connectTo",
296  (void(RemoteModule::*)(const std::string&, bool, bool, bool, const Duration&))(&RemoteModule::addKnownFramework), this,
297  "Add an address to the list of known remote frameworks and try to connect",
298  "address", "Address of the remote framework as Host:Port", "127.0.0.1:1234",
299  "forcePTP", "Force PTP time sync", false,
300  "legacyBinaryFormat", "Use legacy binary serializer to encode data (connect to legacy framework)", false,
301  "monitorOnly", "Do not publish local channels, services and authorities to remote", false,
302  "delayConnect", "Wait for this duration before trying to connect", Duration::seconds(1));
303  }
304 
309  static uint32 getCurrentVersion();
310 
316  void setPort(uint16 port) { mPort = port; }
317 
321  uint16 getPort() const;
322 
323 
327  void enablePTPSync(bool enable=true) { mEnablePTPSync = enable; }
328 
332  bool isPTPSyncEnabled() const { return mEnablePTPSync; }
333 
337  void enablePingTimeout(bool enable=true) { mEnablePingTimeout = enable; }
338 
342  bool isPingTimeoutEnabled() const { return mEnablePingTimeout; }
343 
347  Duration getPingInterval() const { return mPingInterval; }
348 
352  Duration getPingTimeout() const { return mPingTimeout; }
353 
359  void setAuthGroup(const std::string& group);
360 
364  void setNoAuth();
365 
370  void setAuthPassword(const std::string& password);
371 
376  void setAuthKey(const std::string& key);
377 
382  void setAuthKeyFile(const std::string& keyfile);
383 
387  const AuthSettings& getAuthSettings() const;
388 
394  void setExitOnDisconnectTimeout(Duration timeout) { mExitOnDisconnectTimeout = timeout; }
395 
399  const UUID& getID() const { return mID; }
400 
404  ServiceLevel getServiceLevel(const std::string& channelID, const Typename& channelType);
405 
409  void start();
410 
414  void stop();
415 
416 
421 
428  void addKnownFramework(const std::string& address);
429 
443  void addKnownFramework(const std::string& address,
444  bool forcePTP,
445  bool legacyBinaryFormat,
446  bool monitorOnly,
447  const Duration& delayConnect = Duration::seconds(0));
448 
454  void disconnectFramework(const std::string& frameworkID,
455  bool autoReconnect = false);
456 
464  void disconnectFrom(const std::string& address,
465  bool autoReconnect = false);
466 
470  bool isFrameworkConnected(const std::string& frameworkID) const;
471 
476  bool isConnectedTo(const std::string& address) const;
477 
487  void migrateUnitToThisFramework(const std::string& id);
488 
494 
498  void publishAuthority(const AuthorityDescription& authority);
499 
503  void unpublishAuthority(const AuthorityDescription& authority);
504 
508  void publishService(const std::string& service);
509 
513  void unpublishService(const std::string& service);
514 
522  void publishChannel(const std::string& channelID, const Typename& type);
523 
530  void unpublishChannel(const std::string& channelID);
531 
539  void subscribeChannel(const std::string& channelID);
540 
546  void unsubscribeChannel(const std::string& channelID);
547 
549 
551  void updateIncomingStats(std::size_t size);
553  void updateOutgoingStats(std::size_t size);
554 
555  std::size_t getIncomingBytesPerSecond() const;
556  std::size_t getOutgoingBytesPerSecond() const;
557 
558 protected:
559 
560  RemoteConnectionProxy createIncomingConnection();
561 
566  RemoteConnectionProxy& addPendingIncomingConnection(RemoteConnectionProxy&& proxy,
567  bool start = true);
568 
573  void removePendingIncomingConnection(RemoteConnection* connection);
574 
579  RemoteConnectionProxy& addPendingOutgoingConnection(RemoteConnectionProxy&& proxy,
580  bool start = true);
581 
586  void removePendingOutgoingConnection(RemoteConnection* connection);
587 
591  void onRemoteFrameworkDiscovered(const std::string& host, uint16 port, UUID id);
592 
598  void storeConnection(RemoteConnectionProxy iConnection);
599 
605  void eraseConnection(ConnectionMap::iterator it);
606 
612  bool onIncomingConnected(RemoteIncomingConnection* iConnection);
613 
618  void onIncomingDisconnected(RemoteIncomingConnection* iConnection);
619 
625  bool onOutgoingConnected(RemoteOutgoingConnectionBase* iConnection);
626 
631  void onOutgoingDisconnected(RemoteOutgoingConnectionBase* iConnection);
632 
638  void process();
639 
640  void startDisconnectTimeout();
641  void stopDisconnectTimeout();
642  bool checkDisconnectTimeout();
643 
644 protected:
645 
650 
653  boost::thread mThread;
654  uint16 mPort;
662 
666 
668 
677  mutable boost::recursive_mutex mConnectionMutex;
678 };
679 
681 
683 typedef boost::shared_ptr<RemoteModule> RemoteModulePtr;
684 
686 
687 template <>
688 class IsCopyAssignable<RemoteModule::ConnectionMap> : public std::false_type {};
689 
691 
692 }
693 
694 #endif
Service that discovers other frameworks within a network using multicast.
This object can use object tracking internally, but the object tracking system&#39;s state remains unchan...
Definition: ReflectControlFlags.h:82
std::list< ServiceLevel > ServiceLevelList
Definition: RemoteModule.h:135
QoS management informations.
void addKnownFramework(const std::string &address)
Adds the address of a framework to the list of known remote frameworks.
PerformanceStatistics mIncomingStats
Definition: RemoteModule.h:646
AuthMode getMode() const
Definition: RemoteModule.h:181
static Duration invalid()
Returns an invalid duration.
Definition: Time.h:247
ConnectionList mPendingOutgoingConnections
Definition: RemoteModule.h:663
void setExitOnDisconnectTimeout(Duration timeout)
Sets the timeout to exit on disconnect, closes the framework if a connection is closed or can not be ...
Definition: RemoteModule.h:394
Connection classes representing connections between frameworks.
PerformanceStatistics mOutgoingStats
Definition: RemoteModule.h:647
Type trait that evaluates to true if a type is copy assignable, false otherwise.
Definition: IsCopyAssignable.h:71
boost::shared_ptr< RemoteModule > RemoteModulePtr
Typedef for a pointer to RemoteModule.
Definition: RemoteModule.h:683
Duration mExitOnDisconnectTimeout
Definition: RemoteModule.h:670
const UUID & getID() const
Returns the unique ID of the remote framework.
Definition: RemoteModule.h:399
ServiceLevel by channel name.
Definition: ServiceLevel.h:102
Manages connections to other remote frameworks.
Definition: RemoteModule.h:126
uint32 mOutgoingData
Definition: RemoteModule.h:649
uint16 mIOThreadCount
Definition: RemoteModule.h:659
Definition of a RSA key (public or private)
Definition: RSAKey.h:71
Connection class for incoming connections.
Definition: RemoteConnection.h:853
PropertyHint file(const std::string &filters=std::string(), bool save=false)
Tells the property editor that the path is for a file, and that it should show a "File Open"/"File Sa...
Definition: Path.h:247
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
void reflect(Reflector &r)
Definition: RemoteModule.h:224
bool isFrameworkConnected(const std::string &frameworkID) const
Returns if a framework with given ID is connected with us.
std::string Typename
Definition: Typename.h:60
bool isConnectedTo(const std::string &address) const
Returns if a framework with given address is connected to us.
void migrateUnitToThisFramework(const std::string &id)
Requests that the unit with the given id migrates to this framework.
RemoteConnectionPool mRemoteConnectionPool
Definition: RemoteModule.h:667
Server class handling incoming connections from remote frameworks.
Definition: RemoteServer.h:66
BinaryIosBase & host(BinaryIosBase &stream)
Manipulator that sets a binary input/output stream into host byte order mode and can be used to reset...
Definition: BinaryStream.h:712
Setter< T > setter(void(*f)(const T &))
Creates a Setter for global or static class methods taking the argument by const reference.
Definition: GetterSetter.h:443
Grants thread-safe access to an object (the Protectee) that should be protected from concurrent acces...
Definition: ScopedAccess.h:119
Connection class for outgoing connections.
Definition: RemoteConnection.h:901
void disconnectFrom(const std::string &address, bool autoReconnect=false)
Disconnects framework with given address (if it is connected).
std::string group
Definition: RemoteModule.h:165
Service that is used to discover other running frameworks in the same network using multicast on...
Definition: DiscoverService.h:68
std::list< KnownFramework > KnownFrameworkList
Definition: RemoteModule.h:134
Duration getPingInterval() const
Return the ping interval.
Definition: RemoteModule.h:347
uint8 VersionType
Definition: ReflectorInterface.h:72
RemoteServerPtr mServer
Definition: RemoteModule.h:661
CyclicRunnable mCyclicRunnable
Definition: RemoteModule.h:651
ServiceLevelList mServiceLevels
Definition: RemoteModule.h:673
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:416
This is the descriptive part of an authority.
Definition: AuthorityDescription.h:61
boost::recursive_mutex mConnectionMutex
Definition: RemoteModule.h:677
Time mDisconnectedSince
Definition: RemoteModule.h:671
std::list< RemoteConnectionProxy > ConnectionList
Definition: RemoteModule.h:131
Marker for indicating parameters that should be ignored if they are missing in the config file...
Definition: IgnoreMissing.h:73
void enablePTPSync(bool enable=true)
Enable/Disable synchronization of clocks via PTP.
Definition: RemoteModule.h:327
Duration mPingTimeout
Definition: RemoteModule.h:658
This class extends threads by the ability to execute an operation repeatedly in an defined interval...
Definition: CyclicRunnable.h:82
#define MIRA_FRAMEWORK_EXPORT
Definition: FrameworkExports.h:61
Server that accepts connections from remote frameworks.
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
A proxy object that represents a connection.
Definition: RemoteConnectionPool.h:53
DiscoverServicePtr mDiscoverService
Definition: RemoteModule.h:652
Duration mPingInterval
Definition: RemoteModule.h:657
std::map< UUID, RemoteConnectionProxy > ConnectionMap
Definition: RemoteModule.h:132
sec_type seconds() const
Returns normalized number of seconds (0..59)
Definition: Time.h:278
Use this class to represent time durations.
Definition: Time.h:104
void reflect(Reflector &r)
Definition: RemoteModule.h:168
bool mEnablePTPSync
Definition: RemoteModule.h:655
Definition: PerformanceStatistics.h:57
Gather statistics of the flow rate per second.
TypeServiceLevelMap mChannelTypeServiceLevels
Definition: RemoteModule.h:676
void enablePingTimeout(bool enable=true)
Enable/Disable the ping timeout.
Definition: RemoteModule.h:337
uint16 mPort
Definition: RemoteModule.h:654
AuthMode
Authentication mode.
Definition: RemoteModule.h:147
bool isPTPSyncEnabled() const
Returns true if PTP clock synchronization is enabled.
Definition: RemoteModule.h:332
KnownFrameworkList mKnownFrameworks
Definition: RemoteModule.h:672
Base class of connections between frameworks.
Definition: RemoteConnection.h:276
boost::shared_ptr< DiscoverService > DiscoverServicePtr
Definition: DiscoverService.h:97
uint32 mIncomingData
Definition: RemoteModule.h:648
std::map< std::string, ServiceLevel > ServiceLevelMap
Definition: RemoteModule.h:136
bool mEnablePingTimeout
Definition: RemoteModule.h:656
void disconnectFramework(const std::string &frameworkID, bool autoReconnect=false)
Disconnects framework with given ID (if it is connected).
Runnable concept that executes an operation repeatedly in an defined interval.
Typedefs and serialization support for uuids.
boost::shared_ptr< RemoteServer > RemoteServerPtr
Definition: RemoteModule.h:129
boost::thread mThread
Definition: RemoteModule.h:653
ServiceLevelMap mChannelServiceLevels
Definition: RemoteModule.h:675
Contains the authentication settings.
Definition: RemoteModule.h:163
std::map< Typename, TypeServiceLevel > TypeServiceLevelMap
Definition: RemoteModule.h:138
boost::uuids::uuid UUID
Shorter name for boost uuid.
Definition: UUID.h:66
void setPort(uint16 port)
Set the port of our TCP server.
Definition: RemoteModule.h:316
bool isPingTimeoutEnabled() const
Returns true if ping timeout is enabled.
Definition: RemoteModule.h:342
Duration getPingTimeout() const
Return the ping timeout.
Definition: RemoteModule.h:352
std::list< TypeServiceLevel > TypeServiceLevelList
Definition: RemoteModule.h:137
UUID mID
Definition: RemoteModule.h:660
AuthSettings mAuthSettings
Definition: RemoteModule.h:669
ConnectionList mPendingIncomingConnections
Definition: RemoteModule.h:664
A class for a RSA key.
Connection pool that holds the ownership for RemoteConnections.
Owner of every RemoteConnection.
Definition: RemoteConnectionPool.h:115
TypeServiceLevelList mTypeServiceLevels
Definition: RemoteModule.h:674
ConnectionMap mConnections
Definition: RemoteModule.h:665