1 /***************************************************************************
2 * Copyright (C) 2006 by Gerd v. Egidy *
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. *
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. *
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 ***************************************************************************/
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>
31 #include <boost/bind.hpp>
33 #include "command_server.hxx"
34 #include "container.hxx"
46 command_server::command_server(server& _s)
47 : s(_s), guard_handle(0)
50 s.add_callback(new_connection,bind(&command_server::send_hello, boost::ref(*this), _1));
56 command_server::~command_server()
60 /// send a hello message to a new connection
61 void command_server::send_hello(unsigned int conn_id)
63 server_connection* sc=s.get_connection(conn_id);
66 return; // connection not existing, so no hello
68 std::ostringstream hello;
70 hello << "T2Nv" << PROTOCOL_VERSION << ';';
73 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
77 sc->write(hello.str());
80 /// handle a command including deserialization and answering
81 void command_server::handle_packet(const std::string& packet, server_connection* conn)
83 OBJLOGSTREAM(s,debug,"handling packet from connection " << conn->get_id());
86 std::istringstream ifs(packet);
87 boost::archive::binary_iarchive ia(ifs);
88 command_container ccont;
95 catch(boost::archive::archive_exception &e)
97 std::ostringstream msg;
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()));
105 if (!res.has_exception())
108 if ((ostr=s.get_logstream(fulldebug))!=NULL)
110 (*ostr) << "decoded packet data: " << std::endl;
111 boost::archive::xml_oarchive xo(*ostr);
112 xo << BOOST_SERIALIZATION_NVP(ccont);
115 command* cmd=cast_command(ccont.get_command());
121 res.set_result((*cmd)());
123 catch (t2n_exception &e)
124 { res.set_exception(e.clone()); }
130 std::ostringstream msg;
131 if (ccont.get_command()!=NULL)
132 msg << "illegal command of type " << typeid(ccont.get_command()).name() << " called";
134 msg << "NULL command called";
135 res.set_exception(new t2n_command_error(msg.str()));
139 std::ostringstream ofs;
140 boost::archive::binary_oarchive oa(ofs);
146 catch(boost::archive::archive_exception &e)
148 std::ostringstream msg;
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()));
158 if ((ostr=s.get_logstream(fulldebug))!=NULL)
160 (*ostr) << "returning result, decoded data: " << std::endl;
161 boost::archive::xml_oarchive xo(*ostr);
162 xo << BOOST_SERIALIZATION_NVP(res);
165 conn->write(ofs.str());
168 /** @brief handle incoming commands
169 @param[in,out] usec_timeout wait until new data is found, max timeout usecs.
170 -1: wait endless, 0: instant return
171 @param[out] usec_timeout_remaining microseconds from the timeout that were not used
173 void command_server::handle(long long usec_timeout, long long* usec_timeout_remaining)
179 if (s.fill_buffer(usec_timeout,usec_timeout_remaining))
182 unsigned int conn_id = 0;
184 while (s.get_packet(packet,conn_id))
186 server_connection* conn=s.get_connection(conn_id);
188 EXCEPTIONSTREAM(error,logic_error,"illegal connection id " << conn_id << " received");
190 { handle_packet(packet,conn); }
191 catch (t2n_transfer_error &e)
193 // shut down a connection with transfer errors (usually write errors)
208 // don't call cleanup on re-entered handle-calls
209 if (guard_handle == 0)