libt2n: (gerd) add example for wrapper
[libt2n] / src / socket_wrapper.hxx
CommitLineData
ffbbf9ab
GE
1/***************************************************************************
2 * Copyright (C) 2008 by Gerd v. Egidy *
3 * gve@intra2net.com *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Lesser General Public License version *
7 * 2.1 as published by the Free Software Foundation. *
8 * *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
19
20#ifndef __LIBT2N_SOCKET_WRAPPER
21#define __LIBT2N_SOCKET_WRAPPER
22
23#include <functional>
24#include <string>
25
26#include <client.hxx>
27#include <command_client.hxx>
28#include <types.hxx>
29#include <client_wrapper.hxx>
30#include <socket_client.hxx>
31
32namespace libt2n
33{
34
35class BasicSocketWrapper : public ConnectionWrapper
36{
37 protected:
38 socket_type_value socket_type;
39
40 std::string path;
41 std::string server;
42 int port;
43
44 long long connect_timeout_usec;
45 int max_retries;
46
47 std::auto_ptr<socket_client_connection> c;
48
49 public:
50 BasicSocketWrapper(int _port, const std::string& _server="127.0.0.1",
51 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
52 int _max_retries=socket_client_connection::max_retries_default)
53 : port(_port), server(_server), connect_timeout_usec(_connect_timeout_usec),
e1614a6d 54 max_retries(_max_retries), socket_type(tcp_s), ConnectionWrapper()
ffbbf9ab
GE
55 { }
56
57 BasicSocketWrapper(const std::string& _path,
58 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
59 int _max_retries=socket_client_connection::max_retries_default)
60 : path(_path), connect_timeout_usec(_connect_timeout_usec),
e1614a6d 61 max_retries(_max_retries), socket_type(unix_s), ConnectionWrapper()
ffbbf9ab
GE
62 { }
63
64 client_connection* get_connection(void);
fb3345ad
GE
65
66 bool connection_established(void)
67 { return (c.get() != NULL); }
2a956e65
GE
68
69 void set_logging(std::ostream *_logstream, log_level_values _log_level);
ffbbf9ab
GE
70};
71
72class ReconnectSocketWrapper : public BasicSocketWrapper
73{
74 public:
75 ReconnectSocketWrapper(int _port, const std::string& _server="127.0.0.1",
76 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
77 int _max_retries=socket_client_connection::max_retries_default)
78 : BasicSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
79 { }
80
81 ReconnectSocketWrapper(const std::string& _path,
82 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
83 int _max_retries=socket_client_connection::max_retries_default)
84 : BasicSocketWrapper(_path,_connect_timeout_usec,_max_retries)
85 { }
86
fb3345ad 87 bool handle(command_client* stubBase, boost::function< void() > f);
ffbbf9ab
GE
88};
89
fb3345ad
GE
90class dummy_client_connection : public client_connection
91{
92 private:
93 void real_write(const std::string& data)
94 { }
95
96 public:
97 dummy_client_connection()
98 : client_connection()
99 { close(); }
100
101 bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
102 { return false; }
103};
ffbbf9ab
GE
104
105class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
106{
fb3345ad
GE
107 private:
108 dummy_client_connection dc;
109
ffbbf9ab
GE
110 public:
111 ReconnectIgnoreFailureSocketWrapper(int _port, const std::string& _server="127.0.0.1",
112 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
113 int _max_retries=socket_client_connection::max_retries_default)
114 : ReconnectSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
115 { }
116
117 ReconnectIgnoreFailureSocketWrapper(const std::string& _path,
118 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
119 int _max_retries=socket_client_connection::max_retries_default)
120 : ReconnectSocketWrapper(_path,_connect_timeout_usec,_max_retries)
121 { }
122
123 client_connection* get_connection(void);
fb3345ad 124 bool handle(command_client* stubBase, boost::function< void() > f);
ffbbf9ab
GE
125};
126
ffbbf9ab
GE
127}
128
129#endif