-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxpl_application_service.h
69 lines (57 loc) · 2.28 KB
/
xpl_application_service.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Copyright (c) 2013, 2014 Danny Havenith
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef XPL_APPLICATION_SERVICE_H_
#define XPL_APPLICATION_SERVICE_H_
#include <memory>
#include <functional>
#include <string>
#include <boost/system/error_code.hpp>
namespace xpl
{
class message;
/// This class implements a generic xpl application service.
/// This class will, when the run() member function is called automatically start to broadcast
/// UDP heartbeat messages and will listen for UDP xpl messages. Whenever an xpl
/// message (command, status or trigger) is received, this class will dispatch the message
/// to any registered handler for that message.
/// Message handlers may be registered by calling the register_command(), register_status() or
/// register_trigger() function.
/// The send() member function can be used to send xpl messages through this server.
class application_service
{
public:
application_service( const std::string &application_id,
const std::string &version_string);
~application_service();
void run();
/// returns whether this service has received messages from an xpl hub.
bool is_connected() const {return connected;}
using handler = std::function<void ( const message &)>;
void register_command( const std::string &schema, handler h);
void register_status( const std::string &schema, handler h);
void register_trigger( const std::string &schema, handler h);
void send( message m);
void send_termination_message();
private:
void discovery_heartbeat( const boost::system::error_code& e, unsigned int counter);
void heartbeat( const boost::system::error_code& e);
void send_heartbeat_message( bool final = false);
unsigned int get_listening_port() const;
void start_read();
void handle_message( const message &m);
struct impl;
impl& get_impl();
const impl& get_impl() const;
std::unique_ptr<impl> pimpl;
bool connected{ false};
const std::string application_id;
const std::string version_string;
static const size_t buffer_size = 512;
char receive_buffer[buffer_size];
};
}
#endif /* XPL_APPLICATION_SERVICE_H_ */