Apollo  6.0
Open source self driving car software
node_service_impl.h
Go to the documentation of this file.
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  *****************************************************************************/
16 
17 #ifndef CYBER_NODE_NODE_SERVICE_IMPL_H_
18 #define CYBER_NODE_NODE_SERVICE_IMPL_H_
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
25 #include "cyber/service/client.h"
26 #include "cyber/service/service.h"
28 
29 namespace apollo {
30 namespace cyber {
31 
32 class Node;
33 
40  public:
41  friend class Node;
42 
48  explicit NodeServiceImpl(const std::string& node_name)
49  : node_name_(node_name) {
50  attr_.set_host_name(common::GlobalData::Instance()->HostName());
51  attr_.set_process_id(common::GlobalData::Instance()->ProcessId());
52  attr_.set_node_name(node_name);
53  auto node_id = common::GlobalData::RegisterNode(node_name);
54  attr_.set_node_id(node_id);
55  }
56 
60  NodeServiceImpl() = delete;
61 
67 
68  private:
69  template <typename Request, typename Response>
70  auto CreateService(const std::string& service_name,
72  service_callback) ->
73  typename std::shared_ptr<Service<Request, Response>>;
74 
75  template <typename Request, typename Response>
76  auto CreateClient(const std::string& service_name) ->
77  typename std::shared_ptr<Client<Request, Response>>;
78 
79  std::vector<std::weak_ptr<ServiceBase>> service_list_;
80  std::vector<std::weak_ptr<ClientBase>> client_list_;
81  std::string node_name_;
82  proto::RoleAttributes attr_;
83 };
84 
85 template <typename Request, typename Response>
86 auto NodeServiceImpl::CreateService(
87  const std::string& service_name,
89  service_callback) ->
90  typename std::shared_ptr<Service<Request, Response>> {
91  auto service_ptr = std::make_shared<Service<Request, Response>>(
92  node_name_, service_name, service_callback);
93  RETURN_VAL_IF(!service_ptr->Init(), nullptr);
94 
95  service_list_.emplace_back(service_ptr);
96  attr_.set_service_name(service_name);
97  auto service_id = common::GlobalData::RegisterService(service_name);
98  attr_.set_service_id(service_id);
99  service_discovery::TopologyManager::Instance()->service_manager()->Join(
100  attr_, RoleType::ROLE_SERVER);
101  return service_ptr;
102 }
103 
104 template <typename Request, typename Response>
105 auto NodeServiceImpl::CreateClient(const std::string& service_name) ->
106  typename std::shared_ptr<Client<Request, Response>> {
107  auto client_ptr =
108  std::make_shared<Client<Request, Response>>(node_name_, service_name);
109  RETURN_VAL_IF(!client_ptr->Init(), nullptr);
110 
111  client_list_.emplace_back(client_ptr);
112  attr_.set_service_name(service_name);
113  auto service_id = common::GlobalData::RegisterService(service_name);
114  attr_.set_service_id(service_id);
115  service_discovery::TopologyManager::Instance()->service_manager()->Join(
116  attr_, RoleType::ROLE_CLIENT);
117  return client_ptr;
118 }
119 
120 } // namespace cyber
121 } // namespace apollo
122 
123 #endif // CYBER_NODE_NODE_SERVICE_IMPL_H_
Definition: node.h:31
NodeServiceImpl()=delete
Forbid default-constructor.
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
#define RETURN_VAL_IF(condition, val)
Definition: log.h:114
NodeServiceImpl(const std::string &node_name)
Construct a new Node Service Impl object.
Definition: node_service_impl.h:48
static uint64_t RegisterNode(const std::string &node_name)
std::function< void(const std::shared_ptr< Request > &, std::shared_ptr< Response > &)> ServiceCallback
Definition: service.h:45
Node is the fundamental building block of Cyber RT. every module contains and communicates through th...
Definition: node.h:44
static uint64_t RegisterService(const std::string &service)
~NodeServiceImpl()
Destroy the Node Service Impl object.
Definition: node_service_impl.h:66
The implementation for Node to create Objects connected by Param. e.g. Param Server and Client...
Definition: node_service_impl.h:39