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
33namespace fawkes {
34
35class Logger;
36
38{
39public:
40 OpenPRSMessagePasserProxy(unsigned short tcp_port,
41 const std::string &mp_host,
42 unsigned short mp_port,
43 fawkes::Logger * logger);
45
46private:
47 class Mapping
48 {
49 public:
50 /** Shortcut for shared pointer of session. */
51 typedef std::shared_ptr<Mapping> Ptr;
52 Mapping(boost::asio::io_service &io_service,
53 const std::string & mp_host,
54 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
81 private: // members
82 boost::asio::io_service & io_service_;
83 boost::asio::ip::tcp::resolver resolver_;
84
85 std::string server_host_;
86 unsigned short server_port_;
87 boost::asio::streambuf server_buffer_;
88
89 fawkes::Logger *logger_;
90
91 int server_in_reg_reply_;
92 int server_in_str_len_;
93 int client_in_msg_type_;
94
95 public:
96 int client_prot;
97 std::string client_name;
98 boost::asio::ip::tcp::socket client_socket;
99 boost::asio::ip::tcp::socket server_socket;
100 };
101
102private:
103 void start_accept();
104 void handle_accept(Mapping::Ptr mapping, const boost::system::error_code &error);
105
106private:
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
Interface for logging.
Definition: logger.h:42
Proxy for the OpenPRS server communication.
OpenPRSMessagePasserProxy(unsigned short tcp_port, const std::string &mp_host, unsigned short mp_port, fawkes::Logger *logger)
Constructor.
virtual ~OpenPRSMessagePasserProxy()
Destructor.
Fawkes library namespace.