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 <boost/asio.hpp>
21 #include <boost/bind/bind.hpp>
22 
23 #include <scip2/connection.h>
24 #include <scip2/response.h>
25 
26 #include <map>
27 #include <string>
28 
29 namespace scip2
30 {
31 class Protocol
32 {
33 protected:
36 
37  void cbReceive(
38  boost::asio::streambuf &buf,
39  const boost::posix_time::ptime &time_read)
40  {
41  std::istream stream(&buf);
42  std::string echo_back;
43  if (!std::getline(stream, echo_back))
44  {
45  std::cerr << "Failed to get echo back" << std::endl;
46  return;
47  }
48  std::string status;
49  if (!std::getline(stream, status))
50  {
51  std::cerr << "Failed to get status" << std::endl;
52  return;
53  }
54  status.pop_back(); // remove checksum
55 
56  response_processor_(time_read, echo_back, status, stream);
57 
58  std::string line;
59  while (std::getline(stream, line))
60  {
61  }
62  }
63 
64 public:
65  using Ptr = std::shared_ptr<Protocol>;
66 
67  explicit Protocol(Connection::Ptr connection)
68  : connection_(connection)
69  {
70  connection_->registerReceiveCallback(
71  boost::bind(&scip2::Protocol::cbReceive, this, boost::arg<1>(), boost::arg<2>()));
72  }
73 
75  const std::string &command,
77  {
78  connection_->send(command + "\n", cb);
79  }
80 
81  template <typename TResponse>
82  void registerCallback(typename TResponse::Callback cb)
83  {
84  response_processor_.registerCallback<TResponse>(cb);
85  }
86 };
87 
88 } // namespace scip2
89 
90 #endif // SCIP2_PROTOCOL_H
void cbReceive(boost::asio::streambuf &buf, const boost::posix_time::ptime &time_read)
Definition: protocol.h:37
ResponseProcessor response_processor_
Definition: protocol.h:35
std::shared_ptr< Connection > Ptr
Definition: connection.h:63
boost::function< void(const boost::posix_time::ptime &)> CallbackSend
Definition: connection.h:38
Protocol(Connection::Ptr connection)
Definition: protocol.h:67
Connection::Ptr connection_
Definition: protocol.h:34
void registerCallback(typename TResponse::Callback cb)
Definition: protocol.h:82
std::shared_ptr< Protocol > Ptr
Definition: protocol.h:65
void sendCommand(const std::string &command, Connection::CallbackSend cb=Connection::CallbackSend())
Definition: protocol.h:74
void registerCallback(typename TResponse::Callback cb)
Definition: response.h:69


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 6 2019 19:55:58