libt2n: (gerd) basic structure of wrappers done, ignore handler missing, unit tests...
[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),
54 max_retries(_max_retries), socket_type(tcp_s)
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),
61 max_retries(_max_retries), socket_type(unix_s)
62 { }
63
64 client_connection* get_connection(void);
65};
66
67class ReconnectSocketWrapper : public BasicSocketWrapper
68{
69 public:
70 ReconnectSocketWrapper(int _port, const std::string& _server="127.0.0.1",
71 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
72 int _max_retries=socket_client_connection::max_retries_default)
73 : BasicSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
74 { }
75
76 ReconnectSocketWrapper(const std::string& _path,
77 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
78 int _max_retries=socket_client_connection::max_retries_default)
79 : BasicSocketWrapper(_path,_connect_timeout_usec,_max_retries)
80 { }
81
82 void handle(command_client* stubBase, boost::function< void() > f);
83};
84
85
86class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
87{
88 public:
89 ReconnectIgnoreFailureSocketWrapper(int _port, const std::string& _server="127.0.0.1",
90 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
91 int _max_retries=socket_client_connection::max_retries_default)
92 : ReconnectSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
93 { }
94
95 ReconnectIgnoreFailureSocketWrapper(const std::string& _path,
96 long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default,
97 int _max_retries=socket_client_connection::max_retries_default)
98 : ReconnectSocketWrapper(_path,_connect_timeout_usec,_max_retries)
99 { }
100
101 client_connection* get_connection(void);
102 void handle(command_client* stubBase, boost::function< void() > f);
103};
104
105
106
107}
108
109#endif