Commit | Line | Data |
---|---|---|
7087e187 GE |
1 | /*************************************************************************** |
2 | * Copyright (C) 2006 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 | #include <string> | |
21 | #include <sstream> | |
22 | ||
23 | #include <boost/archive/binary_oarchive.hpp> | |
24 | #include <boost/archive/binary_iarchive.hpp> | |
25 | #include <boost/archive/xml_oarchive.hpp> | |
26 | #include <boost/archive/xml_iarchive.hpp> | |
27 | #include <boost/serialization/serialization.hpp> | |
7087e187 | 28 | |
8104c8f7 GE |
29 | #include <boost/bind.hpp> |
30 | ||
7087e187 GE |
31 | #include "command_client.hxx" |
32 | ||
8104c8f7 GE |
33 | #ifdef HAVE_CONFIG_H |
34 | #include <config.h> | |
35 | #endif | |
36 | ||
7087e187 GE |
37 | using namespace std; |
38 | ||
39 | namespace libt2n | |
40 | { | |
41 | ||
fb3345ad | 42 | command_client::command_client(client_connection* _c, long long _command_timeout_usec, long long _hello_timeout_usec) |
8104c8f7 GE |
43 | : c(_c) |
44 | { | |
45a2ebc9 GE |
45 | command_timeout_usec=_command_timeout_usec; |
46 | hello_timeout_usec=_hello_timeout_usec; | |
47 | ||
8104c8f7 | 48 | // for reconnects |
fb3345ad | 49 | c->add_callback(new_connection,bind(&command_client::read_hello, boost::ref(*this))); |
8104c8f7 | 50 | |
fb3345ad GE |
51 | // don't expect hello from an always closed connection (like dummy_client_connection) |
52 | if (!c->is_closed()) | |
53 | read_hello(); | |
8104c8f7 GE |
54 | } |
55 | ||
af84dfb5 GE |
56 | /** @brief replace the connection currently in use with a new one |
57 | ||
fb3345ad | 58 | @param _c pointer to the new connection |
af84dfb5 GE |
59 | |
60 | @note the old connection must still be valid when this method is called, | |
61 | it can safely be deleted after this method returned | |
62 | ||
63 | @note all callbacks registered on the old connection will be copied over | |
64 | to the new one | |
65 | */ | |
fb3345ad | 66 | void command_client::replace_connection(client_connection* _c) |
af84dfb5 GE |
67 | { |
68 | // copy all callbacks registered on the old connection | |
69 | for(callback_event_type e=static_cast<callback_event_type>(0); | |
70 | e < __events_end; | |
71 | e=static_cast<callback_event_type>(static_cast<int>(e)+1)) | |
72 | { | |
fb3345ad | 73 | list<boost::function<void ()> > evcb=c->get_callback_list(e); |
af84dfb5 GE |
74 | |
75 | for (list<boost::function<void ()> >::iterator i=evcb.begin(); i != evcb.end(); i++) | |
fb3345ad | 76 | _c->add_callback(e,*i); |
af84dfb5 GE |
77 | } |
78 | ||
79 | // replace the connection | |
80 | c=_c; | |
81 | ||
82 | read_hello(); | |
83 | } | |
84 | ||
45a2ebc9 | 85 | std::string command_client::read_packet(const long long &usec_timeout) |
8104c8f7 | 86 | { |
8104c8f7 | 87 | string resultpacket; |
45a2ebc9 GE |
88 | bool got_packet=false; |
89 | long long my_timeout=usec_timeout; | |
fb3345ad GE |
90 | while(!(got_packet=c->get_packet(resultpacket)) && my_timeout > 0 && !c->is_closed()) |
91 | c->fill_buffer(my_timeout,&my_timeout); | |
8104c8f7 | 92 | |
45a2ebc9 GE |
93 | if (!got_packet) |
94 | throw t2n_transfer_error("timeout exceeded"); | |
95 | ||
96 | return resultpacket; | |
97 | } | |
98 | ||
99 | void command_client::read_hello() | |
100 | { | |
b2ba0928 GE |
101 | string resultpacket; |
102 | bool got_packet=false; | |
103 | long long my_timeout=hello_timeout_usec; | |
fb3345ad | 104 | while(!(got_packet=c->get_packet(resultpacket)) && my_timeout > 0 && !c->is_closed()) |
b2ba0928 | 105 | { |
fb3345ad | 106 | c->fill_buffer(my_timeout,&my_timeout); |
b2ba0928 | 107 | |
fb3345ad | 108 | c->peek_packet(resultpacket); |
b2ba0928 GE |
109 | check_hello(resultpacket); // will throw before timeout if wrong data received |
110 | } | |
111 | ||
112 | if (!got_packet) | |
113 | throw t2n_transfer_error("timeout exceeded"); | |
114 | ||
115 | if (!check_hello(resultpacket)) | |
116 | throw t2n_version_mismatch("illegal hello received (incomplete): "+resultpacket); | |
117 | } | |
118 | ||
119 | bool command_client::check_hello(const string& hellostr) | |
120 | { | |
121 | istringstream hello(hellostr); | |
8104c8f7 | 122 | |
b2ba0928 GE |
123 | char chk; |
124 | ||
125 | if (hello.read(&chk,1)) | |
126 | { | |
127 | if (chk != 'T') | |
128 | throw t2n_version_mismatch("illegal hello received (T2N)"); | |
129 | } | |
130 | else | |
131 | return false; | |
132 | ||
133 | if (hello.read(&chk,1)) | |
134 | { | |
135 | if (chk != '2') | |
136 | throw t2n_version_mismatch("illegal hello received (T2N)"); | |
137 | } | |
138 | else | |
139 | return false; | |
140 | ||
141 | if (hello.read(&chk,1)) | |
142 | { | |
143 | if (chk != 'N') | |
144 | throw t2n_version_mismatch("illegal hello received (T2N)"); | |
145 | } | |
146 | else | |
147 | return false; | |
148 | ||
149 | if (hello.read(&chk,1)) | |
150 | { | |
151 | if (chk != 'v') | |
152 | throw t2n_version_mismatch("illegal hello received (T2N)"); | |
153 | } | |
154 | else | |
155 | return false; | |
8104c8f7 GE |
156 | |
157 | int prot_version; | |
b2ba0928 GE |
158 | if (hello >> prot_version) |
159 | { | |
160 | if (prot_version != PROTOCOL_VERSION) | |
161 | throw t2n_version_mismatch("not compatible with the server protocol version"); | |
162 | } | |
163 | else | |
164 | return false; | |
165 | ||
166 | if (hello.read(&chk,1)) | |
167 | { | |
168 | if (chk != ';') | |
169 | throw t2n_version_mismatch("illegal hello received (1. ;)"); | |
170 | } | |
171 | else | |
172 | return false; | |
8104c8f7 | 173 | |
b2ba0928 GE |
174 | unsigned int hbo; |
175 | if (hello.read((char*)&hbo,sizeof(hbo))) | |
176 | { | |
177 | if (hbo != 1) | |
178 | throw t2n_version_mismatch("host byte order not matching"); | |
179 | } | |
180 | else | |
181 | return false; | |
8104c8f7 | 182 | |
b2ba0928 GE |
183 | if (hello.read(&chk,1)) |
184 | { | |
185 | if (chk != ';') | |
186 | throw t2n_version_mismatch("illegal hello received (2. ;)"); | |
187 | } | |
188 | else | |
189 | return false; | |
8104c8f7 | 190 | |
b2ba0928 | 191 | return true; |
8104c8f7 GE |
192 | } |
193 | ||
7087e187 GE |
194 | void command_client::send_command(command* cmd, result_container &res) |
195 | { | |
196 | ostringstream ofs; | |
197 | command_container cc(cmd); | |
198 | boost::archive::binary_oarchive oa(ofs); | |
199 | ||
45a2ebc9 GE |
200 | try |
201 | { | |
202 | oa << cc; | |
203 | } | |
204 | catch(boost::archive::archive_exception &e) | |
205 | { | |
206 | ostringstream msg; | |
207 | msg << "archive_exception while serializing on client-side, code " << e.code << " (" << e.what() << ")"; | |
208 | throw t2n_serialization_error(msg.str()); | |
209 | } | |
210 | catch(...) | |
211 | { throw; } | |
7087e187 | 212 | |
d535333f | 213 | std::ostream* ostr; |
fb3345ad | 214 | if ((ostr=c->get_logstream(fulldebug))!=NULL) |
d535333f GE |
215 | { |
216 | (*ostr) << "sending command, decoded data: " << std::endl; | |
217 | boost::archive::xml_oarchive xo(*ostr); | |
218 | xo << BOOST_SERIALIZATION_NVP(cc); | |
219 | } | |
220 | ||
fb3345ad | 221 | c->write(ofs.str()); |
7087e187 | 222 | |
45a2ebc9 | 223 | istringstream ifs(read_packet(command_timeout_usec)); |
7087e187 GE |
224 | boost::archive::binary_iarchive ia(ifs); |
225 | ||
45a2ebc9 GE |
226 | try |
227 | { | |
228 | ia >> res; | |
229 | } | |
230 | catch(boost::archive::archive_exception &e) | |
231 | { | |
232 | ostringstream msg; | |
233 | msg << "archive_exception while deserializing on client-side, code " << e.code << " (" << e.what() << ")"; | |
234 | throw t2n_serialization_error(msg.str()); | |
235 | } | |
236 | catch(...) | |
237 | { throw; } | |
d535333f | 238 | |
fb3345ad | 239 | if ((ostr=c->get_logstream(fulldebug))!=NULL) |
d535333f GE |
240 | { |
241 | (*ostr) << "received result, decoded data: " << std::endl; | |
242 | boost::archive::xml_oarchive xo(*ostr); | |
243 | xo << BOOST_SERIALIZATION_NVP(res); | |
244 | } | |
7087e187 GE |
245 | } |
246 | ||
247 | } |