Commit | Line | Data |
---|---|---|
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 |
41 | using namespace std; |
42 | ||
43 | namespace libt2n | |
44 | { | |
45 | ||
28cb45a5 GE |
46 | command_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 | 53 | void command_server::send_hello(unsigned int conn_id) |
28cb45a5 | 54 | { |
8104c8f7 GE |
55 | server_connection* sc=s.get_connection(conn_id); |
56 | ||
539b09c0 | 57 | std::ostringstream hello; |
8104c8f7 GE |
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 |
70 | void 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 | 74 | // deserialize packet |
539b09c0 | 75 | std::istringstream ifs(packet); |
7087e187 GE |
76 | boost::archive::binary_iarchive ia(ifs); |
77 | command_container ccont; | |
01a46463 | 78 | result_container res; |
7087e187 | 79 | |
01a46463 | 80 | try |
d535333f | 81 | { |
01a46463 | 82 | ia >> ccont; |
d535333f | 83 | } |
01a46463 GE |
84 | catch(boost::archive::archive_exception &e) |
85 | { | |
539b09c0 | 86 | std::ostringstream msg; |
01a46463 GE |
87 | msg << "archive_exception while deserializing on server-side, " |
88 | "code " << e.code << " (" << e.what() << ")"; | |
89 | res.set_exception(new t2n_serialization_error(msg.str())); | |
90 | } | |
91 | catch(...) | |
92 | { throw; } | |
d535333f | 93 | |
01a46463 GE |
94 | if (!res.has_exception()) |
95 | { | |
96 | std::ostream* ostr; | |
97 | if ((ostr=s.get_logstream(fulldebug))!=NULL) | |
98 | { | |
99 | (*ostr) << "decoded packet data: " << std::endl; | |
100 | boost::archive::xml_oarchive xo(*ostr); | |
101 | xo << BOOST_SERIALIZATION_NVP(ccont); | |
102 | } | |
7087e187 | 103 | |
539b09c0 | 104 | command* cmd=cast_command(ccont.get_command()); |
7087e187 | 105 | |
01a46463 | 106 | if (cmd) |
7087e187 | 107 | { |
01a46463 GE |
108 | try |
109 | { | |
110 | res.set_result((*cmd)()); | |
111 | } | |
112 | catch (t2n_exception &e) | |
113 | { res.set_exception(e.clone()); } | |
114 | catch (...) | |
115 | { throw; } | |
7087e187 | 116 | } |
01a46463 | 117 | else |
539b09c0 GE |
118 | { |
119 | std::ostringstream msg; | |
120 | if (ccont.get_command()!=NULL) | |
121 | msg << "illegal command of type " << typeid(ccont.get_command()).name() << " called"; | |
122 | else | |
123 | msg << "NULL command called"; | |
124 | res.set_exception(new t2n_command_error(msg.str())); | |
125 | } | |
7087e187 | 126 | } |
7087e187 | 127 | |
539b09c0 | 128 | std::ostringstream ofs; |
7087e187 GE |
129 | boost::archive::binary_oarchive oa(ofs); |
130 | ||
01a46463 GE |
131 | try |
132 | { | |
133 | oa << res; | |
134 | } | |
135 | catch(boost::archive::archive_exception &e) | |
136 | { | |
539b09c0 | 137 | std::ostringstream msg; |
01a46463 GE |
138 | msg << "archive_exception while serializing on server-side, " |
139 | "code " << e.code << " (" << e.what() << ")"; | |
140 | res.set_exception(new t2n_serialization_error(msg.str())); | |
141 | oa << res; | |
142 | } | |
143 | catch(...) | |
144 | { throw; } | |
7087e187 | 145 | |
01a46463 | 146 | std::ostream* ostr; |
d535333f GE |
147 | if ((ostr=s.get_logstream(fulldebug))!=NULL) |
148 | { | |
149 | (*ostr) << "returning result, decoded data: " << std::endl; | |
150 | boost::archive::xml_oarchive xo(*ostr); | |
151 | xo << BOOST_SERIALIZATION_NVP(res); | |
152 | } | |
153 | ||
7087e187 GE |
154 | conn->write(ofs.str()); |
155 | } | |
156 | ||
157 | /** @brief handle incoming commands | |
45a2ebc9 GE |
158 | @param[in,out] usec_timeout wait until new data is found, max timeout usecs. |
159 | -1: wait endless, 0: instant return | |
7087e187 | 160 | */ |
45a2ebc9 | 161 | void command_server::handle(long long usec_timeout, long long* usec_timeout_remaining) |
7087e187 | 162 | { |
45a2ebc9 | 163 | if (s.fill_buffer(usec_timeout,usec_timeout_remaining)) |
7087e187 | 164 | { |
539b09c0 | 165 | std::string packet; |
7087e187 GE |
166 | unsigned int conn_id; |
167 | ||
168 | while (s.get_packet(packet,conn_id)) | |
169 | handle_packet(packet,s.get_connection(conn_id)); | |
170 | } | |
171 | s.cleanup(); | |
172 | } | |
173 | ||
174 | } |