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_RESPONSE_H 00018 #define SCIP2_RESPONSE_H 00019 00020 #include <boost/asio.hpp> 00021 00022 #include <map> 00023 #include <string> 00024 00025 #include <scip2/response/abstract.h> 00026 #include <scip2/response/parameters.h> 00027 #include <scip2/response/stream.h> 00028 #include <scip2/response/time_sync.h> 00029 #include <scip2/response/quit.h> 00030 00031 namespace scip2 00032 { 00033 class ResponseProcessor 00034 { 00035 protected: 00036 std::map<std::string, Response::Ptr> responses_; 00037 void registerResponse(Response::Ptr response) 00038 { 00039 responses_[response->getCommandCode()] = response; 00040 } 00041 00042 public: 00043 ResponseProcessor() 00044 { 00045 registerResponse(Response::Ptr(new ResponsePP)); 00046 registerResponse(Response::Ptr(new ResponseVV)); 00047 registerResponse(Response::Ptr(new ResponseII)); 00048 registerResponse(Response::Ptr(new ResponseMD)); 00049 registerResponse(Response::Ptr(new ResponseME)); 00050 registerResponse(Response::Ptr(new ResponseTM)); 00051 registerResponse(Response::Ptr(new ResponseQT)); 00052 } 00053 void operator()( 00054 const boost::posix_time::ptime &time_read, 00055 const std::string &echo_back, 00056 const std::string &status, 00057 std::istream &stream) const 00058 { 00059 const std::string command_code(echo_back.substr(0, 2)); 00060 const auto response = responses_.find(command_code); 00061 if (response == responses_.end()) 00062 { 00063 std::cerr << "Unknown response " << command_code << std::endl; 00064 return; 00065 } 00066 (*(response->second))(time_read, echo_back, status, stream); 00067 } 00068 template <typename TResponse> 00069 void registerCallback(typename TResponse::Callback cb) 00070 { 00071 const auto response = responses_.find(TResponse().getCommandCode()); 00072 assert(response != responses_.end()); 00073 auto response_downcast = std::dynamic_pointer_cast<TResponse>(response->second); 00074 assert(response_downcast); 00075 00076 response_downcast->registerCallback(cb); 00077 } 00078 }; 00079 00080 } // namespace scip2 00081 00082 #endif // SCIP2_RESPONSE_H