protocol.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2018 The urg_stamped Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
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();  // remove checksum
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 }  // namespace scip2
00089 
00090 #endif  // SCIP2_PROTOCOL_H


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 6 2019 18:59:51