SubscriptionHandler.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 
18 #ifndef __IGN_TRANSPORT_SUBSCRIPTIONHANDLER_HH_INCLUDED__
19 #define __IGN_TRANSPORT_SUBSCRIPTIONHANDLER_HH_INCLUDED__
20 
21 #ifdef _MSC_VER
22 #pragma warning(push, 0)
23 #endif
24 #include <google/protobuf/message.h>
25 #ifdef _MSC_VER
26 #pragma warning(pop)
27 #endif
28 
29 #include <functional>
30 #include <iostream>
31 #include <memory>
32 #include <string>
33 
37 
38 namespace ignition
39 {
40  namespace transport
41  {
46  {
49  public: ISubscriptionHandler(const std::string &_nUuid)
50  : hUuid(Uuid().ToString()),
51  nUuid(_nUuid)
52  {
53  }
54 
56  public: virtual ~ISubscriptionHandler()
57  {
58  }
59 
63  public: virtual bool RunLocalCallback(
64  const transport::ProtoMsg &_msg) const = 0;
65 
69  public: virtual const std::shared_ptr<transport::ProtoMsg> CreateMsg(
70  const std::string &_data) const = 0;
71 
75  public: virtual std::string TypeName() = 0;
76 
79  public: std::string NodeUuid() const
80  {
81  return this->nUuid;
82  }
83 
86  public: std::string HandlerUuid() const
87  {
88  return this->hUuid;
89  }
90 
92  protected: std::string hUuid;
93 
95  private: std::string nUuid;
96  };
97 
102  template <typename T> class SubscriptionHandler
103  : public ISubscriptionHandler
104  {
105  // Documentation inherited.
106  public: SubscriptionHandler(const std::string &_nUuid)
107  : ISubscriptionHandler(_nUuid)
108  {
109  }
110 
111  // Documentation inherited.
112  public: const std::shared_ptr<transport::ProtoMsg> CreateMsg(
113  const std::string &_data) const
114  {
115  // Instantiate a specific protobuf message
116  auto msgPtr = std::make_shared<T>();
117 
118  // Create the message using some serialized data
119  if (!msgPtr->ParseFromString(_data))
120  {
121  std::cerr << "SubscriptionHandler::CreateMsg() error: ParseFromString"
122  << " failed" << std::endl;
123  }
124 
125  return msgPtr;
126  }
127 
128  // Documentation inherited.
129  public: std::string TypeName()
130  {
131  return T().GetTypeName();
132  }
133 
137  public: void SetCallback(const std::function <void(const T &_msg)> &_cb)
138  {
139  this->cb = _cb;
140  }
141 
142  // Documentation inherited.
143  public: bool RunLocalCallback(const transport::ProtoMsg &_msg) const
144  {
145  // Execute the callback (if existing)
146  if (this->cb)
147  {
148  auto msgPtr = google::protobuf::down_cast<const T*>(&_msg);
149 
150  this->cb(*msgPtr);
151  return true;
152  }
153  else
154  {
155  std::cerr << "SubscriptionHandler::RunLocalCallback() error: "
156  << "Callback is NULL" << std::endl;
157  return false;
158  }
159  }
160 
164  private: std::function<void(const T &_msg)> cb;
165  };
166  }
167 }
168 
169 #endif
std::string NodeUuid() const
Get the node UUID.
Definition: SubscriptionHandler.hh:79
#define IGNITION_VISIBLE
Use to represent "symbol visible" if supported.
Definition: Helpers.hh:56
std::string HandlerUuid() const
Get the unique UUID of this handler.
Definition: SubscriptionHandler.hh:86
SubscriptionHandler(const std::string &_nUuid)
Definition: SubscriptionHandler.hh:106
const std::shared_ptr< transport::ProtoMsg > CreateMsg(const std::string &_data) const
Create a specific protobuf message given its serialized data.
Definition: SubscriptionHandler.hh:112
void SetCallback(const std::function< void(const T &_msg)> &_cb)
Set the callback for this handler.
Definition: SubscriptionHandler.hh:137
std::string hUuid
Unique handler&#39;s UUID.
Definition: SubscriptionHandler.hh:92
A portable class for representing a Universally Unique Identifier.
Definition: Uuid.hh:41
google::protobuf::Message ProtoMsg
Definition: TransportTypes.hh:62
std::string TypeName()
Get the type of the messages from which this subscriber handler is subscribed.
Definition: SubscriptionHandler.hh:129
bool RunLocalCallback(const transport::ProtoMsg &_msg) const
Executes the local callback registered for this handler.
Definition: SubscriptionHandler.hh:143
It creates a subscription handler for a specific protobuf message.
Definition: SubscriptionHandler.hh:102
Definition: AdvertiseOptions.hh:25
ISubscriptionHandler(const std::string &_nUuid)
Constructor.
Definition: SubscriptionHandler.hh:49
ignition/transport/SubscriptionHandler.hh
Definition: SubscriptionHandler.hh:45
virtual ~ISubscriptionHandler()
Destructor.
Definition: SubscriptionHandler.hh:56