protocol.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The urg_stamped Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SCIP2_PROTOCOL_H
18 #define SCIP2_PROTOCOL_H
19 
20 #include <map>
21 #include <memory>
22 #include <string>
23 
24 #include <boost/asio.hpp>
25 #include <boost/bind/bind.hpp>
26 
27 #include <scip2/connection.h>
28 #include <scip2/response.h>
29 #include <scip2/logger.h>
30 
31 namespace scip2
32 {
33 class Protocol
34 {
35 protected:
38 
39  void cbReceive(
40  boost::asio::streambuf& buf,
41  const boost::posix_time::ptime& time_read)
42  {
43  std::istream stream(&buf);
44 
45  std::string echo_back;
46  if (!std::getline(stream, echo_back))
47  {
48  logger::error() << "Failed to get echo back" << std::endl;
49  return;
50  }
51  if (echo_back == "")
52  {
53  logger::debug() << "Empty response echo back " << std::endl;
54  return;
55  }
56 
57  std::string status;
58  if (!std::getline(stream, status))
59  {
60  logger::error() << "Failed to get status" << std::endl;
61  return;
62  }
63  if (status == "")
64  {
65  logger::debug() << "Empty response status" << std::endl;
66  return;
67  }
68  status.pop_back(); // remove checksum
69 
70  response_processor_(time_read, echo_back, status, stream);
71  }
72 
73 public:
74  using Ptr = std::shared_ptr<Protocol>;
75 
76  explicit Protocol(Connection::Ptr connection)
77  : connection_(connection)
78  {
79  connection_->registerReceiveCallback(
80  boost::bind(&scip2::Protocol::cbReceive, this, boost::arg<1>(), boost::arg<2>()));
81  }
82 
84  const std::string& command,
86  {
87  connection_->send(command + "\n", cb);
88  }
89 
90  template <typename TResponse>
91  void registerCallback(typename TResponse::Callback cb)
92  {
94  }
95 };
96 
97 } // namespace scip2
98 
99 #endif // SCIP2_PROTOCOL_H
scip2::Protocol::Protocol
Protocol(Connection::Ptr connection)
Definition: protocol.h:76
scip2::Protocol::response_processor_
ResponseProcessor response_processor_
Definition: protocol.h:37
logger.h
command
ROSLIB_DECL std::string command(const std::string &cmd)
response.h
scip2
Definition: connection.h:30
scip2::ResponseProcessor::registerCallback
void registerCallback(typename TResponse::Callback cb)
Definition: response.h:76
scip2::Protocol::connection_
Connection::Ptr connection_
Definition: protocol.h:36
scip2::Protocol::registerCallback
void registerCallback(typename TResponse::Callback cb)
Definition: protocol.h:91
scip2::Protocol::sendCommand
void sendCommand(const std::string &command, Connection::CallbackSend cb=Connection::CallbackSend())
Definition: protocol.h:83
scip2::logger::debug
std::ostream & debug()
Definition: logger.cpp:93
connection.h
scip2::Protocol::Ptr
std::shared_ptr< Protocol > Ptr
Definition: protocol.h:74
scip2::Connection::Ptr
std::shared_ptr< Connection > Ptr
Definition: connection.h:68
scip2::ResponseProcessor
Definition: response.h:36
scip2::Protocol::cbReceive
void cbReceive(boost::asio::streambuf &buf, const boost::posix_time::ptime &time_read)
Definition: protocol.h:39
scip2::Protocol
Definition: protocol.h:33
scip2::logger::error
std::ostream & error()
Definition: logger.cpp:105
scip2::Connection::CallbackSend
boost::function< void(const boost::posix_time::ptime &)> CallbackSend
Definition: connection.h:43


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Wed Dec 18 2024 03:10:57