2 Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy
4 The software in this package is distributed under the GNU General
5 Public License version 2 (with a special exception described below).
7 A copy of GNU General Public License (GPL) is included in this distribution,
8 in the file COPYING.GPL.
10 As a special exception, if other files instantiate templates or use macros
11 or inline functions from this file, or you compile this file and link it
12 with other works to produce a work based on this file, this file
13 does not by itself cause the resulting work to be covered
14 by the GNU General Public License.
16 However the source code for this file must still be made available
17 in accordance with section (3) of the GNU General Public License.
19 This exception does not invalidate any other reasons why a work based
20 on this file might be covered by the GNU General Public License.
28 #include <boost/archive/binary_oarchive.hpp>
29 #include <boost/archive/binary_iarchive.hpp>
30 #include <boost/archive/xml_oarchive.hpp>
31 #include <boost/archive/xml_iarchive.hpp>
32 #include <boost/serialization/serialization.hpp>
34 #include <boost/bind.hpp>
36 #include "command_server.hxx"
37 #include "container.hxx"
47 command_server::command_server(server& _s)
48 : s(_s), guard_handle(0)
51 s.add_callback(new_connection,bind(&command_server::send_hello, boost::ref(*this), _1));
57 command_server::~command_server()
61 /// send a hello message to a new connection
62 void command_server::send_hello(unsigned int conn_id)
64 server_connection* sc=s.get_connection(conn_id);
67 return; // connection not existing, so no hello
69 std::ostringstream hello;
71 hello << "T2Nv" << PROTOCOL_VERSION << ';';
74 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
78 sc->write(hello.str());
81 /// handle a command including deserialization and answering
82 void command_server::handle_packet(const std::string& packet, server_connection* conn)
84 OBJLOGSTREAM(s,debug,"handling packet from connection " << conn->get_id());
87 std::istringstream ifs(packet);
88 boost::archive::binary_iarchive ia(ifs);
89 command_container ccont;
96 catch(boost::archive::archive_exception &e)
98 std::ostringstream msg;
99 msg << "archive_exception while deserializing on server-side, "
100 "code " << e.code << " (" << e.what() << ")";
101 res.set_exception(new t2n_serialization_error(msg.str()));
104 if (!res.has_exception())
107 if ((ostr=s.get_logstream(fulldebug))!=NULL)
109 (*ostr) << "decoded packet data: " << std::endl;
110 boost::archive::xml_oarchive xo(*ostr);
111 xo << BOOST_SERIALIZATION_NVP(ccont);
114 command* cmd=cast_command(ccont.get_command());
120 res.set_result((*cmd)());
122 catch (t2n_exception &e)
123 { res.set_exception(e.clone()); }
127 std::ostringstream msg;
128 if (ccont.get_command()!=NULL)
129 msg << "illegal command of type " << typeid(ccont.get_command()).name() << " called";
131 msg << "NULL command called";
132 res.set_exception(new t2n_command_error(msg.str()));
136 std::ostringstream ofs;
137 boost::archive::binary_oarchive oa(ofs);
143 catch(boost::archive::archive_exception &e)
145 std::ostringstream msg;
146 msg << "archive_exception while serializing on server-side, "
147 "code " << e.code << " (" << e.what() << ")";
148 res.set_exception(new t2n_serialization_error(msg.str()));
153 if ((ostr=s.get_logstream(fulldebug))!=NULL)
155 (*ostr) << "returning result, decoded data: " << std::endl;
156 boost::archive::xml_oarchive xo(*ostr);
157 xo << BOOST_SERIALIZATION_NVP(res);
160 conn->write(ofs.str());
163 /** @brief handle incoming commands
164 @param[in,out] usec_timeout wait until new data is found, max timeout usecs.
165 -1: wait endless, 0: instant return
166 @param[out] usec_timeout_remaining microseconds from the timeout that were not used
168 void command_server::handle(long long usec_timeout, long long* usec_timeout_remaining)
174 if (s.fill_buffer(usec_timeout,usec_timeout_remaining))
177 unsigned int conn_id = 0;
179 while (s.get_packet(packet,conn_id))
181 server_connection* conn=s.get_connection(conn_id);
183 EXCEPTIONSTREAM(error,logic_error,"illegal connection id " << conn_id << " received");
185 { handle_packet(packet,conn); }
186 catch (t2n_transfer_error &e)
188 // shut down a connection with transfer errors (usually write errors)
201 // don't call cleanup on re-entered handle-calls
202 if (guard_handle == 0)