libt2n: (gerd) fix & improve logging
[libt2n] / src / command_client.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
23 #include <boost/archive/binary_oarchive.hpp>
24 #include <boost/archive/binary_iarchive.hpp>
25 #include <boost/archive/xml_oarchive.hpp>
26 #include <boost/archive/xml_iarchive.hpp>
27 #include <boost/serialization/serialization.hpp>
28
29 #include "command_client.hxx"
30
31 using namespace std;
32
33 namespace libt2n
34 {
35
36 void command_client::send_command(command* cmd, result_container &res)
37 {
38     ostringstream ofs;
39     command_container cc(cmd);
40     boost::archive::binary_oarchive oa(ofs);
41
42     // TODO: exceptions
43     oa << cc;
44
45     std::ostream* ostr;
46     if ((ostr=c.get_logstream(fulldebug))!=NULL)
47     {
48         (*ostr) << "sending command, decoded data: " << std::endl;
49         boost::archive::xml_oarchive xo(*ostr);
50         xo << BOOST_SERIALIZATION_NVP(cc);
51     }
52
53     c.write(ofs.str());
54
55     // TODO: fix timeout
56     string resultpacket;
57     while(!c.get_packet(resultpacket))
58         c.fill_buffer();
59
60     istringstream ifs(resultpacket);
61     boost::archive::binary_iarchive ia(ifs);
62
63     // TODO: exceptions
64     ia >> res;
65
66     if ((ostr=c.get_logstream(fulldebug))!=NULL)
67     {
68         (*ostr) << "received result, decoded data: " << std::endl;
69         boost::archive::xml_oarchive xo(*ostr);
70         xo << BOOST_SERIALIZATION_NVP(res);
71     }
72 }
73
74 }