libt2n: (tomj) small fix to support data transfers over 2GB
[libt2n] / src / command_server.cpp
CommitLineData
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#include <stdexcept>
28cb45a5 23#include <iostream>
7087e187
GE
24
25#include <boost/archive/binary_oarchive.hpp>
26#include <boost/archive/binary_iarchive.hpp>
27#include <boost/archive/xml_oarchive.hpp>
28#include <boost/archive/xml_iarchive.hpp>
29#include <boost/serialization/serialization.hpp>
7087e187 30
28cb45a5
GE
31#include <boost/bind.hpp>
32
7087e187
GE
33#include "command_server.hxx"
34#include "container.hxx"
a7170401 35#include "log.hxx"
7087e187 36
8104c8f7
GE
37#ifdef HAVE_CONFIG_H
38#include <config.h>
39#endif
40
7087e187
GE
41using namespace std;
42
43namespace libt2n
44{
45
28cb45a5 46command_server::command_server(server& _s)
3b2543e7 47 : s(_s), guard_handle(0)
28cb45a5
GE
48{
49 // register callback
8104c8f7 50 s.add_callback(new_connection,bind(&command_server::send_hello, boost::ref(*this), _1));
28cb45a5
GE
51}
52
487afb79 53/// send a hello message to a new connection
8104c8f7 54void command_server::send_hello(unsigned int conn_id)
28cb45a5 55{
8104c8f7 56 server_connection* sc=s.get_connection(conn_id);
4b995a82 57 // TODO: Gerd: Throw exception if get_connection() returns NULL or just return?
8104c8f7 58
539b09c0 59 std::ostringstream hello;
8104c8f7
GE
60
61 hello << "T2Nv" << PROTOCOL_VERSION << ';';
62
63 int byteordercheck=1;
64 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
65
66 hello << ';';
67
68 sc->write(hello.str());
28cb45a5
GE
69}
70
7087e187
GE
71/// handle a command including deserialization and answering
72void command_server::handle_packet(const std::string& packet, server_connection* conn)
73{
a7170401
GE
74 OBJLOGSTREAM(s,debug,"handling packet from connection " << conn->get_id());
75
7087e187 76 // deserialize packet
539b09c0 77 std::istringstream ifs(packet);
7087e187
GE
78 boost::archive::binary_iarchive ia(ifs);
79 command_container ccont;
01a46463 80 result_container res;
7087e187 81
01a46463 82 try
d535333f 83 {
01a46463 84 ia >> ccont;
d535333f 85 }
01a46463
GE
86 catch(boost::archive::archive_exception &e)
87 {
539b09c0 88 std::ostringstream msg;
01a46463
GE
89 msg << "archive_exception while deserializing on server-side, "
90 "code " << e.code << " (" << e.what() << ")";
91 res.set_exception(new t2n_serialization_error(msg.str()));
92 }
93 catch(...)
94 { throw; }
d535333f 95
01a46463
GE
96 if (!res.has_exception())
97 {
98 std::ostream* ostr;
99 if ((ostr=s.get_logstream(fulldebug))!=NULL)
100 {
101 (*ostr) << "decoded packet data: " << std::endl;
102 boost::archive::xml_oarchive xo(*ostr);
103 xo << BOOST_SERIALIZATION_NVP(ccont);
104 }
7087e187 105
539b09c0 106 command* cmd=cast_command(ccont.get_command());
7087e187 107
01a46463 108 if (cmd)
7087e187 109 {
01a46463
GE
110 try
111 {
112 res.set_result((*cmd)());
113 }
114 catch (t2n_exception &e)
115 { res.set_exception(e.clone()); }
116 catch (...)
117 { throw; }
7087e187 118 }
01a46463 119 else
539b09c0
GE
120 {
121 std::ostringstream msg;
122 if (ccont.get_command()!=NULL)
123 msg << "illegal command of type " << typeid(ccont.get_command()).name() << " called";
124 else
125 msg << "NULL command called";
126 res.set_exception(new t2n_command_error(msg.str()));
127 }
7087e187 128 }
7087e187 129
539b09c0 130 std::ostringstream ofs;
7087e187
GE
131 boost::archive::binary_oarchive oa(ofs);
132
01a46463
GE
133 try
134 {
135 oa << res;
136 }
137 catch(boost::archive::archive_exception &e)
138 {
539b09c0 139 std::ostringstream msg;
01a46463
GE
140 msg << "archive_exception while serializing on server-side, "
141 "code " << e.code << " (" << e.what() << ")";
142 res.set_exception(new t2n_serialization_error(msg.str()));
143 oa << res;
144 }
145 catch(...)
146 { throw; }
7087e187 147
01a46463 148 std::ostream* ostr;
d535333f
GE
149 if ((ostr=s.get_logstream(fulldebug))!=NULL)
150 {
151 (*ostr) << "returning result, decoded data: " << std::endl;
152 boost::archive::xml_oarchive xo(*ostr);
153 xo << BOOST_SERIALIZATION_NVP(res);
154 }
155
7087e187
GE
156 conn->write(ofs.str());
157}
158
159/** @brief handle incoming commands
45a2ebc9
GE
160 @param[in,out] usec_timeout wait until new data is found, max timeout usecs.
161 -1: wait endless, 0: instant return
9a5d7790 162 @param[out] usec_timeout_remaining microseconds from the timeout that were not used
7087e187 163*/
45a2ebc9 164void command_server::handle(long long usec_timeout, long long* usec_timeout_remaining)
7087e187 165{
3b2543e7
GE
166 guard_handle++;
167 try
7087e187 168 {
3b2543e7 169 if (s.fill_buffer(usec_timeout,usec_timeout_remaining))
c7857475 170 {
3b2543e7
GE
171 std::string packet;
172 unsigned int conn_id;
c7857475 173
3b2543e7 174 while (s.get_packet(packet,conn_id))
c7857475 175 {
3b2543e7
GE
176 server_connection* conn=s.get_connection(conn_id);
177 if (!conn)
178 EXCEPTIONSTREAM(error,logic_error,"illegal connection id " << conn_id << " received");
179
180 try
181 { handle_packet(packet,conn); }
182 catch (t2n_transfer_error &e)
183 {
184 // shut down a connection with transfer errors (usually write errors)
185 conn->close();
186 }
187 catch(...)
188 { throw; }
c7857475 189 }
c7857475 190 }
7087e187 191 }
3b2543e7
GE
192 catch(...)
193 {
194 guard_handle--;
195 throw;
196 }
197 guard_handle--;
198
199 // don't call cleanup on re-entered handle-calls
200 if (guard_handle == 0)
201 s.cleanup();
7087e187
GE
202}
203
204}