00001 00006 /***************************************************************************** 00007 ** Ifdefs 00008 *****************************************************************************/ 00009 00010 #ifndef mm_messages_PUBLISHER_HPP_ 00011 #define mm_messages_PUBLISHER_HPP_ 00012 00013 /***************************************************************************** 00014 ** Includes 00015 *****************************************************************************/ 00016 00017 #include <iostream> 00018 #include <ecl/exceptions.hpp> 00019 #include "exceptions.hpp" 00020 #include "registry.hpp" 00021 #include <sstream> 00022 #include <string> 00023 #include <vector> 00024 00025 /***************************************************************************** 00026 ** Namespaces 00027 *****************************************************************************/ 00028 00029 namespace mm_messages { 00030 00031 /***************************************************************************** 00032 ** Interfaces 00033 *****************************************************************************/ 00040 template<typename Multiplexer> 00041 class Publisher { 00042 public: 00043 typedef mm_messages::MessageRegistry MessageRegistry; 00044 typedef mm_messages::ByteArray ByteArray; 00045 00046 Publisher(const std::string &name) 00047 : name(name) 00048 { 00049 // might want to check 'name' is actually up on Multiplexer 00050 } 00051 00052 virtual ~Publisher() {} 00053 00062 template<typename T> 00063 void publish(const unsigned int& id, const T& msg) { 00064 if ( !MessageRegistry::isRegistered(id) ) { 00065 std::stringstream ss; 00066 ss << "id '" << id << "' has not been registered"; 00067 throw mm_messages::UnregisteredID(ss.str()); 00068 } 00069 if ( !MessageRegistry::isRegisteredWithType<T>(id) ) { 00070 std::stringstream ss; 00071 ss << "id '" << id << "' is registered, but not with this type"; 00072 throw mm_messages::InvalidIDTypeCombination(ss.str()); 00073 } 00074 ByteArray buffer; 00075 mm_messages::Message<T>::encode(msg, buffer); 00076 try { 00077 int result = Multiplexer::send(name, id, buffer); // this throws ecl::StandardException(ecl::TimeOutError) 00078 } catch (ecl::StandardException &e) { 00079 if ( e.flag() == ecl::TimeOutError ) { 00080 // quiet...could use this to update internally so that it knows there is a client or not. 00081 // could pass a boolean to this flag so that it knows to be verbose. 00082 } else { 00083 throw ecl::StandardException(e); 00084 } 00085 } 00086 } 00087 00088 private: 00089 std::string name; 00090 }; 00091 00092 //class PseudoPublisher { 00093 //public: 00094 // PseudoPublisher(const std::string &name, const std::string &url = "") {} 00095 // 00096 // template<typename T> 00097 // void publish(const unsigned int& id, const T& msg) {} 00098 //}; 00099 00100 } // namespace mm_messages 00101 00102 #endif /* mm_messages_PUBLISHER_HPP_ */