VSM C++ SDK
Vehicle Specific Modules SDK
service_discovery_processor.h
Go to the documentation of this file.
1 // Copyright (c) 2015, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
11 #ifndef SRC_SSDP_PROCESSOR_H_
12 #define SRC_SSDP_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 private:
110 
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.
168 
169  void
170  On_read(
171  Io_buffer::Ptr buffer,
172  Io_result result,
173  Socket_address::Ptr addr,
174  std::string stream_id);
175 
176  bool
177  On_timer();
178 
179  void
180  On_sender_bound(Socket_processor::Stream::Ref l, Io_result);
181 
182  void
183  Send_notify(
185  Socket_address::Ptr dest_addr,
186  const std::string& type,
187  const std::string& name,
188  const std::string& location,
189  bool alive);
190 
191  void
192  Send_response(
193  Socket_address::Ptr addr,
194  const std::string& type,
195  const std::string& name,
196  const std::string& location);
197 
198  void
199  Send_msearch(
201  Socket_address::Ptr dest_addr,
202  const std::string& type);
203 
206  virtual void
207  On_enable() override;
208 
209  // return true if already activated
210  // false if was not activated
211  bool
212  Activate();
213 
214  // close all sockets when there are no subcriptions or
215  void
216  Deactivate_if_no_services();
217 
218  void
219  On_advertise(
220  const std::string type,
221  const std::string name,
222  const std::string location,
223  Request::Ptr request);
224 
225  void
226  On_unadvertise(
227  const std::string type,
228  const std::string name,
229  const std::string location,
230  Request::Ptr request);
231 
232  void
233  On_subscribe_for_service(
234  const std::string type,
235  Detection_handler handler,
236  Request_processor::Ptr context,
237  Request::Ptr request);
238 
239  void
240  On_unsubscribe_from_service(
241  const std::string type,
242  Request::Ptr request);
243 
244  void
245  On_search_for_service(
246  const std::string type,
247  Request::Ptr request);
248 
249  void
250  On_disable_impl(Request::Ptr request);
251 
254  virtual void
255  On_disable() override;
256 
257  void
258  Schedule_read(std::string id);
259 
260  static bool
261  Has_location_string(const std::string& loc);
262 
263  static std::string
264  Build_location_string(const std::string& loc, Socket_address::Ptr local_addr);
265 };
266 
267 } /* namespace vsm */
268 } /* namespace ugcs */
269 
270 #endif /* SRC_SSDP_PROCESSOR_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
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:25
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:691
Generic container for queued requests.
Definition: request_container.h:31
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