067931ef8f1d40925b17eefd035ff5e95ffd1ed5
[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 /** @brief a basic implementation of ConnectionWrapper
36
37     This is a basic version of a ConnectionWrapper which does not do any fancy
38     error handling or anything, it justs executes the regular calls. Use this
39     wrapper if you only want to use the singleton-feature of T2nSingletonWrapper.
40 */
41 class BasicSocketWrapper : public ConnectionWrapper
42 {
43     protected:
44         socket_type_value socket_type;
45
46         std::string path;
47         std::string server;
48         int port;
49
50         long long connect_timeout_usec;
51         int max_retries;
52
53         std::auto_ptr<socket_client_connection> c;
54
55     public:
56         BasicSocketWrapper(int _port, const std::string& _server="127.0.0.1", 
57             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
58             int _max_retries=socket_client_connection::max_retries_default)
59             : port(_port), server(_server), connect_timeout_usec(_connect_timeout_usec),
60               max_retries(_max_retries), socket_type(tcp_s), ConnectionWrapper()
61             { }
62
63         BasicSocketWrapper(const std::string& _path,
64             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
65             int _max_retries=socket_client_connection::max_retries_default)
66             : path(_path), connect_timeout_usec(_connect_timeout_usec),
67               max_retries(_max_retries), socket_type(unix_s), ConnectionWrapper()
68             { }
69
70         client_connection* get_connection(void);
71
72         bool connection_established(void)
73             { return (c.get() != NULL); }
74
75         void set_logging(std::ostream *_logstream, log_level_values _log_level);
76 };
77
78 /** @brief a wrapper implementing reconnect-then-throw
79
80     This ConnectionWrapper tries to reconnect to the server if something with the connection
81     goes wrong. If even reconnecting max_retries times does not help, an exception is thrown.
82 */
83 class ReconnectSocketWrapper : public BasicSocketWrapper
84 {
85     public:
86         ReconnectSocketWrapper(int _port, const std::string& _server="127.0.0.1", 
87             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
88             int _max_retries=socket_client_connection::max_retries_default)
89             : BasicSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
90             { }
91
92         ReconnectSocketWrapper(const std::string& _path,
93             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
94             int _max_retries=socket_client_connection::max_retries_default)
95             : BasicSocketWrapper(_path,_connect_timeout_usec,_max_retries)
96             { }
97
98         bool handle(command_client* stubBase, boost::function< void() > f);
99 };
100
101 /// a placeholder-client_connection which is closed all the time
102 class dummy_client_connection : public client_connection
103 {
104     private:
105         void real_write(const std::string& data)
106             { }
107
108     public:
109         dummy_client_connection()
110             : client_connection()
111             { close(); }
112
113         bool fill_buffer(long long usec_timeout=-1, long long *usec_timeout_remaining=NULL)
114             { return false; }
115 };
116
117 /** @brief a wrapper implementing reconnect-then-ignore
118
119     This ConnectionWrapper tries to reconnect to the server if something with the connection
120     goes wrong. If even reconnecting max_retries times does not help, the complete t2n-call is
121     ignored. The return value of the call will be created with the default constructor.
122 */
123 class ReconnectIgnoreFailureSocketWrapper : public ReconnectSocketWrapper
124 {
125     private:
126         dummy_client_connection dc;
127
128     public:
129         ReconnectIgnoreFailureSocketWrapper(int _port, const std::string& _server="127.0.0.1", 
130             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
131             int _max_retries=socket_client_connection::max_retries_default)
132             : ReconnectSocketWrapper(_port,_server,_connect_timeout_usec,_max_retries)
133             { }
134
135         ReconnectIgnoreFailureSocketWrapper(const std::string& _path,
136             long long _connect_timeout_usec=socket_client_connection::connect_timeout_usec_default, 
137             int _max_retries=socket_client_connection::max_retries_default)
138             : ReconnectSocketWrapper(_path,_connect_timeout_usec,_max_retries)
139             { }
140
141         client_connection* get_connection(void);
142         bool handle(command_client* stubBase, boost::function< void() > f);
143 };
144
145 }
146
147 #endif