X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=src%2Fcommand_client.cpp;h=41c74de07f6060c8fd31d7b1221a930d121cc52c;hp=0d387b9e0029faa8d93ded8be30fcc7b37e2486b;hb=04d86ba4ad4f14ab08f38804e772ec46a8ac92b0;hpb=7087e18783f91d2b889e880462d1d1da24831c28 diff --git a/src/command_client.cpp b/src/command_client.cpp index 0d387b9..41c74de 100644 --- a/src/command_client.cpp +++ b/src/command_client.cpp @@ -25,15 +25,62 @@ #include #include #include -#include + +#include #include "command_client.hxx" +#ifdef HAVE_CONFIG_H +#include +#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.is_closed()) + 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; @@ -43,6 +90,14 @@ void command_client::send_command(command* cmd, result_container &res) // 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 @@ -55,6 +110,13 @@ void command_client::send_command(command* cmd, result_container &res) // 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); + } } }