libt2n: (gerd) add protocol version config, add server callbacks (not fully working...
[libt2n] / src / command_server.cpp
index d3721a8..e7d6d4d 100644 (file)
@@ -20,6 +20,7 @@
 #include <string>
 #include <sstream>
 #include <stdexcept>
+#include <iostream>
 
 #include <boost/archive/binary_oarchive.hpp>
 #include <boost/archive/binary_iarchive.hpp>
@@ -27,6 +28,8 @@
 #include <boost/archive/xml_iarchive.hpp>
 #include <boost/serialization/serialization.hpp>
 
+#include <boost/bind.hpp>
+
 #include "command_server.hxx"
 #include "container.hxx"
 #include "log.hxx"
@@ -36,6 +39,18 @@ using namespace std;
 namespace libt2n
 {
 
+command_server::command_server(server& _s)
+    : s(_s)
+{
+    // register callback
+    s.add_callback(new_connection,bind(&command_server::new_connection_callback, boost::ref(*this), _1));
+}
+
+void command_server::new_connection_callback(server_connection* conn)
+{
+    cerr << "new connection callback: " << conn->get_id() << endl;
+}
+
 /// handle a command including deserialization and answering
 void command_server::handle_packet(const std::string& packet, server_connection* conn)
 {
@@ -49,6 +64,14 @@ void command_server::handle_packet(const std::string& packet, server_connection*
     // TODO: catch
     ia >> ccont;
 
+    std::ostream* ostr;
+    if ((ostr=s.get_logstream(fulldebug))!=NULL)
+    {
+        (*ostr) << "decoded packet data: " << std::endl;
+        boost::archive::xml_oarchive xo(*ostr);
+        xo << BOOST_SERIALIZATION_NVP(ccont);
+    }
+
     // TODO: cast to command subclass (template)
     command *cmd=ccont.get_command();
 
@@ -74,6 +97,13 @@ void command_server::handle_packet(const std::string& packet, server_connection*
     // TODO: catch
     oa << res;
 
+    if ((ostr=s.get_logstream(fulldebug))!=NULL)
+    {
+        (*ostr) << "returning result, decoded data: " << std::endl;
+        boost::archive::xml_oarchive xo(*ostr);
+        xo << BOOST_SERIALIZATION_NVP(res);
+    }
+
     conn->write(ofs.str());
 }