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