Fawkes API  Fawkes Development Version
openprs_server_proxy.h
1 
2 /***************************************************************************
3  * openprs_server_proxy.h - OpenPRS server proxy
4  *
5  * Created: Tue Aug 19 16:59:27 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_UTILS_OPENPRS_SERVER_PROXY_H_
24 #define __PLUGINS_OPENPRS_UTILS_OPENPRS_SERVER_PROXY_H_
25 
26 #include <core/utils/lockptr.h>
27 #include <core/utils/lock_list.h>
28 
29 #include <boost/asio.hpp>
30 #include <list>
31 #include <string>
32 #include <thread>
33 
34 namespace fawkes {
35 #if 0 /* just to make Emacs auto-indent happy */
36 }
37 #endif
38 
39 class Logger;
40 
42 {
43  public:
44  OpenPRSServerProxy(unsigned short tcp_port,
45  const std::string &server_host, unsigned short server_port,
46  fawkes::Logger *logger);
47  virtual ~OpenPRSServerProxy();
48 
49  void transmit_command(const std::string &client_name, const std::string &command);
50  void transmit_command_f(const std::string &client_name, const char *format, ...);
51  void transmit_command_v(const std::string &client_name, const char *format, va_list arg);
52 
53  bool has_kernel(const std::string &kernel_name);
54 
55  static int read_int_from_socket(boost::asio::ip::tcp::socket &socket);
56  static std::string read_string_from_socket(boost::asio::ip::tcp::socket &socket);
57  static void write_int_to_socket(boost::asio::ip::tcp::socket &socket, int i);
58  static void write_string_to_socket(boost::asio::ip::tcp::socket &socket, const std::string &str);
59  static void write_string_newline_to_socket(boost::asio::ip::tcp::socket &socket, const std::string &str);
60 
61  private:
62  class Mapping {
63  public:
64  /** Shortcut for shared pointer of session. */
65  typedef std::shared_ptr<Mapping> Ptr;
66  Mapping(boost::asio::io_service &io_service,
67  const std::string &server_host, unsigned short server_port,
68  fawkes::Logger *logger);
69  ~Mapping();
70 
71  void start();
72  bool alive() const;
73  void disconnect();
74 
75  void transmit_command(const std::string &command);
76 
77  private: // methods
78  void handle_resolve(const boost::system::error_code& err,
79  boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
80  void handle_connect(const boost::system::error_code& err);
81  void start_recv_client();
82  void start_recv_server();
83  void handle_recv_client(const boost::system::error_code &err);
84  void handle_recv_server(const boost::system::error_code &err);
85 
86  private: // members
87  boost::asio::io_service &io_service_;
88  boost::asio::ip::tcp::resolver resolver_;
89 
90  std::string server_host_;
91  unsigned short server_port_;
92 
93  int client_in_num_completions_;
94  boost::asio::streambuf server_buffer_;
95 
96  fawkes::Logger *logger_;
97 
98  public:
99  std::string client_name;
100  boost::asio::ip::tcp::socket client_socket;
101  boost::asio::ip::tcp::socket server_socket;
102 
103  };
104 
105  private:
106  void start_accept();
107  void handle_accept(Mapping::Ptr mapping, const boost::system::error_code &error);
108  Mapping::Ptr find_mapping(const std::string &client_name);
109 
110  private:
111  boost::asio::io_service io_service_;
112  std::thread io_service_thread_;
113  boost::asio::io_service::work io_service_work_;
114  boost::asio::ip::tcp::acceptor acceptor_;
115 
116  std::string server_host_;
117  unsigned short server_port_;
118  Logger *logger_;
119 
121 };
122 
123 } // end namespace fawkes
124 
125 #endif
void transmit_command_v(const std::string &client_name, const char *format, va_list arg)
Transmit a command to an OpenPRS kernel.
Proxy for the OpenPRS server communication.
static void write_string_to_socket(boost::asio::ip::tcp::socket &socket, const std::string &str)
Write a string to a given socket.
Fawkes library namespace.
static void write_string_newline_to_socket(boost::asio::ip::tcp::socket &socket, const std::string &str)
Write a string followed by a newline character to a given socket.
void transmit_command(const std::string &client_name, const std::string &command)
Transmit a command to an OpenPRS kernel.
virtual ~OpenPRSServerProxy()
Destructor.
static void write_int_to_socket(boost::asio::ip::tcp::socket &socket, int i)
Write an int to a given socket.
static std::string read_string_from_socket(boost::asio::ip::tcp::socket &socket)
Read a string from a given socket.
static int read_int_from_socket(boost::asio::ip::tcp::socket &socket)
Read an int from a given socket.
bool has_kernel(const std::string &kernel_name)
Check if a kernel connected to the proxy.
OpenPRSServerProxy(unsigned short tcp_port, const std::string &server_host, unsigned short server_port, fawkes::Logger *logger)
Constructor.
void transmit_command_f(const std::string &client_name, const char *format,...)
Transmit a command to an OpenPRS kernel.
Interface for logging.
Definition: logger.h:34