Apollo 10.0
自动驾驶开放平台
client_alignment.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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#pragma once
17
18#include <memory>
19#include <string>
20#include <typeinfo>
21
22#include "grpc++/grpc++.h"
23#include "yaml-cpp/yaml.h"
24
25#include "cyber/cyber.h"
29#include "modules/map/tools/map_datachecker/proto/collection_service.grpc.pb.h"
30
31namespace apollo {
32namespace hdmap {
33
34template <typename REQUEST_TYPE, typename RESPONSE_TYPE>
35class Alignment {
36 public:
38 YAML::Node node = YAML::LoadFile(FLAGS_client_conf_yaml);
39 std::string server_addr =
40 node["grpc_host_port"]["grpc_host"].as<std::string>() + ":" +
41 node["grpc_host_port"]["grpc_port"].as<std::string>();
42 check_period_ = node["alignment"]["check_period"].as<int>();
43 service_stub_ = CollectionCheckerService::NewStub(
44 grpc::CreateChannel(server_addr, grpc::InsecureChannelCredentials()));
45 }
46
47 int SyncStart() {
48 int ret = Start();
49 if (ret != 0) {
50 AERROR << "start alignment failed";
51 return -1;
52 }
53 return PeriodicCheck();
54 }
55
56 int SyncStop() {
57 // stop server
58 return Stop();
59 }
60
62 int ret = 0;
63 while (true) {
64 double progress = 0.0;
65 ret = Check(&progress);
66 if (ret != 0) {
67 AERROR << "alignment check failed";
68 break;
69 }
70 AINFO << "alignment check progress: [" << progress << "]";
71 fprintf(USER_STREAM, "alignment progress: %.2lf%%\n", progress * 100);
72 if (fabs(progress - 1.0) < 1e-8) {
73 AINFO << "diff " << fabs(progress - 1.0);
74 break;
75 }
76 std::this_thread::sleep_for(std::chrono::seconds(check_period_));
77 }
78 if (ret != 0) {
79 return -1;
80 }
81 ret = Stop();
82 if (ret != 0) {
83 AERROR << "alignment stop failed";
84 return -1;
85 }
86 return 0;
87 }
88
89 int GrpcStub(REQUEST_TYPE* request, RESPONSE_TYPE* response) {
90 grpc::Status status = GrpcAlignmentStub(request, response);
91 if (status.error_code() == grpc::StatusCode::UNAVAILABLE) {
92 AERROR << "FATAL Error. Map grpc service is UNAVAILABLE.";
93 fprintf(USER_STREAM, "You should start server first\n");
94 return -1;
95 }
96 AINFO << "response error code: " << response->code();
97 if (response->code() != ErrorCode::SUCCESS) {
98 return ExceptionHandler::ExceptionHandlerFun(response->code());
99 }
100 return 0;
101 }
102
103 private:
104 int Start() {
105 REQUEST_TYPE request;
106 request.set_cmd(CmdType::START);
107 AINFO << "alignment request: "
108 << "cmd: [" << request.cmd() << "]";
109 RESPONSE_TYPE response;
110 return GrpcStub(&request, &response);
111 }
112
113 int Check(double* progress) {
114 REQUEST_TYPE request;
115 request.set_cmd(CmdType::CHECK);
116 AINFO << "alignment request: "
117 << "cmd: [" << request.cmd() << "]";
118 RESPONSE_TYPE response;
119 int ret = GrpcStub(&request, &response);
120 *progress = response.progress();
121 return ret;
122 }
123
124 int Stop() {
125 REQUEST_TYPE request;
126 request.set_cmd(CmdType::STOP);
127 AINFO << "alignment request: "
128 << "cmd: [" << request.cmd() << "]";
129 RESPONSE_TYPE response;
130 return GrpcStub(&request, &response);
131 }
132
133 private:
134 int check_period_;
135
136 protected:
137 std::unique_ptr<CollectionCheckerService::Stub> service_stub_;
138 virtual grpc::Status GrpcAlignmentStub(REQUEST_TYPE* request,
139 RESPONSE_TYPE* response) = 0;
140};
141
142class StaticAlign : public Alignment<StaticAlignRequest, StaticAlignResponse> {
143 grpc::Status GrpcAlignmentStub(StaticAlignRequest* request,
144 StaticAlignResponse* response) {
145 grpc::ClientContext context;
146 return service_stub_->ServiceStaticAlign(&context, *request, response);
147 }
148};
149
150class EightRoute : public Alignment<EightRouteRequest, EightRouteResponse> {
151 grpc::Status GrpcAlignmentStub(EightRouteRequest* request,
152 EightRouteResponse* response) {
153 grpc::ClientContext context;
154 return service_stub_->ServiceEightRoute(&context, *request, response);
155 }
156};
157
158} // namespace hdmap
159} // namespace apollo
int GrpcStub(REQUEST_TYPE *request, RESPONSE_TYPE *response)
std::unique_ptr< CollectionCheckerService::Stub > service_stub_
virtual grpc::Status GrpcAlignmentStub(REQUEST_TYPE *request, RESPONSE_TYPE *response)=0
static int ExceptionHandlerFun(ErrorCode error_code)
#define USER_STREAM
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37