Apollo 10.0
自动驾驶开放平台
client_base.h
浏览该文件的文档.
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
25namespace apollo {
26namespace cyber {
27
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) {
66 has_service = service_discovery::TopologyManager::Instance()
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_
Base class of Client
Definition client_base.h:33
ClientBase(const std::string &service_name)
Construct a new Client Base object
Definition client_base.h:40
virtual void Destroy()=0
Destroy the Client
const std::string & ServiceName() const
Get the service name
Definition client_base.h:52
bool WaitForServiceNanoseconds(std::chrono::nanoseconds time_out)
Definition client_base.h:62
virtual bool ServiceIsReady() const =0
Ensure whether there is any Service named service_name_
class register implement
Definition arena_queue.h:37