libt2n: (gerd) small fixes, hello unit tests
[libt2n] / src / command_client.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
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>
7087e187 28
8104c8f7
GE
29#include <boost/bind.hpp>
30
7087e187
GE
31#include "command_client.hxx"
32
8104c8f7
GE
33#ifdef HAVE_CONFIG_H
34#include <config.h>
35#endif
36
7087e187
GE
37using namespace std;
38
39namespace libt2n
40{
41
8104c8f7
GE
42command_client::command_client(client_connection& _c)
43 : c(_c)
44{
45 // for reconnects
46 c.add_callback(new_connection,bind(&command_client::read_hello, boost::ref(*this)));
47
48 read_hello();
49}
50
51void command_client::read_hello()
52{
53 // TODO: fix timeout
54 string resultpacket;
04d86ba4 55 while(!c.get_packet(resultpacket) && !c.is_closed())
8104c8f7
GE
56 c.fill_buffer();
57
58 istringstream hello(resultpacket);
59
60 char chk[5];
61 hello.read(chk,4);
62 chk[4]=0;
63 if (hello.fail() || hello.eof() || string("T2Nv") != chk)
64 throw t2n_version_mismatch("illegal hello received (T2N)");
65
66 int prot_version;
67 hello >> prot_version;
68 if (hello.fail() || hello.eof() || prot_version != PROTOCOL_VERSION)
69 throw t2n_version_mismatch("not compatible with the server protocol version");
70
71 hello.read(chk,1);
72 if (hello.fail() || hello.eof() || chk[0] != ';')
73 throw t2n_version_mismatch("illegal hello received (1. ;)");
74
75 hello.read(chk,4);
76 if (hello.fail() || hello.eof() || *((int*)chk) != 1)
77 throw t2n_version_mismatch("host byte order not matching");
78
79 hello.read(chk,1);
80 if (hello.fail() || hello.eof() || chk[0] != ';')
81 throw t2n_version_mismatch("illegal hello received (2. ;)");
82}
83
7087e187
GE
84void command_client::send_command(command* cmd, result_container &res)
85{
86 ostringstream ofs;
87 command_container cc(cmd);
88 boost::archive::binary_oarchive oa(ofs);
89
90 // TODO: exceptions
91 oa << cc;
92
d535333f
GE
93 std::ostream* ostr;
94 if ((ostr=c.get_logstream(fulldebug))!=NULL)
95 {
96 (*ostr) << "sending command, decoded data: " << std::endl;
97 boost::archive::xml_oarchive xo(*ostr);
98 xo << BOOST_SERIALIZATION_NVP(cc);
99 }
100
7087e187
GE
101 c.write(ofs.str());
102
103 // TODO: fix timeout
104 string resultpacket;
105 while(!c.get_packet(resultpacket))
106 c.fill_buffer();
107
108 istringstream ifs(resultpacket);
109 boost::archive::binary_iarchive ia(ifs);
110
111 // TODO: exceptions
112 ia >> res;
d535333f
GE
113
114 if ((ostr=c.get_logstream(fulldebug))!=NULL)
115 {
116 (*ostr) << "received result, decoded data: " << std::endl;
117 boost::archive::xml_oarchive xo(*ostr);
118 xo << BOOST_SERIALIZATION_NVP(res);
119 }
7087e187
GE
120}
121
122}