Fawkes API  Fawkes Development Version
openprs_comm.h
1 
2 /***************************************************************************
3  * openprs_comm.h - OpenPRS communication wrapper for Fawkes
4  *
5  * Created: Mon Aug 18 14:33:55 2014
6  * Copyright 2014 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version. A runtime exception applies to
13  * this software (see LICENSE.GPL_WRE file mentioned below for details).
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
21  */
22 
23 #ifndef __PLUGINS_OPENPRS_ASPECT_OPENPRS_COMM_H_
24 #define __PLUGINS_OPENPRS_ASPECT_OPENPRS_COMM_H_
25 
26 #include <vector>
27 #include <string>
28 #include <thread>
29 
30 #include <boost/asio.hpp>
31 #include <boost/signals2.hpp>
32 
33 namespace fawkes {
34 #if 0 /* just to make Emacs auto-indent happy */
35 }
36 #endif
37 
38 class OpenPRSServerProxy;
39 class Logger;
40 
42 {
43  public:
44  OpenPRSComm(const char *local_name, const char *hostname, unsigned short port,
45  OpenPRSServerProxy *server_proxy, Logger *logger = NULL);
46  virtual ~OpenPRSComm();
47 
48  /** Get OpenPRS local name.
49  * @return OpenPRS local name */
50  const std::string & name() const
51  { return name_; }
52 
53  void send_message(const char *recipient, const char *message);
54  void broadcast_message(const char *message);
55  void multicast_message(std::vector<const char *> &recipients, const char *message);
56 
57  void send_message(const std::string &recipient, const std::string &message);
58  void broadcast_message(const std::string &message);
59  void multicast_message(const std::vector<std::string> &recipients, const std::string &message);
60 
61  void send_message_f(const std::string &recipient, const char *format, ...);
62  void broadcast_message_f(const char *format, ...);
63  void multicast_message_f(const std::vector<std::string> &recipients, const char *format, ...);
64 
65  void transmit_command(const char *recipient, const char *message);
66  void transmit_command(const std::string &recipient, const std::string &message);
67  void transmit_command_f(const std::string &recipient, const char *format, ...);
68 
69  /** Boost signal for a received message. */
70  typedef
71  boost::signals2::signal<void (std::string, std::string)>
73 
74  /** Signal that is invoked when a message has been received.
75  * @return signal
76  */
78  { return sig_rcvd_; }
79 
80  private: // methods
81  void start_recv();
82  void handle_recv(const boost::system::error_code &err);
83  std::string read_string_from_socket(boost::asio::posix::stream_descriptor &socket);
84 
85 
86  private: // members
87  const std::string name_;
88  int mp_socket_;
89  OpenPRSServerProxy *server_proxy_;
90  Logger *logger_;
91 
92  boost::asio::io_service io_service_;
93  std::thread io_service_thread_;
94  boost::asio::io_service::work io_service_work_;
95  boost::asio::posix::stream_descriptor sd_mp_socket_;
96 
97  signal_msg_rcvd_type sig_rcvd_;
98 };
99 
100 } // end namespace fawkes
101 
102 #endif
virtual ~OpenPRSComm()
Destructor.
Proxy for the OpenPRS server communication.
OpenPRS communication wrapper.
Definition: openprs_comm.h:41
Fawkes library namespace.
boost::signals2::signal< void(std::string, std::string)> signal_msg_rcvd_type
Boost signal for a received message.
Definition: openprs_comm.h:72
void multicast_message_f(const std::vector< std::string > &recipients, const char *format,...)
Send a message to multiple OpenPRS kernel.
void transmit_command(const char *recipient, const char *message)
Transmit a command to an OpenPRS kernel.
signal_msg_rcvd_type & signal_msg_rcvd()
Signal that is invoked when a message has been received.
Definition: openprs_comm.h:77
void broadcast_message(const char *message)
Send a message to all OpenPRS kernels.
void send_message_f(const std::string &recipient, const char *format,...)
Send a formatted message to an OpenPRS kernel.
const std::string & name() const
Get OpenPRS local name.
Definition: openprs_comm.h:50
void broadcast_message_f(const char *format,...)
Send a formatted message to all OpenPRS kernels.
void transmit_command_f(const std::string &recipient, const char *format,...)
Transmit a command to an OpenPRS kernel.
void multicast_message(std::vector< const char *> &recipients, const char *message)
Send a message to multiple OpenPRS kernel.
void send_message(const char *recipient, const char *message)
Send a message to an OpenPRS kernel.
OpenPRSComm(const char *local_name, const char *hostname, unsigned short port, OpenPRSServerProxy *server_proxy, Logger *logger=NULL)
Constructor.
Interface for logging.
Definition: logger.h:34