da972a2d01c57d01553522594728ea32697de12b
[libt2n] / src / socket_wrapper.hxx
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
32 namespace libt2n
33 {
34
35 class 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), ConnectionWrapper()
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), ConnectionWrapper()
62             { }
63
64         client_connection* get_connection(void);
65
66         bool connection_established(void)
67             { return (c.get() != NULL); }
68 };
69
70 class ReconnectSocketWrapper : public BasicSocketWrapper
71 {
72     public:
73         ReconnectSocketWrapper(int _port, const std::string& _server="127.0.0.1", 
74             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
75             int _max_retries=socket_client_connection::max_retries_default)
76             : BasicSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
77             { }
78
79         ReconnectSocketWrapper(const std::string& _path,
80             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
81             int _max_retries=socket_client_connection::max_retries_default)
82             : BasicSocketWrapper(_path,_connect_timeout_usec,_max_retries)
83             { }
84
85         bool handle(command_client* stubBase, boost::function< void() > f);
86 };
87
88 class dummy_client_connection : public client_connection
89 {
90     private:
91         void real_write(const std::string& data)
92             { }
93
94     public:
95         dummy_client_connection()
96             : client_connection()
97             { close(); }
98
99         bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
100             { return false; }
101 };
102
103 class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
104 {
105     private:
106         dummy_client_connection dc;
107
108     public:
109         ReconnectIgnoreFailureSocketWrapper(int _port, const std::string& _server="127.0.0.1", 
110             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
111             int _max_retries=socket_client_connection::max_retries_default)
112             : ReconnectSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
113             { }
114
115         ReconnectIgnoreFailureSocketWrapper(const std::string& _path,
116             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
117             int _max_retries=socket_client_connection::max_retries_default)
118             : ReconnectSocketWrapper(_path,_connect_timeout_usec,_max_retries)
119             { }
120
121         client_connection* get_connection(void);
122         bool handle(command_client* stubBase, boost::function< void() > f);
123 };
124
125 }
126
127 #endif