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/lock_list.h>
27#include <core/utils/lockptr.h>
28
29#include <boost/asio.hpp>
30#include <list>
31#include <string>
32#include <thread>
33
34namespace fawkes {
35
36class Logger;
37
39{
40public:
41 OpenPRSServerProxy(unsigned short tcp_port,
42 const std::string &server_host,
43 unsigned short server_port,
44 fawkes::Logger * logger);
45 virtual ~OpenPRSServerProxy();
46
47 void transmit_command(const std::string &client_name, const std::string &command);
48 void transmit_command_f(const std::string &client_name, const char *format, ...);
49 void transmit_command_v(const std::string &client_name, const char *format, va_list arg);
50
51 bool has_kernel(const std::string &kernel_name);
52
53 static int read_int_from_socket(boost::asio::ip::tcp::socket &socket);
54 static std::string read_string_from_socket(boost::asio::ip::tcp::socket &socket);
55 static void write_int_to_socket(boost::asio::ip::tcp::socket &socket, int i);
56 static void write_string_to_socket(boost::asio::ip::tcp::socket &socket, const std::string &str);
57 static void write_string_newline_to_socket(boost::asio::ip::tcp::socket &socket,
58 const std::string & str);
59
60private:
61 class Mapping
62 {
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,
68 unsigned short server_port,
69 fawkes::Logger * logger);
70 ~Mapping();
71
72 void start();
73 bool alive() const;
74 void disconnect();
75
76 void transmit_command(const std::string &command);
77
78 private: // methods
79 void handle_resolve(const boost::system::error_code & err,
80 boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
81 void handle_connect(const boost::system::error_code &err);
82 void start_recv_client();
83 void start_recv_server();
84 void handle_recv_client(const boost::system::error_code &err);
85 void handle_recv_server(const boost::system::error_code &err);
86
87 private: // members
88 boost::asio::io_service & io_service_;
89 boost::asio::ip::tcp::resolver resolver_;
90
91 std::string server_host_;
92 unsigned short server_port_;
93
94 int client_in_num_completions_;
95 boost::asio::streambuf server_buffer_;
96
97 fawkes::Logger *logger_;
98
99 public:
100 std::string client_name;
101 boost::asio::ip::tcp::socket client_socket;
102 boost::asio::ip::tcp::socket server_socket;
103 };
104
105private:
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
110private:
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
Interface for logging.
Definition: logger.h:42
Proxy for the OpenPRS server communication.
void transmit_command_f(const std::string &client_name, const char *format,...)
Transmit a command to an OpenPRS kernel.
virtual ~OpenPRSServerProxy()
Destructor.
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.
static void write_int_to_socket(boost::asio::ip::tcp::socket &socket, int i)
Write an int to a given socket.
static int read_int_from_socket(boost::asio::ip::tcp::socket &socket)
Read an int from a given socket.
static std::string read_string_from_socket(boost::asio::ip::tcp::socket &socket)
Read a string from a given socket.
void transmit_command(const std::string &client_name, const std::string &command)
Transmit a command to an OpenPRS kernel.
void transmit_command_v(const std::string &client_name, const char *format, va_list arg)
Transmit a command to an OpenPRS kernel.
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.
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.