#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/serialization/serialization.hpp>
-#include <boost/serialization/export.hpp>
+
+#include <boost/bind.hpp>
#include "command_client.hxx"
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
using namespace std;
namespace libt2n
{
+command_client::command_client(client_connection& _c)
+ : c(_c)
+{
+ // for reconnects
+ c.add_callback(new_connection,bind(&command_client::read_hello, boost::ref(*this)));
+
+ read_hello();
+}
+
+void command_client::read_hello()
+{
+ // TODO: fix timeout
+ string resultpacket;
+ while(!c.get_packet(resultpacket))
+ c.fill_buffer();
+
+ istringstream hello(resultpacket);
+
+ char chk[5];
+ hello.read(chk,4);
+ chk[4]=0;
+ if (hello.fail() || hello.eof() || string("T2Nv") != chk)
+ throw t2n_version_mismatch("illegal hello received (T2N)");
+
+ int prot_version;
+ hello >> prot_version;
+ if (hello.fail() || hello.eof() || prot_version != PROTOCOL_VERSION)
+ throw t2n_version_mismatch("not compatible with the server protocol version");
+
+ hello.read(chk,1);
+ if (hello.fail() || hello.eof() || chk[0] != ';')
+ throw t2n_version_mismatch("illegal hello received (1. ;)");
+
+ hello.read(chk,4);
+ if (hello.fail() || hello.eof() || *((int*)chk) != 1)
+ throw t2n_version_mismatch("host byte order not matching");
+
+ hello.read(chk,1);
+ if (hello.fail() || hello.eof() || chk[0] != ';')
+ throw t2n_version_mismatch("illegal hello received (2. ;)");
+}
+
void command_client::send_command(command* cmd, result_container &res)
{
ostringstream ofs;
// TODO: exceptions
oa << cc;
+ std::ostream* ostr;
+ if ((ostr=c.get_logstream(fulldebug))!=NULL)
+ {
+ (*ostr) << "sending command, decoded data: " << std::endl;
+ boost::archive::xml_oarchive xo(*ostr);
+ xo << BOOST_SERIALIZATION_NVP(cc);
+ }
+
c.write(ofs.str());
// TODO: fix timeout
// TODO: exceptions
ia >> res;
+
+ if ((ostr=c.get_logstream(fulldebug))!=NULL)
+ {
+ (*ostr) << "received result, decoded data: " << std::endl;
+ boost::archive::xml_oarchive xo(*ostr);
+ xo << BOOST_SERIALIZATION_NVP(res);
+ }
}
}