Apollo 11.0
自动驾驶开放平台
parameter_server.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
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 *****************************************************************************/
17
18#include "cyber/common/log.h"
19#include "cyber/node/node.h"
21
22namespace apollo {
23namespace cyber {
24
25ParameterServer::ParameterServer(const std::shared_ptr<Node>& node)
26 : node_(node) {
27 auto name = node_->Name();
28 get_parameter_service_ = node_->CreateService<ParamName, Param>(
29 FixParameterServiceName(name, GET_PARAMETER_SERVICE_NAME),
30 [this](const std::shared_ptr<ParamName>& request,
31 std::shared_ptr<Param>& response) {
32 std::lock_guard<std::mutex> lock(param_map_mutex_);
33 if (param_map_.find(request->value()) != param_map_.end()) {
34 response->CopyFrom(param_map_[request->value()]);
35 } else {
36 AINFO << "GetParameterService: [" << request->value() << "] not set";
37 }
38 });
39
40 set_parameter_service_ = node_->CreateService<Param, BoolResult>(
41 FixParameterServiceName(name, SET_PARAMETER_SERVICE_NAME),
42 [this](const std::shared_ptr<Param>& request,
43 std::shared_ptr<BoolResult>& response) {
44 std::lock_guard<std::mutex> lock(param_map_mutex_);
45 param_map_[request->name()] = *request;
46 response->set_value(true);
47 });
48
49 list_parameters_service_ = node_->CreateService<NodeName, Params>(
50 FixParameterServiceName(name, LIST_PARAMETERS_SERVICE_NAME),
51 [this](const std::shared_ptr<NodeName>& request,
52 std::shared_ptr<Params>& response) {
53 std::lock_guard<std::mutex> lock(param_map_mutex_);
54 for (auto& item : param_map_) {
55 auto param = response->add_param();
56 param->CopyFrom(item.second);
57 }
58 });
59}
60
62 std::lock_guard<std::mutex> lock(param_map_mutex_);
63 param_map_[parameter.Name()] = parameter.ToProtoParam();
64}
65
66bool ParameterServer::GetParameter(const std::string& parameter_name,
67 Parameter* parameter) {
68 std::lock_guard<std::mutex> lock(param_map_mutex_);
69 auto ite = param_map_.find(parameter_name);
70 if (ite == param_map_.end()) {
71 return false;
72 }
73 parameter->FromProtoParam(ite->second);
74 return true;
75}
76
77void ParameterServer::ListParameters(std::vector<Parameter>* parameters) {
78 std::lock_guard<std::mutex> lock(param_map_mutex_);
79 for (auto& item : param_map_) {
80 Parameter parameter;
81 parameter.FromProtoParam(item.second);
82 parameters->emplace_back(parameter);
83 }
84}
85
86} // namespace cyber
87} // namespace apollo
void ListParameters(std::vector< Parameter > *parameters)
get all the parameters
apollo::cyber::proto::ParamName ParamName
apollo::cyber::proto::Param Param
bool GetParameter(const std::string &parameter_name, Parameter *parameter)
Get the Parameter object
apollo::cyber::proto::NodeName NodeName
ParameterServer(const std::shared_ptr< Node > &node)
Construct a new ParameterServer object
void SetParameter(const Parameter &parmeter)
Set the Parameter object
A Parameter holds an apollo::cyber::proto::Param, It's more human-readable, you can use basic-value t...
Definition parameter.h:42
void FromProtoParam(const Param &param)
Parse a cyber::proto::Param object to cyber::parameter::Parameter object
Definition parameter.cc:112
Param ToProtoParam() const
Parse a cyber::parameter::Parameter object to cyber::proto::Param object
Definition parameter.cc:114
const std::string Name() const
Get the Parameter name
Definition parameter.h:348
#define AINFO
Definition log.h:42
constexpr auto SET_PARAMETER_SERVICE_NAME
constexpr auto LIST_PARAMETERS_SERVICE_NAME
constexpr auto GET_PARAMETER_SERVICE_NAME
class register implement
Definition arena_queue.h:37