radio.hpp
Go to the documentation of this file.
1 
4 /*****************************************************************************
5 ** Ifdefs
6 *****************************************************************************/
7 
8 #ifndef mm_radio_RADIO_HPP_
9 #define mm_radio_RADIO_HPP_
10 
11 /*****************************************************************************
12 ** Includes
13 *****************************************************************************/
14 
16 #include <ecl/threads/mutex.hpp>
17 #include <ecl/threads/thread.hpp>
18 #include <map>
19 #include <memory>
20 #include <mm_messages.hpp>
21 #include <string>
22 
23 /*****************************************************************************
24 ** Namespaces
25 *****************************************************************************/
26 
27 namespace mm_radio {
28 namespace impl {
29 
30 /*****************************************************************************
31  ** Interfaces
32  *****************************************************************************/
33 
34 class Radio {
35 public:
36  Radio(const std::string& name,
37  const std::string& url,
38  const bool bind,
40  );
41  Radio(const Radio& other);
42  ~Radio();
43 
44  /********************
45  ** Start/Stop
46  ********************/
47  void spin();
48  void shutdown();
49 
50  /********************
51  ** Transmission
52  ********************/
53  int send(const unsigned int& id, const mm_messages::ByteArray& msg_buffer);
54 
55  /********************
56  ** Subscribers
57  ********************/
58  template<typename C>
60  const unsigned int& id,
61  void (C::*processPacket)(const unsigned char*, const unsigned int&),
62  C &s) {
64  mutex.lock();
66  ret = subscribers.insert(SubscriberMapPair(id, function));
67  mutex.unlock();
68  if ( !ret.second ) {
69  // some error handling
70  }
71  }
72 
73  void unregisterSubscriber(const unsigned int& id);
74 
75 private:
77  typedef std::map<unsigned int, BufferCallbackFunction> SubscriberMap;
78  typedef std::map<unsigned int, BufferCallbackFunction>::iterator SubscriberMapIterator;
79  typedef std::map<unsigned int, BufferCallbackFunction>::const_iterator SubscriberMapConstIterator;
80  typedef std::pair<unsigned int, BufferCallbackFunction> SubscriberMapPair;
81  typedef std::pair<std::map<unsigned int, BufferCallbackFunction>::iterator,bool> SubscriberMapResultPair;
82 
83  std::string name, url;
87  ecl::Thread thread;
88  SubscriberMap subscribers;
89  ecl::Mutex mutex;
90 };
91 
92 } // namespace impl
93 } // mm_radio
94 
95 /*****************************************************************************
96 ** Namespaces
97 *****************************************************************************/
98 
99 namespace mm_radio {
100 
101 /*****************************************************************************
102 ** Interfaces
103 *****************************************************************************/
104 
105 class Radio {
106 public:
107  typedef std::map<std::string, std::shared_ptr<impl::Radio>> RadioMap;
108  typedef std::pair<std::string, std::shared_ptr<impl::Radio>> RadioMapPair;
109  typedef std::map<std::string, std::shared_ptr<impl::Radio>>::iterator RadioMapIterator;
110  typedef std::map<std::string, std::shared_ptr<impl::Radio>>::const_iterator RadioMapConstIterator;
111 
112  enum Errors {
113  NotAvailable = -1 // when you try to connect to <name>, but <name> has not yet been started
114  };
115 
116  /********************
117  ** Registration
118  ********************/
119  static void startServer(const std::string& name,
120  const std::string& url,
122  static void startClient(const std::string& name,
123  const std::string& url,
125  static RadioMap& radios();
126 
127  /********************
128  ** Shutdown
129  ********************/
130  static void shutdown(); // shutdown all radios
131  static void shutdown(const std::string& name); // shutdown only this radio
132 
133  /********************
134  ** Transmissions
135  ********************/
136  static int send(const std::string& name, const unsigned int& id, const mm_messages::ByteArray& msg_buffer);
137 
152  template<typename C>
153  static void registerSubscriber(
154  const std::string& name,
155  const unsigned int& id,
156  void (C::*processPacket)(const unsigned char*, const unsigned int&),
157  C &s)
158  {
159  RadioMapIterator iter = Radio::radios().find(name);
160  if ( iter != Radio::radios().end() ) {
161  (iter->second)->registerSubscriber(id, &C::processPacket, s);
162  } else {
163  std::cout << "Radio : no radio by that name found (while registering subscriber)"<< std::endl;
164  }
165  }
166 
167  static void unregisterSubscriber(const std::string& name, const unsigned int& id);
168 };
169 
170 } // namespace mm_radio
171 
172 #endif /* mm_radio_RADIO_HPP_ */
int send(const unsigned int &id, const mm_messages::ByteArray &msg_buffer)
Definition: lib/radio.cpp:166
void unregisterSubscriber(const unsigned int &id)
Definition: lib/radio.cpp:159
std::map< std::string, std::shared_ptr< impl::Radio > >::iterator RadioMapIterator
Definition: radio.hpp:109
ecl::BinaryFunction< const unsigned char *, const unsigned int &, void > * BufferCallbackFunction
Definition: radio.hpp:76
std::map< std::string, std::shared_ptr< impl::Radio > > RadioMap
Definition: radio.hpp:107
std::pair< std::string, std::shared_ptr< impl::Radio > > RadioMapPair
Definition: radio.hpp:108
std::map< unsigned int, BufferCallbackFunction > SubscriberMap
Definition: radio.hpp:77
std::map< unsigned int, BufferCallbackFunction >::const_iterator SubscriberMapConstIterator
Definition: radio.hpp:79
ecl::Thread thread
Definition: radio.hpp:87
void registerSubscriber(const unsigned int &id, void(C::*processPacket)(const unsigned char *, const unsigned int &), C &s)
Definition: radio.hpp:59
std::map< unsigned int, BufferCallbackFunction >::iterator SubscriberMapIterator
Definition: radio.hpp:78
static RadioMap & radios()
Definition: lib/radio.cpp:253
Radio(const std::string &name, const std::string &url, const bool bind, const mm_messages::Verbosity::Level &verbosity=mm_messages::Verbosity::QUIET)
Definition: lib/radio.cpp:28
mm_messages::Verbosity::Level verbosity
Definition: radio.hpp:85
std::map< std::string, std::shared_ptr< impl::Radio > >::const_iterator RadioMapConstIterator
Definition: radio.hpp:110
SubscriberMap subscribers
Definition: radio.hpp:88
static void registerSubscriber(const std::string &name, const unsigned int &id, void(C::*processPacket)(const unsigned char *, const unsigned int &), C &s)
Register a callback with the specified demux.
Definition: radio.hpp:153
std::vector< unsigned char > ByteArray
std::string name
Definition: radio.hpp:83
std::string url
Definition: radio.hpp:83
std::pair< std::map< unsigned int, BufferCallbackFunction >::iterator, bool > SubscriberMapResultPair
Definition: radio.hpp:81
ecl::Mutex mutex
Definition: radio.hpp:89
std::pair< unsigned int, BufferCallbackFunction > SubscriberMapPair
Definition: radio.hpp:80


mm_radio
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:52:16