Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef SCIP2_PROTOCOL_H
00018 #define SCIP2_PROTOCOL_H
00019
00020 #include <boost/asio.hpp>
00021 #include <boost/bind/bind.hpp>
00022
00023 #include <scip2/connection.h>
00024 #include <scip2/response.h>
00025
00026 #include <map>
00027 #include <string>
00028
00029 namespace scip2
00030 {
00031 class Protocol
00032 {
00033 protected:
00034 Connection::Ptr connection_;
00035 ResponseProcessor response_processor_;
00036
00037 void cbReceive(
00038 boost::asio::streambuf &buf,
00039 const boost::posix_time::ptime &time_read)
00040 {
00041 std::istream stream(&buf);
00042 std::string echo_back;
00043 if (!std::getline(stream, echo_back))
00044 {
00045 std::cerr << "Failed to get echo back" << std::endl;
00046 return;
00047 }
00048 std::string status;
00049 if (!std::getline(stream, status))
00050 {
00051 std::cerr << "Failed to get status" << std::endl;
00052 return;
00053 }
00054 status.pop_back();
00055
00056 response_processor_(time_read, echo_back, status, stream);
00057
00058 std::string line;
00059 while (std::getline(stream, line))
00060 {
00061 }
00062 }
00063
00064 public:
00065 using Ptr = std::shared_ptr<Protocol>;
00066
00067 explicit Protocol(Connection::Ptr connection)
00068 : connection_(connection)
00069 {
00070 connection_->registerReceiveCallback(
00071 boost::bind(&scip2::Protocol::cbReceive, this, boost::arg<1>(), boost::arg<2>()));
00072 }
00073
00074 void sendCommand(
00075 const std::string &command,
00076 Connection::CallbackSend cb = Connection::CallbackSend())
00077 {
00078 connection_->send(command + "\n", cb);
00079 }
00080
00081 template <typename TResponse>
00082 void registerCallback(typename TResponse::Callback cb)
00083 {
00084 response_processor_.registerCallback<TResponse>(cb);
00085 }
00086 };
00087
00088 }
00089
00090 #endif // SCIP2_PROTOCOL_H