libt2n: (gerd) more documentation-polishing
[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
GE
56 server_connection* sc=s.get_connection(conn_id);
57
539b09c0 58 std::ostringstream hello;
8104c8f7
GE
59
60 hello << "T2Nv" << PROTOCOL_VERSION << ';';
61
62 int byteordercheck=1;
63 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
64
65 hello << ';';
66
67 sc->write(hello.str());
28cb45a5
GE
68}
69
7087e187
GE
70/// handle a command including deserialization and answering
71void command_server::handle_packet(const std::string& packet, server_connection* conn)
72{
a7170401
GE
73 OBJLOGSTREAM(s,debug,"handling packet from connection " << conn->get_id());
74
7087e187 75 // deserialize packet
539b09c0 76 std::istringstream ifs(packet);
7087e187
GE
77 boost::archive::binary_iarchive ia(ifs);
78 command_container ccont;
01a46463 79 result_container res;
7087e187 80
01a46463 81 try
d535333f 82 {
01a46463 83 ia >> ccont;
d535333f 84 }
01a46463
GE
85 catch(boost::archive::archive_exception &e)
86 {
539b09c0 87 std::ostringstream msg;
01a46463
GE
88 msg << "archive_exception while deserializing on server-side, "
89 "code " << e.code << " (" << e.what() << ")";
90 res.set_exception(new t2n_serialization_error(msg.str()));
91 }
92 catch(...)
93 { throw; }
d535333f 94
01a46463
GE
95 if (!res.has_exception())
96 {
97 std::ostream* ostr;
98 if ((ostr=s.get_logstream(fulldebug))!=NULL)
99 {
100 (*ostr) << "decoded packet data: " << std::endl;
101 boost::archive::xml_oarchive xo(*ostr);
102 xo << BOOST_SERIALIZATION_NVP(ccont);
103 }
7087e187 104
539b09c0 105 command* cmd=cast_command(ccont.get_command());
7087e187 106
01a46463 107 if (cmd)
7087e187 108 {
01a46463
GE
109 try
110 {
111 res.set_result((*cmd)());
112 }
113 catch (t2n_exception &e)
114 { res.set_exception(e.clone()); }
115 catch (...)
116 { throw; }
7087e187 117 }
01a46463 118 else
539b09c0
GE
119 {
120 std::ostringstream msg;
121 if (ccont.get_command()!=NULL)
122 msg << "illegal command of type " << typeid(ccont.get_command()).name() << " called";
123 else
124 msg << "NULL command called";
125 res.set_exception(new t2n_command_error(msg.str()));
126 }
7087e187 127 }
7087e187 128
539b09c0 129 std::ostringstream ofs;
7087e187
GE
130 boost::archive::binary_oarchive oa(ofs);
131
01a46463
GE
132 try
133 {
134 oa << res;
135 }
136 catch(boost::archive::archive_exception &e)
137 {
539b09c0 138 std::ostringstream msg;
01a46463
GE
139 msg << "archive_exception while serializing on server-side, "
140 "code " << e.code << " (" << e.what() << ")";
141 res.set_exception(new t2n_serialization_error(msg.str()));
142 oa << res;
143 }
144 catch(...)
145 { throw; }
7087e187 146
01a46463 147 std::ostream* ostr;
d535333f
GE
148 if ((ostr=s.get_logstream(fulldebug))!=NULL)
149 {
150 (*ostr) << "returning result, decoded data: " << std::endl;
151 boost::archive::xml_oarchive xo(*ostr);
152 xo << BOOST_SERIALIZATION_NVP(res);
153 }
154
7087e187
GE
155 conn->write(ofs.str());
156}
157
158/** @brief handle incoming commands
45a2ebc9
GE
159 @param[in,out] usec_timeout wait until new data is found, max timeout usecs.
160 -1: wait endless, 0: instant return
9a5d7790 161 @param[out] usec_timeout_remaining microseconds from the timeout that were not used
7087e187 162*/
45a2ebc9 163void command_server::handle(long long usec_timeout, long long* usec_timeout_remaining)
7087e187 164{
3b2543e7
GE
165 guard_handle++;
166 try
7087e187 167 {
3b2543e7 168 if (s.fill_buffer(usec_timeout,usec_timeout_remaining))
c7857475 169 {
3b2543e7
GE
170 std::string packet;
171 unsigned int conn_id;
c7857475 172
3b2543e7 173 while (s.get_packet(packet,conn_id))
c7857475 174 {
3b2543e7
GE
175 server_connection* conn=s.get_connection(conn_id);
176 if (!conn)
177 EXCEPTIONSTREAM(error,logic_error,"illegal connection id " << conn_id << " received");
178
179 try
180 { handle_packet(packet,conn); }
181 catch (t2n_transfer_error &e)
182 {
183 // shut down a connection with transfer errors (usually write errors)
184 conn->close();
185 }
186 catch(...)
187 { throw; }
c7857475 188 }
c7857475 189 }
7087e187 190 }
3b2543e7
GE
191 catch(...)
192 {
193 guard_handle--;
194 throw;
195 }
196 guard_handle--;
197
198 // don't call cleanup on re-entered handle-calls
199 if (guard_handle == 0)
200 s.cleanup();
7087e187
GE
201}
202
203}