dpyMwClient.h
Go to the documentation of this file.
1 
3 #pragma once
4 #include <boost/shared_ptr.hpp>
5 #include <boost/asio.hpp>
6 #include <boost/make_shared.hpp>
7 #include <boost/any.hpp>
8 #include <boost/system/error_code.hpp>
9 #include <boost/function.hpp>
10 #include <boost/enable_shared_from_this.hpp>
11 #include <boost/chrono.hpp>
12 #include <boost/asio/waitable_timer_service.hpp>
13 #include <boost/thread.hpp>
14 
15 #ifdef TESTING
16 #include "gtest/gtest.h"
17 #endif
18 
19 // The forward reference.
20 class DpyMwDealer;
21 class DpyMwSubscriber;
22 
24 enum class ServiceState {
25  UNKNOWN ,
26  ACTIVE ,
28 };
29 
36 typedef boost::function<void(const std::string&, boost::any, int)> f_proto_message_client;
37 
43 typedef std::map<uint64_t, f_proto_message_client> map_proto_register_client;
44 
50 typedef boost::function<void(const bool &alive)> f_hearbeat_handler;
51 
52 typedef boost::asio::basic_waitable_timer<boost::chrono::steady_clock> steady_timer;
53 
59 class DpyMwClient: public boost::enable_shared_from_this<DpyMwClient>
60 {
64  struct Global_config
65  {
66  int time_count;
67  boost::any handler;
68  int response_type;
69  };
70 
71 private:
72  std::condition_variable mCv;
73  boost::asio::io_service mIo;
74  bool m_working;
75  steady_timer m_dealerTimer;
76  steady_timer m_heartbeatTimer;
77  int timerCounter;
78  boost::shared_ptr<DpyMwSubscriber> m_subscriber;
79  boost::shared_ptr<DpyMwDealer> m_dealer;
80  int lastMessageID = 0;
81  std::map<int, Global_config> m_map_dealerHandlers;
82  std::map<int, std::vector<boost::any>> m_map_subscriberHandlers;
84  f_hearbeat_handler mHeartbeatHandler;
85 
89  boost::thread bt;
90 
91 protected:
96 
97  void sendRequest(int requestType, int responseType, std::string content, boost::any handler, int ms_timeout = 2000);
101  virtual void RegisterCallbacks()=0;
105  std::mutex m_map_mutex;
106 public:
107  DpyMwClient(std::string& ip, int dport, int sport);
108  virtual ~DpyMwClient();
109  void start();
110  void stop();
111  void subscribe(int messageType, boost::any handler);
112  void unsubscribe(int messageType);
113  bool isAlive();
114  void monitorServiceAvailability_S(const f_hearbeat_handler& handler);
115  void monitorServiceAvailability_U();
116 
117 
118 private:
119  void dealer_handler(const std::string &msg);
120  void dealerTimer_handler(const boost::system::error_code& e);
121  void heartbeatTimer_handler(const boost::system::error_code& e);
122  void subscriber_handler(const std::string &msg);
123 
124 #ifdef TESTING
125 public:
126  FRIEND_TEST(Client_dealer_test, Request_timeout);
127  FRIEND_TEST(Client_dealer_test, Request_answered);
128 
129 #endif
130 };
Service is active.
Subscriber class.
Definition: dpyMwSubscriber.h:27
Service is not active.
boost::asio::basic_waitable_timer< boost::chrono::steady_clock > steady_timer
Typedef to use a steady timer.
Definition: dpyMwClient.h:52
std::map< uint64_t, f_proto_message_client > map_proto_register_client
Map to keep functions with a type of message associated.
Definition: dpyMwClient.h:43
boost::function< void(const std::string &, boost::any, int)> f_proto_message_client
Prototype of the function to call depending on the message received.
Definition: dpyMwClient.h:36
std::mutex m_map_mutex
Definition: dpyMwClient.h:105
Dealer class.
Definition: dpyMwDealer.h:27
map_proto_register_client m_register_map
Definition: dpyMwClient.h:95
Unknown service state.
boost::function< void(const bool &alive)> f_hearbeat_handler
Prototype of the handler function used to keep track of service availability.
Definition: dpyMwClient.h:50
Manages dealer and susbriber.
Definition: dpyMwClient.h:59
ServiceState
Service states.
Definition: dpyMwClient.h:24