libt2n: (gerd) fix exception passing
[libt2n] / src / command_server.cpp
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>
23
24 #include <boost/archive/binary_oarchive.hpp>
25 #include <boost/archive/binary_iarchive.hpp>
26 #include <boost/archive/xml_oarchive.hpp>
27 #include <boost/archive/xml_iarchive.hpp>
28 #include <boost/serialization/serialization.hpp>
29
30 #include "command_server.hxx"
31 #include "container.hxx"
32 #include "log.hxx"
33
34 using namespace std;
35
36 namespace libt2n
37 {
38
39 /// handle a command including deserialization and answering
40 void command_server::handle_packet(const std::string& packet, server_connection* conn)
41 {
42     OBJLOGSTREAM(s,debug,"handling packet from connection " << conn->get_id());
43
44     // deserialize packet
45     istringstream ifs(packet);
46     boost::archive::binary_iarchive ia(ifs);
47     command_container ccont;
48
49     // TODO: catch
50     ia >> ccont;
51
52     std::ostream* ostr;
53     if ((ostr=s.get_logstream(fulldebug))!=NULL)
54     {
55         (*ostr) << "decoded packet data: " << std::endl;
56         boost::archive::xml_oarchive xo(*ostr);
57         xo << BOOST_SERIALIZATION_NVP(ccont);
58     }
59
60     // TODO: cast to command subclass (template)
61     command *cmd=ccont.get_command();
62
63     result_container res;
64
65     if (cmd)
66     {
67         try
68         {
69             res.set_result((*cmd)());
70         }
71         catch (t2n_exception &e)
72             { res.set_exception(e.clone()); }
73         catch (...)
74             { throw; }
75     }
76     else
77         throw logic_error("uninitialized command called");
78
79     ostringstream ofs;
80     boost::archive::binary_oarchive oa(ofs);
81
82     // TODO: catch
83     oa << res;
84
85     if ((ostr=s.get_logstream(fulldebug))!=NULL)
86     {
87         (*ostr) << "returning result, decoded data: " << std::endl;
88         boost::archive::xml_oarchive xo(*ostr);
89         xo << BOOST_SERIALIZATION_NVP(res);
90     }
91
92     conn->write(ofs.str());
93 }
94
95 /** @brief handle incoming commands
96     @param usec_timeout wait until new data is found, max timeout usecs.
97             -1: wait endless, 0: no timeout
98 */
99 void command_server::handle(long long usec_timeout)
100 {
101     if (s.fill_buffer(usec_timeout))
102     {
103         string packet;
104         unsigned int conn_id;
105
106         while (s.get_packet(packet,conn_id))
107             handle_packet(packet,s.get_connection(conn_id)); 
108     }
109     s.cleanup();
110 }
111
112 }