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  std::string echo_back;
45  if (!std::getline(stream, echo_back))
46  {
47  logger::error() << "Failed to get echo back" << std::endl;
48  return;
49  }
50  std::string status;
51  if (!std::getline(stream, status))
52  {
53  logger::error() << "Failed to get status" << std::endl;
54  return;
55  }
56  status.pop_back(); // remove checksum
57 
58  response_processor_(time_read, echo_back, status, stream);
59 
60  std::string line;
61  while (std::getline(stream, line))
62  {
63  }
64  }
65 
66 public:
67  using Ptr = std::shared_ptr<Protocol>;
68 
69  explicit Protocol(Connection::Ptr connection)
70  : connection_(connection)
71  {
72  connection_->registerReceiveCallback(
73  boost::bind(&scip2::Protocol::cbReceive, this, boost::arg<1>(), boost::arg<2>()));
74  }
75 
77  const std::string& command,
79  {
80  connection_->send(command + "\n", cb);
81  }
82 
83  template <typename TResponse>
84  void registerCallback(typename TResponse::Callback cb)
85  {
86  response_processor_.registerCallback<TResponse>(cb);
87  }
88 };
89 
90 } // namespace scip2
91 
92 #endif // SCIP2_PROTOCOL_H
void cbReceive(boost::asio::streambuf &buf, const boost::posix_time::ptime &time_read)
Definition: protocol.h:39
ResponseProcessor response_processor_
Definition: protocol.h:37
std::shared_ptr< Connection > Ptr
Definition: connection.h:66
Protocol(Connection::Ptr connection)
Definition: protocol.h:69
Connection::Ptr connection_
Definition: protocol.h:36
void registerCallback(typename TResponse::Callback cb)
Definition: protocol.h:84
boost::function< void(const boost::posix_time::ptime &)> CallbackSend
Definition: connection.h:41
std::shared_ptr< Protocol > Ptr
Definition: protocol.h:67
void sendCommand(const std::string &command, Connection::CallbackSend cb=Connection::CallbackSend())
Definition: protocol.h:76
void registerCallback(typename TResponse::Callback cb)
Definition: response.h:75
std::ostream & error()
Definition: logger.cpp:105


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Tue May 11 2021 02:14:05