response.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_RESPONSE_H
18 #define SCIP2_RESPONSE_H
19 
20 #include <boost/asio.hpp>
21 
22 #include <map>
23 #include <string>
24 
27 #include <scip2/response/quit.h>
28 #include <scip2/response/reboot.h>
29 #include <scip2/response/reset.h>
30 #include <scip2/response/stream.h>
32 #include <scip2/logger.h>
33 
34 namespace scip2
35 {
37 {
38 protected:
39  std::map<std::string, Response::Ptr> responses_;
41  {
42  responses_[response->getCommandCode()] = response;
43  }
44 
45 public:
47  {
58  }
59  void operator()(
60  const boost::posix_time::ptime& time_read,
61  const std::string& echo_back,
62  const std::string& status,
63  std::istream& stream) const
64  {
65  const std::string command_code(echo_back.substr(0, 2));
66  const auto response = responses_.find(command_code);
67  if (response == responses_.end())
68  {
69  logger::debug() << "Unknown response " << command_code << std::endl;
70  readUntilEnd(stream);
71  return;
72  }
73  (*(response->second))(time_read, echo_back, status, stream);
74  }
75  template <typename TResponse>
76  void registerCallback(typename TResponse::Callback cb)
77  {
78  const auto response = responses_.find(TResponse().getCommandCode());
79  assert(response != responses_.end());
80  auto response_downcast = std::dynamic_pointer_cast<TResponse>(response->second);
81  assert(response_downcast);
82 
83  response_downcast->registerCallback(cb);
84  }
85 };
86 
87 } // namespace scip2
88 
89 #endif // SCIP2_RESPONSE_H
response
const std::string response
scip2::ResponsePP
Definition: parameters.h:86
scip2::ResponseProcessor::responses_
std::map< std::string, Response::Ptr > responses_
Definition: response.h:39
quit.h
logger.h
scip2::ResponseME
Definition: stream.h:162
scip2::ResponseRB
Definition: reboot.h:28
scip2::ResponseRT
Definition: reset.h:62
scip2::readUntilEnd
void readUntilEnd(std::istream &stream)
Definition: abstract.h:39
scip2::ResponseTM
Definition: time_sync.h:42
scip2::ResponseMD
Definition: stream.h:102
scip2::ResponseProcessor::ResponseProcessor
ResponseProcessor()
Definition: response.h:46
scip2::ResponseVV
Definition: parameters.h:95
scip2
Definition: connection.h:30
scip2::ResponseProcessor::registerCallback
void registerCallback(typename TResponse::Callback cb)
Definition: response.h:76
scip2::ResponseII
Definition: parameters.h:104
scip2::ResponseRS
Definition: reset.h:28
stream.h
reset.h
parameters.h
scip2::logger::debug
std::ostream & debug()
Definition: logger.cpp:93
reboot.h
scip2::Response::Ptr
std::shared_ptr< Response > Ptr
Definition: abstract.h:30
abstract.h
scip2::ResponseProcessor
Definition: response.h:36
scip2::ResponseQT
Definition: quit.h:28
scip2::ResponseProcessor::operator()
void operator()(const boost::posix_time::ptime &time_read, const std::string &echo_back, const std::string &status, std::istream &stream) const
Definition: response.h:59
time_sync.h
scip2::ResponseProcessor::registerResponse
void registerResponse(Response::Ptr response)
Definition: response.h:40


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