Apollo  6.0
Open source self driving car software
client_base.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_SERVICE_CLIENT_BASE_H_
18 #define CYBER_SERVICE_CLIENT_BASE_H_
19 
20 #include <chrono>
21 #include <string>
22 
23 #include "cyber/common/macros.h"
24 
25 namespace apollo {
26 namespace cyber {
27 
33 class ClientBase {
34  public:
40  explicit ClientBase(const std::string& service_name)
41  : service_name_(service_name) {}
42  virtual ~ClientBase() {}
43 
47  virtual void Destroy() = 0;
48 
52  const std::string& ServiceName() const { return service_name_; }
53 
57  virtual bool ServiceIsReady() const = 0;
58 
59  protected:
60  std::string service_name_;
61 
62  bool WaitForServiceNanoseconds(std::chrono::nanoseconds time_out) {
63  bool has_service = false;
64  auto step_duration = std::chrono::nanoseconds(5 * 1000 * 1000);
65  while (time_out.count() > 0) {
67  ->service_manager()
68  ->HasService(service_name_);
69  if (!has_service) {
70  std::this_thread::sleep_for(step_duration);
71  time_out -= step_duration;
72  } else {
73  break;
74  }
75  }
76  return has_service;
77  }
78 };
79 
80 } // namespace cyber
81 } // namespace apollo
82 
83 #endif // CYBER_SERVICE_CLIENT_BASE_H_
ClientBase(const std::string &service_name)
Construct a new Client Base object.
Definition: client_base.h:40
bool WaitForServiceNanoseconds(std::chrono::nanoseconds time_out)
Definition: client_base.h:62
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
virtual ~ClientBase()
Definition: client_base.h:42
const std::string & ServiceName() const
Get the service name.
Definition: client_base.h:52
Base class of Client.
Definition: client_base.h:33
virtual void Destroy()=0
Destroy the Client.
std::string service_name_
Definition: client_base.h:60
virtual bool ServiceIsReady() const =0
Ensure whether there is any Service named service_name_