publisher.hpp
Go to the documentation of this file.
1 
6 /*****************************************************************************
7 ** Ifdefs
8 *****************************************************************************/
9 
10 #ifndef mm_messages_PUBLISHER_HPP_
11 #define mm_messages_PUBLISHER_HPP_
12 
13 /*****************************************************************************
14 ** Includes
15 *****************************************************************************/
16 
17 #include <iostream>
18 #include <ecl/exceptions.hpp>
19 #include "exceptions.hpp"
20 #include "registry.hpp"
21 #include <sstream>
22 #include <string>
23 #include <vector>
24 
25 /*****************************************************************************
26 ** Namespaces
27 *****************************************************************************/
28 
29 namespace mm_messages {
30 
31 /*****************************************************************************
32 ** Interfaces
33 *****************************************************************************/
40 template<typename Multiplexer>
41 class Publisher {
42 public:
45 
46  Publisher(const std::string &name)
47  : name(name)
48  {
49  // might want to check 'name' is actually up on Multiplexer
50  }
51 
52  virtual ~Publisher() {}
53 
62  template<typename T>
63  void publish(const unsigned int& id, const T& msg) {
64  if ( !MessageRegistry::isRegistered(id) ) {
65  std::stringstream ss;
66  ss << "id '" << id << "' has not been registered";
67  throw mm_messages::UnregisteredID(ss.str());
68  }
69  if ( !MessageRegistry::isRegisteredWithType<T>(id) ) {
70  std::stringstream ss;
71  ss << "id '" << id << "' is registered, but not with this type";
73  }
74  ByteArray buffer;
76  try {
77  int result = Multiplexer::send(name, id, buffer); // this throws ecl::StandardException(ecl::TimeOutError)
78  } catch (ecl::StandardException &e) {
79  if ( e.flag() == ecl::TimeOutError ) {
80  // quiet...could use this to update internally so that it knows there is a client or not.
81  // could pass a boolean to this flag so that it knows to be verbose.
82  } else {
83  throw ecl::StandardException(e);
84  }
85  }
86  }
87 
88 private:
89  std::string name;
90 };
91 
92 //class PseudoPublisher {
93 //public:
94 // PseudoPublisher(const std::string &name, const std::string &url = "") {}
95 //
96 // template<typename T>
97 // void publish(const unsigned int& id, const T& msg) {}
98 //};
99 
100 } // namespace mm_messages
101 
102 #endif /* mm_messages_PUBLISHER_HPP_ */
mm_messages::MessageRegistry MessageRegistry
Definition: publisher.hpp:43
void publish(const unsigned int &id, const T &msg)
Shunt a message off to be published.
Definition: publisher.hpp:63
static void encode(const T &msg, ByteArray &buffer)
Definition: message.hpp:47
static bool isRegistered(const int &id)
Definition: registry.hpp:56
Template parent class for specific publishers.
Definition: publisher.hpp:41
Publisher(const std::string &name)
Definition: publisher.hpp:46
std::vector< unsigned char > ByteArray
Definition: message.hpp:29
TimeOutError
mm_messages::ByteArray ByteArray
Definition: publisher.hpp:44
Short description of this file.
const ErrorFlag & flag() const


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