Subscription.hpp
Go to the documentation of this file.
1 #ifndef SUBSCRIPTION_HPP
2 #define SUBSCRIPTION_HPP
3 
4 #include <functional>
5 #include "Client.hpp"
6 #include "Message.hpp"
7 #include "PeriodicTimer.hpp"
8 
9 namespace msp {
10 namespace client {
11 
13 public:
15 
16  virtual ~SubscriptionBase() {}
17 
18  virtual void decode(msp::ByteVector& data) const = 0;
19 
20  virtual void makeRequest() const = 0;
21 
22  virtual void handleResponse() const = 0;
23 
24  virtual const msp::Message& getMsgObject() const = 0;
25 
30  bool isAutomatic() const {
31  return hasTimer() && (timer_->getPeriod() > 0.0);
32  }
33 
38  bool hasTimer() const { return timer_ ? true : false; }
39 
44  bool start() const { return this->timer_->start(); }
45 
50  bool stop() const { return this->timer_->stop(); }
51 
56  void setTimerPeriod(const double& period_seconds) {
57  if(timer_) {
58  timer_->setPeriod(period_seconds);
59  }
60  else if(period_seconds > 0.0) {
61  timer_ = std::unique_ptr<PeriodicTimer>(new PeriodicTimer(
62  std::bind(&SubscriptionBase::makeRequest, this),
63  period_seconds));
64  this->timer_->start();
65  }
66  }
67 
72  void setTimerFrequency(const double& rate_hz) {
73  if(timer_) {
74  timer_->setPeriod(1.0 / rate_hz);
75  }
76  else if(rate_hz > 0.0) {
77  timer_ = std::unique_ptr<PeriodicTimer>(new PeriodicTimer(
78  std::bind(&SubscriptionBase::makeRequest, this),
79  1.0 / rate_hz));
80  this->timer_->start();
81  }
82  }
83 
84 protected:
85  std::unique_ptr<PeriodicTimer> timer_;
86 };
87 
88 template <typename T> class Subscription : public SubscriptionBase {
89 public:
90  typedef std::function<void(const T&)> CallbackT;
91  typedef std::function<void(const msp::Message&)> CallbackM;
92 
97 
105  Subscription(const CallbackT& recv_callback, const CallbackM& send_callback,
106  std::unique_ptr<T>&& io_object, const double& period = 0.0) :
107  recv_callback_(recv_callback),
108  send_callback_(send_callback),
109  io_object_(std::move(io_object)) {
110  if(period > 0.0) {
111  timer_ = std::unique_ptr<PeriodicTimer>(new PeriodicTimer(
112  std::bind(&Subscription<T>::makeRequest, this), period));
113  this->timer_->start();
114  }
115  }
116 
121  virtual void decode(msp::ByteVector& data) const override {
122  io_object_->decode(data);
123  recv_callback_(*io_object_);
124  }
125 
130  void setIoObject(std::unique_ptr<T>&& obj) const {
131  io_object_ = std::move(obj);
132  }
133 
138  const T& getIoObject() const { return *io_object_; }
139 
144  virtual const msp::Message& getMsgObject() const override {
145  return *io_object_;
146  }
147 
152  void setReceiveCallback(const CallbackT& recv_callback) const {
153  recv_callback_ = recv_callback;
154  }
155 
159  virtual void handleResponse() const override {
160  if(recv_callback_) recv_callback_(*io_object_);
161  }
162 
167  void setSendCallback(const CallbackM& send_callback) const {
168  send_callback_ = send_callback;
169  }
170 
174  virtual void makeRequest() const override {
175  if(send_callback_) send_callback_(*io_object_);
176  }
177 
178 protected:
179  CallbackT recv_callback_;
180  CallbackM send_callback_;
181  std::unique_ptr<T> io_object_;
182 };
183 
184 } // namespace client
185 } // namespace msp
186 
187 #endif
std::unique_ptr< T > io_object_
void setReceiveCallback(const CallbackT &recv_callback) const
Sets the callback to be executed on success.
Subscription()
Subscription constructor.
std::function< void(const msp::Message &)> CallbackM
bool start() const
Start the timer for automatic execution.
const T & getIoObject() const
Gets a reference to the IO object.
std::unique_ptr< PeriodicTimer > timer_
void setTimerFrequency(const double &rate_hz)
setTimerFrequency change the update rate of timer
bool hasTimer() const
Checks to see if the timer has been created.
virtual void makeRequest() const override
Calls the send callback if it exists.
void setIoObject(std::unique_ptr< T > &&obj) const
Sets the object used for packing and unpacking data.
virtual void makeRequest() const =0
bool stop() const
Stop the timer&#39;s automatic execution.
bool isAutomatic() const
Checks to see if the subscription fires automatically.
virtual void decode(msp::ByteVector &data) const =0
virtual void handleResponse() const =0
Subscription(const CallbackT &recv_callback, const CallbackM &send_callback, std::unique_ptr< T > &&io_object, const double &period=0.0)
Subscription constructor setting all parameters.
void setTimerPeriod(const double &period_seconds)
setTimerPeriod change the period of the timer
virtual const msp::Message & getMsgObject() const =0
std::function< void(const T &)> CallbackT
void setSendCallback(const CallbackM &send_callback) const
Sets the callback used to send the request.
virtual const msp::Message & getMsgObject() const override
Gets a reference to the internal IO object as a Message.
virtual void decode(msp::ByteVector &data) const override
Virtual method for decoding received data.
virtual void handleResponse() const override
Calls the receive callback if it exists.


msp
Author(s): Christian Rauch
autogenerated on Tue Oct 6 2020 03:39:02