radio.hpp
Go to the documentation of this file.
00001 
00004 /*****************************************************************************
00005 ** Ifdefs
00006 *****************************************************************************/
00007 
00008 #ifndef mm_radio_RADIO_HPP_
00009 #define mm_radio_RADIO_HPP_
00010 
00011 /*****************************************************************************
00012 ** Includes
00013 *****************************************************************************/
00014 
00015 #include <ecl/utilities/function_objects.hpp>
00016 #include <ecl/threads/mutex.hpp>
00017 #include <ecl/threads/thread.hpp>
00018 #include <map>
00019 #include <memory>
00020 #include <mm_messages.hpp>
00021 #include <string>
00022 
00023 /*****************************************************************************
00024 ** Namespaces
00025 *****************************************************************************/
00026 
00027 namespace mm_radio {
00028 namespace impl {
00029 
00030 /*****************************************************************************
00031  ** Interfaces
00032  *****************************************************************************/
00033 
00034 class Radio {
00035 public:
00036   Radio(const std::string& name,
00037                 const std::string& url,
00038                 const bool bind,
00039                 const mm_messages::Verbosity::Level& verbosity=mm_messages::Verbosity::QUIET
00040                );
00041   Radio(const Radio& other);
00042   ~Radio();
00043 
00044   /********************
00045    ** Start/Stop
00046    ********************/
00047   void spin();
00048   void shutdown();
00049 
00050   /********************
00051    ** Transmission
00052    ********************/
00053   int send(const unsigned int& id, const mm_messages::ByteArray& msg_buffer);
00054 
00055   /********************
00056    ** Subscribers
00057    ********************/
00058   template<typename C>
00059   void registerSubscriber(
00060       const unsigned int& id,
00061       void (C::*processPacket)(const unsigned char*, const unsigned int&),
00062       C &s) {
00063     SubscriberMapResultPair ret;
00064     mutex.lock();
00065     BufferCallbackFunction function = new ecl::PartiallyBoundBinaryMemberFunction<C,const unsigned char*,const unsigned int&, void>(processPacket,s);
00066     ret = subscribers.insert(SubscriberMapPair(id, function));
00067     mutex.unlock();
00068     if ( !ret.second )  {
00069       // some error handling
00070     }
00071   }
00072 
00073   void unregisterSubscriber(const unsigned int& id);
00074 
00075 private:
00076   typedef ecl::BinaryFunction<const unsigned char*,const unsigned int&,void>* BufferCallbackFunction;
00077   typedef std::map<unsigned int, BufferCallbackFunction> SubscriberMap;
00078   typedef std::map<unsigned int, BufferCallbackFunction>::iterator SubscriberMapIterator;
00079   typedef std::map<unsigned int, BufferCallbackFunction>::const_iterator SubscriberMapConstIterator;
00080   typedef std::pair<unsigned int, BufferCallbackFunction> SubscriberMapPair;
00081   typedef std::pair<std::map<unsigned int, BufferCallbackFunction>::iterator,bool> SubscriberMapResultPair;
00082 
00083   std::string name, url;
00084   int socket, endpoint_id;
00085   mm_messages::Verbosity::Level verbosity;
00086   bool shutdown_requested;
00087   ecl::Thread thread;
00088   SubscriberMap subscribers;
00089   ecl::Mutex mutex;
00090 };
00091 
00092 } // namespace impl
00093 } // mm_radio
00094 
00095 /*****************************************************************************
00096 ** Namespaces
00097 *****************************************************************************/
00098 
00099 namespace mm_radio {
00100 
00101 /*****************************************************************************
00102 ** Interfaces
00103 *****************************************************************************/
00104 
00105 class Radio {
00106 public:
00107   typedef std::map<std::string, std::shared_ptr<impl::Radio>> RadioMap;
00108   typedef std::pair<std::string, std::shared_ptr<impl::Radio>> RadioMapPair;
00109   typedef std::map<std::string, std::shared_ptr<impl::Radio>>::iterator RadioMapIterator;
00110   typedef std::map<std::string, std::shared_ptr<impl::Radio>>::const_iterator RadioMapConstIterator;
00111 
00112   enum Errors {
00113     NotAvailable = -1   // when you try to connect to <name>, but <name> has not yet been started
00114   };
00115 
00116   /********************
00117    ** Registration
00118    ********************/
00119   static void startServer(const std::string& name,
00120                           const std::string& url,
00121                           const mm_messages::Verbosity::Level& verbosity=mm_messages::Verbosity::QUIET);
00122   static void startClient(const std::string& name,
00123                           const std::string& url,
00124                           const mm_messages::Verbosity::Level& verbosity=mm_messages::Verbosity::QUIET);
00125   static RadioMap& radios();
00126 
00127   /********************
00128    ** Shutdown
00129    ********************/
00130   static void shutdown();  // shutdown all radios
00131   static void shutdown(const std::string& name);  // shutdown only this radio
00132 
00133   /********************
00134    ** Transmissions
00135    ********************/
00136   static int send(const std::string& name, const unsigned int& id, const mm_messages::ByteArray& msg_buffer);
00137 
00152  template<typename C>
00153  static void registerSubscriber(
00154      const std::string& name,
00155      const unsigned int& id,
00156      void (C::*processPacket)(const unsigned char*, const unsigned int&),
00157      C &s)
00158  {
00159    RadioMapIterator iter = Radio::radios().find(name);
00160    if ( iter != Radio::radios().end() ) {
00161      (iter->second)->registerSubscriber(id, &C::processPacket, s);
00162    } else {
00163      std::cout << "Radio : no radio by that name found (while registering subscriber)"<< std::endl;
00164    }
00165  }
00166 
00167  static void unregisterSubscriber(const std::string& name, const unsigned int& id);
00168 };
00169 
00170 } // namespace mm_radio
00171 
00172 #endif /* mm_radio_RADIO_HPP_ */


mm_radio
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:13:24