VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
service_discovery_processor.h
Go to the documentation of this file.
1 // Copyright (c) 2018, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
11 #ifndef _UGCS_VSM_SERVICE_DISCOVERY_PROCESSOR_H_
12 #define _UGCS_VSM_SERVICE_DISCOVERY_PROCESSOR_H_
13 
16 #include <ugcs/vsm/callback.h>
17 #include <set>
18 
19 namespace ugcs {
20 namespace vsm {
21 
23 {
25 public:
27  Socket_address::Ptr muticast_adress =
28  Socket_address::Create(DEFAULT_DISCOVERY_ADDRESS, DEFAULT_DISCOVERY_PORT));
29 
30  virtual ~Service_discovery_processor();
31 
50 
53  Make_detection_handler,
54  (std::string, std::string, std::string, std::string, bool),
55  ("", "", "", "", true));
56 
69  void
70  Advertise_service(const std::string& type, const std::string& name, const std::string& location);
71 
75  void
76  Unadvertise_service(const std::string& type, const std::string& name, const std::string& location);
77 
83  void
85  const std::string &type,
86  Detection_handler handler,
87  Request_processor::Ptr context);
88 
93  void
94  Search_for_service(const std::string& type);
95 
99  void
100  Unsubscribe_from_service(const std::string& type);
101 
103  template <typename... Args>
104  static Ptr
105  Get_instance(Args &&... args)
106  {
107  return singleton.Get_instance(std::forward<Args>(args)...);
108  }
109 
110 private:
111  static std::string DEFAULT_DISCOVERY_ADDRESS;
112  static std::string DEFAULT_DISCOVERY_PORT;
113  static std::string LOOPBACK_BROADCAST_ADDRESS;
114  static std::string SEARCH_METHOD_STRING;
115  static std::string NOTIFY_METHOD_STRING;
116  // token to replace with interface IP in notify message and discovery response
117  static std::string LOCAL_ADDRESS_IDENTIFIER;
118  // used to distinguish loopback socket in reader callback
119  static std::string LOOPBACK_IDENTIFIER;
120  // used to distinguish multicast socket in reader callback
121  static std::string MC_IDENTIFIER;
122 
123  // Randomly generated string to uniquely identify service instance.
124  // Used by service locator to distinguish between multiple locations
125  // for the same service instance.
126  std::string my_instance_identifier;
127 
130 
131  // Service type, name, location
132  typedef std::tuple<std::string, std::string, std::string> Service_info;
133 
134  // Sender socket with current io operation.
135  typedef std::pair<Operation_waiter, Socket_processor::Stream::Ref> My_socket;
136 
137  // Multicasts listener address
138  Socket_address::Ptr multicast_adress;
139 
140  // loopback detection requests are sent to this address so they are
141  // received by all locally listening processes.
142  Socket_address::Ptr loopback_broadcast_adress;
143 
144  // Sender and reader callbacks executing in this thread.
145  Request_worker::Ptr worker;
146 
147  // Listener socket bound on 0.0.0.0 on multicast_port.
148  My_socket receiver;
149 
150  // One socket per interface indexed by ip address string.
151  // Bound to that interface IP.
152  // Used to send discovery packets.
153  // Incoming unicast responses are read from this socket.
154  std::map<std::string, My_socket> sender_sockets;
155 
156  // Loopback interface handled separately. Bound to 127.0.0.1:0
157  My_socket sender_loopback;
158 
159  // advertised services
160  std::set<Service_info> my_services;
161 
162  // Services to listen for.
163  // maps service type into user specified callback
164  std::map<std::string, std::pair<Detection_handler, Request_processor::Ptr>> subscribed_services;
165 
166  // look for new interfaces each 5 seconds.
167  Timer_processor::Timer::Ptr interface_checker_timer;
168 
169  // Send notifications each 10 seconds.
170  Timer_processor::Timer::Ptr notify_timer;
171 
172  void
173  On_read(
174  Io_buffer::Ptr buffer,
175  Io_result result,
176  Socket_address::Ptr addr,
177  std::string stream_id);
178 
179  bool
180  On_timer();
181 
182  bool
183  On_notify_timer();
184 
185  void
186  On_sender_bound(Socket_processor::Stream::Ref l, Io_result);
187 
188  void
189  Send_notify(
191  Socket_address::Ptr dest_addr,
192  const std::string& type,
193  const std::string& name,
194  const std::string& location,
195  bool alive);
196 
197  // Sends notifications about all services on given interface
198  void
199  Send_notify_all_services(
201  Socket_address::Ptr dest_addr);
202 
203  void
204  Send_response(
205  Socket_address::Ptr addr,
206  const std::string& type,
207  const std::string& name,
208  const std::string& location);
209 
210  void
211  Send_msearch(
213  Socket_address::Ptr dest_addr,
214  const std::string& type);
215 
218  virtual void
219  On_enable() override;
220 
221  // return true if already activated
222  // false if was not activated
223  bool
224  Activate();
225 
226  // close all sockets when there are no subcriptions or
227  void
228  Deactivate_if_no_services();
229 
230  void
231  On_advertise(
232  const std::string type,
233  const std::string name,
234  const std::string location,
235  Request::Ptr request);
236 
237  void
238  On_unadvertise(
239  const std::string type,
240  const std::string name,
241  const std::string location,
242  Request::Ptr request);
243 
244  void
245  On_subscribe_for_service(
246  const std::string type,
247  Detection_handler handler,
248  Request_processor::Ptr context,
249  Request::Ptr request);
250 
251  void
252  On_unsubscribe_from_service(
253  const std::string type,
254  Request::Ptr request);
255 
256  void
257  On_search_for_service(
258  const std::string type,
259  Request::Ptr request);
260 
261  void
262  On_disable_impl(Request::Ptr request);
263 
266  virtual void
267  On_disable() override;
268 
269  void
270  Schedule_read(std::string id);
271 
272  static bool
273  Has_location_string(const std::string& loc);
274 
275  static std::string
276  Build_location_string(const std::string& loc, Socket_address::Ptr local_addr);
277 };
278 
279 } /* namespace vsm */
280 } /* namespace ugcs */
281 
282 #endif /* _UGCS_VSM_SERVICE_DISCOVERY_PROCESSOR_H_ */
Request worker.
Callback_proxy< void, std::string, std::string, std::string, std::string, bool > Detection_handler
Detector function.
Definition: service_discovery_processor.h:49
static Ptr Get_instance(Args &&...args)
Get global or create new object instance.
Definition: service_discovery_processor.h:105
#define DEFINE_CALLBACK_BUILDER(__name, __types, __values)
Define callback builder function.
Definition: callback.h:42
Definition: service_discovery_processor.h:22
std::shared_ptr< Service_discovery_processor > Ptr
Pointer type.
Definition: service_discovery_processor.h:24
Io_result
Result of I/O operation.
Definition: io_stream.h:37
void Search_for_service(const std::string &type)
Sends out the M-SEARCH on all available interfaces.
Generic callback which can be used to define and create an instance of an abstract callable operation...
std::shared_ptr< Request > Ptr
Pointer type.
Definition: request_container.h:38
std::shared_ptr< Request_worker > Ptr
Pointer type.
Definition: request_worker.h:25
void Advertise_service(const std::string &type, const std::string &name, const std::string &location)
Sends out the NOTIFY ssdp:alive on all available interfaces.
Request execution context.
Definition: request_context.h:24
Socket processor.
Helper class for proxying callback invocation.
Definition: callback.h:699
Generic container for queued requests.
Definition: request_container.h:30
void Subscribe_for_service(const std::string &type, Detection_handler handler, Request_processor::Ptr context)
Sends out the M-SEARCH on all available interfaces.
std::shared_ptr< Request_context > Ptr
Pointer type.
Definition: request_context.h:25
std::shared_ptr< Io_buffer > Ptr
Pointer type.
Definition: io_buffer.h:34
void Unsubscribe_from_service(const std::string &type)
Remove service type from subscription list.
void Unadvertise_service(const std::string &type, const std::string &name, const std::string &location)
Sends out the NOTIFY ssdp:byebye on all available interfaces.
#define DEFINE_COMMON_CLASS(__class_name,...)
Use this macro to define some common attributes for a class.
Definition: utils.h:25
std::shared_ptr< Timer > Ptr
Pointer type.
Definition: timer_processor.h:46
Helper class for implementing singletons.
Definition: singleton.h:69