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