demux.hpp
Go to the documentation of this file.
1 
6 /*****************************************************************************
7 ** Ifdefs
8 *****************************************************************************/
9 
10 #ifndef mm_mux_demux_DEMUX_HPP_
11 #define mm_mux_demux_DEMUX_HPP_
12 
13 /*****************************************************************************
14 ** Includes
15 *****************************************************************************/
16 
18 #include <ecl/threads/mutex.hpp>
19 #include <ecl/threads.hpp>
20 #include <map>
21 #include <memory>
23 #include <string>
24 
25 /*****************************************************************************
26 ** Namespaces
27 *****************************************************************************/
28 
29 namespace mm_mux_demux {
30 namespace impl {
31 
32 /*****************************************************************************
33  ** Interfaces
34  *****************************************************************************/
35 
36 class MessageDemux {
37 public:
38  MessageDemux(const std::string& name,
39  const std::string& url,
41  const bool bind = false
42  );
43  MessageDemux(const MessageDemux& other);
44  ~MessageDemux();
45  void spin();
46  void shutdown();
47 
48  template<typename C>
50  const unsigned int& id,
51  void (C::*processPacket)(const unsigned char*, const unsigned int&),
52  C &s) {
54  mutex.lock();
56  ret = subscribers.insert(SubscriberMapPair(id, function));
57  mutex.unlock();
58  if ( !ret.second ) {
59  // some error handling
60  }
61  }
62 
63  void unregisterSubscriber(const unsigned int& id);
64 
65 private:
66 // typedef ecl::UnaryFunction<const unsigned char*,void>* BufferCallbackFunction;
68  typedef std::map<unsigned int, BufferCallbackFunction> SubscriberMap;
69  typedef std::map<unsigned int, BufferCallbackFunction>::iterator SubscriberMapIterator;
70  typedef std::map<unsigned int, BufferCallbackFunction>::const_iterator SubscriberMapConstIterator;
71  typedef std::pair<unsigned int, BufferCallbackFunction> SubscriberMapPair;
72  typedef std::pair<std::map<unsigned int, BufferCallbackFunction>::iterator,bool> SubscriberMapResultPair;
73 
74  std::string name;
75  std::string url; // copy of the connection url used for debugging purposes.
76  int socket;
77  int endpoint_id; // used by nanomsg to id the endpoint connected to the socket (used in shutdown).
80  ecl::Thread thread;
81  //std::thread thread;
82  SubscriberMap subscribers;
83  ecl::Mutex mutex;
84 };
85 
86 } // namespace impl
87 } // mm_mux_demux
88 
89 /*****************************************************************************
90 ** Namespaces
91 *****************************************************************************/
92 
93 namespace mm_mux_demux {
94 
95 /*****************************************************************************
96 ** Interfaces
97 *****************************************************************************/
98 
99 class MessageDemux {
100 public:
101  typedef std::map<std::string, std::shared_ptr<impl::MessageDemux>> DemuxMap;
102  typedef std::pair<std::string, std::shared_ptr<impl::MessageDemux>> DemuxMapPair;
103  typedef std::map<std::string, std::shared_ptr<impl::MessageDemux>>::iterator DemuxMapIterator;
104  typedef std::map<std::string, std::shared_ptr<impl::MessageDemux>>::const_iterator DemuxMapConstIterator;
105 
106  static void start(const std::string& name,
107  const std::string& url,
109  const bool bind = false);
110  static void shutdown(); // shutdown all demuxes
111  static void shutdown(const std::string& name); // shutdown only this demux
119  static DemuxMap& demultiplexers();
120 
135  template<typename C>
136  static void registerSubscriber(
137  const std::string& name,
138  const unsigned int& id,
139  void (C::*processPacket)(const unsigned char*, const unsigned int&),
140  C &s)
141  {
142  DemuxMapIterator iter = demultiplexers().find(name);
143  if ( iter != demultiplexers().end() ) {
144  (iter->second)->registerSubscriber(id, &C::processPacket, s);
145  } else {
146  std::cout << "Demux : no demux by that name found (while registering subscriber)"<< std::endl;
147  }
148  }
149 
150  static void unregisterSubscriber(const std::string& name, const unsigned int& id);
151 };
152 
154 
155 } // namespace mm_mux_demux
156 
157 #endif /* mm_mux_demux_DEMUX_HPP_ */
MessageDemux(const std::string &name, const std::string &url, const mm_messages::Verbosity::Level &verbosity=mm_messages::Verbosity::QUIET, const bool bind=false)
Definition: demux.cpp:28
std::pair< std::map< unsigned int, BufferCallbackFunction >::iterator, bool > SubscriberMapResultPair
Definition: demux.hpp:72
std::pair< unsigned int, BufferCallbackFunction > SubscriberMapPair
Definition: demux.hpp:71
ecl::BinaryFunction< const unsigned char *, const unsigned int &, void > * BufferCallbackFunction
Definition: demux.hpp:67
std::map< std::string, std::shared_ptr< impl::MessageDemux > >::iterator DemuxMapIterator
Definition: demux.hpp:103
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: demux.hpp:136
void unregisterSubscriber(const unsigned int &id)
Definition: demux.cpp:150
mm_messages::Verbosity::Level verbosity
Definition: demux.hpp:78
void registerSubscriber(const unsigned int &id, void(C::*processPacket)(const unsigned char *, const unsigned int &), C &s)
Definition: demux.hpp:49
std::map< unsigned int, BufferCallbackFunction > SubscriberMap
Definition: demux.hpp:68
std::pair< std::string, std::shared_ptr< impl::MessageDemux > > DemuxMapPair
Definition: demux.hpp:102
std::map< std::string, std::shared_ptr< impl::MessageDemux > > DemuxMap
Definition: demux.hpp:101
std::map< std::string, std::shared_ptr< impl::MessageDemux > >::const_iterator DemuxMapConstIterator
Definition: demux.hpp:104
std::map< unsigned int, BufferCallbackFunction >::iterator SubscriberMapIterator
Definition: demux.hpp:69
std::map< unsigned int, BufferCallbackFunction >::const_iterator SubscriberMapConstIterator
Definition: demux.hpp:70
MessageDemux MessageClient
Definition: demux.hpp:153


mm_mux_demux
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:52:14