EventPublisher.cpp
Go to the documentation of this file.
3 
4 using namespace swarmros;
5 using namespace swarmros::bridge;
6 
7 EventPublisher::EventPublisher(ros::NodeHandle& nodeHandle, const std::string& suffix, const std::string& message, swarmio::services::event::Service& eventService, const std::string& name)
8  : _eventService(eventService), _name(name)
9 {
10  // Register schema
12 
13  // Construct topic name
14  auto topic = "bridge/events/" + suffix;
15 
16  // Check message format
18  {
19  throw MessageMismatchException("Message type has no valid event header", topic, "swarmros/EventHeader", _serializer->GetFullName());
20  }
21 
22  // Create publisher
24  _publisher = nodeHandle.advertise(options);
25 
26  // Start handline
27  _eventService.RegisterHandler(name, this);
28 }
29 
30 void EventPublisher::EventWasTriggered(const swarmio::Node* node, const swarmio::data::event::Notification& event)
31 {
32  if (event.name() == _name)
33  {
35 
36  // Set type
37  message.SetType(_serializer->GetFullName());
38 
39  // Fill header
40  message.SetName(event.name());
41  message.SetNode(node->GetUUID());
42 
43  // Fill parameters
44  message.GetMutableParameters().mutable_pairs()->insert(event.parameters().begin(), event.parameters().end());
45 
46  // Publish message
47  _publisher.publish(message);
48  }
49  else
50  {
51  throw Exception("Trying to deliver unknown event");
52  }
53 }
54 
55 swarmio::data::discovery::Schema EventPublisher::DescribeEvent(const std::string& name)
56 {
57  if (name == _name)
58  {
60  }
61  else
62  {
63  throw Exception("Trying to query event descriptor for unknown event");
64  }
65 }
66 
68 {
70 }
bool HasHeader() const
Checks whether the message fits the requirements of a message with a header.
The Event service is responsible for triggering, handling and monitoring events.
Definition: event/Service.h:18
void publish(const boost::shared_ptr< M > &message) const
const std::string & GetCanonicalDefinition() const
Get the canonical definition of the message.
virtual swarmio::data::discovery::Schema DescribeEvent(const std::string &name) override
Describe the event based on the configuration of this instance.
std::string _name
Event name.
swarmio::data::Map & GetMutableParameters()
Get mutable event parameters.
Definition: EventMessage.h:150
void RegisterHandler(const std::string &name, Handler *handler)
Subscribe to events.
const std::string & GetHash() const
Get the MD5 hash of the message definition file.
static const MessageSerializer & MessageSerializerForType(const std::string &type, const std::string &parentPackage)
Look up or build a reader for a message type.
Exception base class for swarmros exceptions.
virtual void EventWasTriggered(const swarmio::Node *node, const swarmio::data::event::Notification &event) override
Publish the event in the ROS topic.
void SetName(const std::string &name)
Set event name.
Definition: EventMessage.h:110
EventMessage implements an interface with the ROS type system to send and receive events...
Definition: EventMessage.h:15
void SetType(const std::string &type)
Set the type of the object and fetch its serializer.
Definition: Message.h:66
const introspection::MessageSerializer * _serializer
Event name.
static bool IsEventSerializer(const introspection::MessageSerializer &serializer)
Checks whether a serializer has the correct layout.
Definition: EventMessage.h:39
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
EventPublisher(ros::NodeHandle &nodeHandle, const std::string &suffix, const std::string &message, swarmio::services::event::Service &eventService, const std::string &name)
Construct a new EventPublisher object.
virtual const std::string & GetUUID() const =0
Returns the unique identifier of the node.
Exception thrown when the expected type of a message does not match the actual type.
virtual ~EventPublisher() override
Destructor.
ros::Publisher _publisher
Publisher.
Represents a Node the Endpoint knows about and can send messages to.
void UnregisterHandler(const std::string &name)
Unsubscribe from events.
virtual const std::string & GetFullName() const override
Get the fully qualified name for the type behind the serializer.
swarmio::services::event::Service & _eventService
Event service.
swarmio::data::discovery::Schema GetSchemaDescriptor(unsigned skipCount) const
Build a field descriptor for the underlying type, with or without its header.
void SetNode(const std::string &node)
Set node UUID.
Definition: EventMessage.h:130


swarmros
Author(s):
autogenerated on Fri Apr 3 2020 03:42:48