libt2n: (gerd) small fixes, hello unit tests
[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
GE
46command_server::command_server(server& _s)
47 : s(_s)
48{
49 // register callback
8104c8f7 50 s.add_callback(new_connection,bind(&command_server::send_hello, boost::ref(*this), _1));
28cb45a5
GE
51}
52
8104c8f7 53void command_server::send_hello(unsigned int conn_id)
28cb45a5 54{
8104c8f7
GE
55 server_connection* sc=s.get_connection(conn_id);
56
57 ostringstream hello;
58
59 hello << "T2Nv" << PROTOCOL_VERSION << ';';
60
61 int byteordercheck=1;
62 hello.write((char*)&byteordercheck,sizeof(byteordercheck));
63
64 hello << ';';
65
66 sc->write(hello.str());
28cb45a5
GE
67}
68
7087e187
GE
69/// handle a command including deserialization and answering
70void command_server::handle_packet(const std::string& packet, server_connection* conn)
71{
a7170401
GE
72 OBJLOGSTREAM(s,debug,"handling packet from connection " << conn->get_id());
73
7087e187
GE
74 // deserialize packet
75 istringstream ifs(packet);
76 boost::archive::binary_iarchive ia(ifs);
77 command_container ccont;
78
79 // TODO: catch
80 ia >> ccont;
81
d535333f
GE
82 std::ostream* ostr;
83 if ((ostr=s.get_logstream(fulldebug))!=NULL)
84 {
85 (*ostr) << "decoded packet data: " << std::endl;
86 boost::archive::xml_oarchive xo(*ostr);
87 xo << BOOST_SERIALIZATION_NVP(ccont);
88 }
89
7087e187
GE
90 // TODO: cast to command subclass (template)
91 command *cmd=ccont.get_command();
92
93 result_container res;
94
95 if (cmd)
96 {
97 try
98 {
99 res.set_result((*cmd)());
100 }
101 catch (t2n_exception &e)
102 { res.set_exception(e.clone()); }
103 catch (...)
104 { throw; }
105 }
106 else
107 throw logic_error("uninitialized command called");
108
109 ostringstream ofs;
110 boost::archive::binary_oarchive oa(ofs);
111
112 // TODO: catch
113 oa << res;
114
d535333f
GE
115 if ((ostr=s.get_logstream(fulldebug))!=NULL)
116 {
117 (*ostr) << "returning result, decoded data: " << std::endl;
118 boost::archive::xml_oarchive xo(*ostr);
119 xo << BOOST_SERIALIZATION_NVP(res);
120 }
121
7087e187
GE
122 conn->write(ofs.str());
123}
124
125/** @brief handle incoming commands
126 @param usec_timeout wait until new data is found, max timeout usecs.
127 -1: wait endless, 0: no timeout
128*/
129void command_server::handle(long long usec_timeout)
130{
131 if (s.fill_buffer(usec_timeout))
132 {
133 string packet;
134 unsigned int conn_id;
135
136 while (s.get_packet(packet,conn_id))
137 handle_packet(packet,s.get_connection(conn_id));
138 }
139 s.cleanup();
140}
141
142}