fipa_services
Transport.hpp
Go to the documentation of this file.
1 #ifndef FIPA_SERVICES_TRANSPORTS_TRANSPORT_HPP
2 #define FIPA_SERVICES_TRANSPORTS_TRANSPORT_HPP
3 
4 #include <map>
5 #include <fipa_acl/fipa_acl.h>
6 #include <fipa_services/DistributedServiceDirectory.hpp>
7 #include <fipa_services/ServiceLocator.hpp>
8 #include <boost/asio/ip/tcp.hpp>
9 #include <fipa_services/transports/udt/OutgoingConnection.hpp>
10 
11 #include "Configuration.hpp"
12 
13 namespace fipa {
14 namespace services {
15 namespace transports {
16 
18 typedef boost::function1<void, const std::string&> TransportObserver;
19 
24 class Transport
25 {
26 public:
28  enum Type { UNKNOWN = 0x00, UDT = 0x01, TCP = 0x02, ALL = 0xFF };
29 
30  static std::map<Type, std::string> TypeTxt;
31 
32 private:
33  Type mType;
34  std::vector<TransportObserver> mObservers;
35 
36  Transport() {}
37 
38 protected:
39  Transport(Type type);
40 
41  Transport(const Configuration& configuration);
42 
43 public:
44  virtual ~Transport() {}
45 
46  typedef boost::shared_ptr<Transport> Ptr;
47 
52  static Transport::Ptr create(Type type);
53 
57  static Type getTypeFromTxt(const std::string& typeTxt);
58 
63  void registerObserver(TransportObserver observer);
64 
69  Type getType() const { return mType; }
70 
74  std::string getName() const { return Transport::TypeTxt[mType]; }
75 
81  static std::string getLocalIPv4Address(const std::string& interfaceName = "eth0");
82 
90  void send(const std::string& receiverName, const Address& address, const std::string& data);
91 
96  std::set<Address> getAddresses() const;
97 
102  virtual Address getAddress(const std::string& interface = "eth0") const { throw std::runtime_error("fipa::services::Transport::getAddress not implemented by transport: " + getName()); }
103 
108  virtual OutgoingConnection::Ptr establishOutgoingConnection(const Address& address) { throw std::runtime_error("fipa::services::Transport::establishOutgoingConnection not implemented by transport: " + getName()); }
109 
113  virtual void start() { throw std::runtime_error("fipa::services::Transport::start not implemented by transport: " + getName()); }
114 
118  virtual void update(bool readAllMessages = true) { throw std::runtime_error("fipa::services::transports::Transport::update not implemented by transport: " + getName()); }
119 
127  OutgoingConnection::Ptr getCachedOutgoingConnection(const std::string& receiverName, const Address& address);
128 
132  void cleanup(const std::string& receiver);
133 
137  void notify(const std::string& message);
138 
142  void setConfiguration(const Configuration& configuration) { mConfiguration = configuration; }
143 
147  const Configuration& getConfiguration() const { return mConfiguration; }
148 
149 protected:
151 
152 private:
156  std::map<std::string, OutgoingConnection::Ptr> mOutgoingConnections;
157 };
158 
159 } // end namespace transports
160 } // end namespace services
161 } // end namespace fipa
162 #endif // FIPA_SERVICES_TRANSPORTS_TRANSPORT_HPP
std::set< Address > getAddresses() const
Definition: Transport.cpp:188
Type
Builtin transport types that can be activated.
Definition: Transport.hpp:28
Definition: Transport.hpp:28
virtual ~Transport()
Definition: Transport.hpp:44
Configuration mConfiguration
Definition: Transport.hpp:150
static std::string getLocalIPv4Address(const std::string &interfaceName="eth0")
Definition: Transport.cpp:27
Connection management base class.
Definition: Transport.hpp:24
static std::map< Type, std::string > TypeTxt
Definition: Transport.hpp:30
boost::shared_ptr< OutgoingConnection > Ptr
Definition: OutgoingConnection.hpp:18
virtual void update(bool readAllMessages=true)
Definition: Transport.hpp:118
void registerObserver(TransportObserver observer)
Definition: Transport.cpp:105
void notify(const std::string &message)
Definition: Transport.cpp:110
virtual Address getAddress(const std::string &interface="eth0") const
Definition: Transport.hpp:102
void setConfiguration(const Configuration &configuration)
Definition: Transport.hpp:142
Communication address specified by IP, port, and protocol.
Definition: Address.hpp:15
virtual void start()
Definition: Transport.hpp:113
virtual OutgoingConnection::Ptr establishOutgoingConnection(const Address &address)
Definition: Transport.hpp:108
void cleanup(const std::string &receiver)
Definition: Transport.cpp:228
OutgoingConnection::Ptr getCachedOutgoingConnection(const std::string &receiverName, const Address &address)
Definition: Transport.cpp:121
Definition: Configuration.hpp:14
Type getType() const
Definition: Transport.hpp:69
Definition: Transport.hpp:28
boost::function1< void, const std::string & > TransportObserver
Allow to register callbacks with a transport once data arrives.
Definition: Transport.hpp:18
boost::shared_ptr< Transport > Ptr
Definition: Transport.hpp:46
static Type getTypeFromTxt(const std::string &typeTxt)
Definition: Transport.cpp:89
std::string getName() const
Definition: Transport.hpp:74
const Configuration & getConfiguration() const
Definition: Transport.hpp:147
Definition: DistributedServiceDirectory.cpp:5
static Transport::Ptr create(Type type)
Definition: Transport.cpp:62
void send(const std::string &receiverName, const Address &address, const std::string &data)
Definition: Transport.cpp:144