Apollo 10.0
自动驾驶开放平台
obu_interface_grpc_impl.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 *****************************************************************************/
21
22#include <memory>
23#include <string>
24#include <utility>
25
27
28namespace apollo {
29namespace v2x {
30
31using ::apollo::v2x::CarStatus;
32using ::apollo::v2x::obu::ObuRsi;
33using ::apollo::v2x::obu::ObuTrafficLight;
34using grpc::Server;
35using grpc::ServerBuilder;
36using grpc::ServerContext;
37
39 : grpc_client_(new GrpcClientImpl(grpc::CreateChannel(
40 FLAGS_grpc_client_host + ":" + FLAGS_grpc_client_port,
41 grpc::InsecureChannelCredentials()))) {
42 AINFO << "ObuInterFaceGrpcImpl Start Construct.";
43 cli_init_ = grpc_client_->InitFlag();
44 grpc_server_.reset(new GrpcServerImpl());
45 srv_init_ = grpc_server_->InitFlag();
46 CHECK(InitialClient());
47 CHECK(InitialServer());
48 init_succ_ = true;
49}
50
52 {
53 std::unique_lock<std::mutex> lck(mutex_);
54 exit_flag_ = true;
55 }
56 condition_.notify_all();
57 if (!!thread_grpc_ && thread_grpc_->joinable()) {
58 thread_grpc_->join();
59 }
60 AINFO << "close obu interface success";
61}
62
63bool ObuInterFaceGrpcImpl::InitialClient() { return cli_init_; }
64
66 if (!srv_init_) {
67 return false;
68 }
69 thread_grpc_.reset(new std::thread([this]() { this->ThreadRunServer(); }));
70 return thread_grpc_ != nullptr;
71}
72
73void ObuInterFaceGrpcImpl::ThreadRunServer() {
74 std::unique_lock<std::mutex> lck(mutex_);
75 auto start = std::chrono::steady_clock::now();
76 std::string server_address(FLAGS_grpc_server_host + ":" +
77 FLAGS_grpc_server_port);
78 ServerBuilder builder;
79 // Listen on the given address without any authentication mechanism.
80 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
81 // Register "service" as the instance through which we'll communicate with
82 // clients. In this case it corresponds to an *synchronous* service.
83 builder.RegisterService(grpc_server_.get());
84 // Finally assemble the server.
85 auto tmp = builder.BuildAndStart();
86 server_ = std::move(tmp);
87 auto end = std::chrono::steady_clock::now();
88 std::chrono::duration<double> time_used = end - start;
89 AINFO << "ObuInterFaceGrpcImpl grpc server has listening on : "
90 << server_address << " time used : " << time_used.count();
91 condition_.wait(lck, [&]() { return exit_flag_; });
92}
93
95 std::shared_ptr<ObuTrafficLight> *msg) {
96 grpc_server_->GetMsgFromGrpc(msg);
97}
98
100 std::shared_ptr<::apollo::v2x::V2XObstacles> *msg) {
101 grpc_server_->GetMsgFromGrpc(msg);
102}
103
104void ObuInterFaceGrpcImpl::GetV2xRsiFromObu(std::shared_ptr<ObuRsi> *msg) {
105 grpc_server_->GetMsgFromGrpc(msg);
106}
107
109 const std::shared_ptr<CarStatus> &msg) {
110 grpc_client_->SendMsgToGrpc(msg);
111}
112} // namespace v2x
113} // namespace apollo
void GetV2xRsiFromObu(std::shared_ptr<::apollo::v2x::obu::ObuRsi > *msg) override
void GetV2xTrafficLightFromObu(std::shared_ptr<::apollo::v2x::obu::ObuTrafficLight > *msg) override
void GetV2xObstaclesFromObu(std::shared_ptr<::apollo::v2x::V2XObstacles > *msg) override
void SendCarStatusToObu(const std::shared_ptr<::apollo::v2x::CarStatus > &msg) override
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37
define v2x proxy module and onboard unit interface grpc impl class
The gflags used by v2x proxy module