Apollo 11.0
自动驾驶开放平台
grpc_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 *****************************************************************************/
16
23
24#include <chrono>
25
27
28namespace apollo {
29namespace v2x {
30
31using ::apollo::v2x::StatusResponse;
32using ::apollo::v2x::obu::ObuRsi;
33using ::apollo::v2x::obu::ObuTrafficLight;
34using grpc::Status;
35
37 : node_(::apollo::cyber::CreateNode("v2x_grpc_server")) {
38 CHECK(!!node_);
39 // _monitor = cybertron::MetricController::Instance();
40 AINFO << "GrpcServerImpl initial success";
41 init_flag_ = true;
42}
43
44grpc::Status GrpcServerImpl::SendV2xRSI(grpc::ServerContext * /* context */,
45 const ObuRsi *request,
46 StatusResponse *response) {
47 ADEBUG << "Received SendV2xRSI request from client!";
48 ADEBUG << request->DebugString();
49
50 if (!request->has_header()) {
51 response->set_status(false);
52 response->set_info("error no header in RSI request");
53 AERROR << "SendV2xRSI request has no header";
54 return Status::CANCELLED;
55 }
56 {
57 std::lock_guard<std::mutex> guard(rsi_mutex_);
58 latest_rsi_.CopyFrom(*request);
59 auto obu_header = latest_rsi_.mutable_header();
60 if (obu_header->has_timestamp_sec()) {
61 AINFO << "Obu Timestamp: " << std::setiosflags(std::ios::fixed)
62 << std::setprecision(11) << obu_header->timestamp_sec();
63 AINFO << "SVB Timestamp: " << std::setiosflags(std::ios::fixed)
64 << std::setprecision(11) << ::apollo::cyber::Time::Now().ToSecond();
65 obu_header->set_timestamp_sec(::apollo::cyber::Time::Now().ToSecond());
66 }
67 rsi_refresh_ = true;
68 }
69 rsi_condition_.notify_one();
70
71 response->set_status(true);
72 AINFO << "SendV2xRSI response success.";
73 return Status::OK;
74}
76 grpc::ServerContext *, const apollo::v2x::V2XObstacles *request,
77 StatusResponse *response) {
78 ADEBUG << "Received SendPerceptionObstacles request from client!";
79 {
80 std::lock_guard<std::mutex> guard(obstacles_mutex_);
81 latest_obstacles_.CopyFrom(*request);
82 response->set_status(true);
83 obs_refresh_ = true;
84 ADEBUG << "SendPerceptionObstacles response success.";
85 }
86 obs_condition_.notify_one();
87 return Status::OK;
88}
89
91 grpc::ServerContext * /* context */, const ObuTrafficLight *request,
92 StatusResponse *response) {
93 ADEBUG << "Received SendV2xTrafficLight request from client!";
94 ADEBUG << request->DebugString();
95
96 int num_current_light = request->road_traffic_light_size();
97
98 if (0 == num_current_light) {
99 response->set_status(false);
100 response->set_info("error v2x current lane traffic light size == 0");
101 AERROR << "SendV2xTrafficLight request no current lane traffic light.";
102 return Status::CANCELLED;
103 }
104 if (!request->has_header()) {
105 response->set_status(false);
106 response->set_info(
107 "error no header in IntersectionTrafficLightData request");
108 AERROR << "SendV2xTrafficLight request has no header";
109 return Status::CANCELLED;
110 }
111 {
112 std::lock_guard<std::mutex> guard(traffic_light_mutex_);
113 latest_traffic_light_.CopyFrom(*request);
114 auto obu_header = latest_traffic_light_.mutable_header();
115 if (obu_header->has_timestamp_sec()) {
116 obu_header->set_timestamp_sec(::apollo::cyber::Time::Now().ToSecond());
117 }
118 refresh_ = true;
119 }
120 traffic_light_condition_.notify_one();
121 auto cur_junction_id = request->intersection_id();
122 ADEBUG << "recieved the junction id " << cur_junction_id;
123 response->set_status(true);
124 return Status::OK;
125}
126
128 grpc::ServerContext *context, const ::apollo::v2x::ObuAlarm *request,
129 StatusResponse *response) {
130 ADEBUG << "Received SendOBUAlarm request from client!";
131 if (request->error_num() != ::apollo::v2x::ErrorCode::OBUID) {
132 } else {
133 AERROR << "OBU id:" << request->error_msg();
134 }
135 response->set_status(true);
136 return Status::OK;
137}
138
139void GrpcServerImpl::GetMsgFromGrpc(std::shared_ptr<ObuRsi> *ptr) {
140 if (nullptr == ptr) {
141 return;
142 }
143 std::unique_lock<std::mutex> guard(rsi_mutex_);
144 rsi_condition_.wait(guard, [this] { return rsi_refresh_; });
145 rsi_refresh_ = false;
146 *ptr = std::make_shared<ObuRsi>();
147 if (nullptr == *ptr) {
148 return;
149 }
150 (*ptr)->CopyFrom(latest_rsi_);
151}
152
154 std::shared_ptr<::apollo::v2x::V2XObstacles> *ptr) {
155 if (nullptr == ptr) {
156 return;
157 }
158 std::unique_lock<std::mutex> guard(obstacles_mutex_);
159 obs_condition_.wait(guard, [this] { return obs_refresh_; });
160 obs_refresh_ = false;
161 *ptr = std::make_shared<::apollo::v2x::V2XObstacles>();
162 if (nullptr == *ptr) {
163 return;
164 }
165 (*ptr)->CopyFrom(latest_obstacles_);
166}
167
168void GrpcServerImpl::GetMsgFromGrpc(std::shared_ptr<ObuTrafficLight> *ptr) {
169 if (nullptr == ptr) {
170 return;
171 }
172 std::unique_lock<std::mutex> guard(traffic_light_mutex_);
173 if (!traffic_light_condition_.wait_for(
174 guard, std::chrono::milliseconds(FLAGS_msg_timeout),
175 [this] { return refresh_; })) {
176 AERROR << "GetMsgFromGrpc IntersectionTrafficLightData is timeout";
177 *ptr = nullptr;
178 return;
179 }
180 *ptr = std::make_shared<ObuTrafficLight>();
181 if (nullptr == *ptr) {
182 return;
183 }
184 (*ptr)->CopyFrom(latest_traffic_light_);
185 latest_traffic_light_.Clear();
186 refresh_ = false;
187}
188
189} // namespace v2x
190} // namespace apollo
static Time Now()
get the current time.
Definition time.cc:57
double ToSecond() const
convert time to second.
Definition time.cc:77
grpc::Status SendPerceptionObstacles(grpc::ServerContext *context, const apollo::v2x::V2XObstacles *request, StatusResponse *response) override
grpc::Status SendObuAlarm(grpc::ServerContext *context, const ::apollo::v2x::ObuAlarm *request, ::apollo::v2x::StatusResponse *response) override
grpc::Status SendV2xRSI(grpc::ServerContext *context, const ::apollo::v2x::obu::ObuRsi *request, ::apollo::v2x::StatusResponse *response)
grpc::Status SendV2xTrafficLight(grpc::ServerContext *context, const ::apollo::v2x::obu::ObuTrafficLight *request, ::apollo::v2x::StatusResponse *response) override
void GetMsgFromGrpc(std::shared_ptr<::apollo::v2x::obu::ObuTrafficLight > *ptr)
define v2x proxy module and onboard unit interface grpc implement
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37
The gflags used by v2x proxy module