Fawkes API  Fawkes Development Version
openprs_mp_proxy.h
1 
2 /***************************************************************************
3  * openprs_mp_proxy.h - OpenPRS message passer 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_MP_PROXY_H_
24 #define __PLUGINS_OPENPRS_UTILS_OPENPRS_MP_PROXY_H_
25 
26 #include <core/utils/lockptr.h>
27 
28 #include <boost/asio.hpp>
29 #include <list>
30 #include <string>
31 #include <thread>
32 
33 namespace fawkes {
34 #if 0 /* just to make Emacs auto-indent happy */
35 }
36 #endif
37 
38 class Logger;
39 
41 {
42  public:
43  OpenPRSMessagePasserProxy(unsigned short tcp_port,
44  const std::string &mp_host, unsigned short mp_port,
45  fawkes::Logger *logger);
47 
48  private:
49  class Mapping {
50  public:
51  /** Shortcut for shared pointer of session. */
52  typedef std::shared_ptr<Mapping> Ptr;
53  Mapping(boost::asio::io_service &io_service,
54  const std::string &mp_host, unsigned short mp_port,
55  fawkes::Logger *logger);
56  ~Mapping();
57 
58  void start();
59  bool alive() const;
60  void disconnect();
61 
62  private: // methods
63  void disconnect(const char *where, const char *reason);
64  void handle_resolve(const boost::system::error_code& err,
65  boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
66  void handle_connect(const boost::system::error_code& err);
67  void start_recv_client();
68  void handle_recv_client(const boost::system::error_code &err);
69  void start_recv_server();
70  void handle_recv_server_reg_reply(const boost::system::error_code &err);
71  void handle_recv_server_message_pt(const boost::system::error_code &err);
72  void handle_recv_server_strings_pt(const boost::system::error_code &err);
73 
74  int read_int_from_socket(boost::asio::ip::tcp::socket &socket);
75  std::string read_string_from_socket(boost::asio::ip::tcp::socket &socket);
76  void write_int_to_socket(boost::asio::ip::tcp::socket &socket, int i);
77  void write_string_to_socket(boost::asio::ip::tcp::socket &socket, std::string &str);
78  void write_string_newline_to_socket(boost::asio::ip::tcp::socket &socket,
79  const std::string &str);
80  private: // members
81  boost::asio::io_service &io_service_;
82  boost::asio::ip::tcp::resolver resolver_;
83 
84  std::string server_host_;
85  unsigned short server_port_;
86  boost::asio::streambuf server_buffer_;
87 
88  fawkes::Logger *logger_;
89 
90  int server_in_reg_reply_;
91  int server_in_str_len_;
92  int client_in_msg_type_;
93 
94  public:
95  int client_prot;
96  std::string client_name;
97  boost::asio::ip::tcp::socket client_socket;
98  boost::asio::ip::tcp::socket server_socket;
99 
100  };
101 
102  private:
103  void start_accept();
104  void handle_accept(Mapping::Ptr mapping, const boost::system::error_code &error);
105 
106  private:
107  boost::asio::io_service io_service_;
108  std::thread io_service_thread_;
109  boost::asio::io_service::work io_service_work_;
110  boost::asio::ip::tcp::acceptor acceptor_;
111 
112  std::string mp_host_;
113  unsigned short mp_port_;
114  Logger *logger_;
115 
116  std::list<Mapping::Ptr> mappings_;
117 };
118 
119 } // end namespace fawkes
120 
121 #endif
virtual ~OpenPRSMessagePasserProxy()
Destructor.
Fawkes library namespace.
OpenPRSMessagePasserProxy(unsigned short tcp_port, const std::string &mp_host, unsigned short mp_port, fawkes::Logger *logger)
Constructor.
Proxy for the OpenPRS server communication.
Interface for logging.
Definition: logger.h:34