-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconnection.cc
179 lines (147 loc) · 5.59 KB
/
connection.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "connection.h"
#include <unistd.h>
#include <chrono>
#include <ctime>
#include <array>
#include <math.h>
#include <iostream>
#include <thread>
using namespace grpc;
using namespace base;
GrpcConnection::GrpcConnection(std::shared_ptr<Channel> channel)
: stub(NetworkService::NewStub(channel))
{
source = std::make_unique<ClientId>();
name_space = std::make_unique<NameSpace>();
source->set_id("my_unique_client_id");
name_space->set_name("vss");
}
void GrpcConnection::subscriber()
{
auto signals = new SignalIds();
// add any number of signals...
{
auto handle = signals->add_signalid();
handle->set_allocated_name(new std::string("Vehicle.Chassis.SteeringWheel.Angle"));
handle->set_allocated_namespace_(new NameSpace(*name_space));
}
{
auto handle = signals->add_signalid();
handle->set_allocated_name(new std::string("Vehicle.Speed"));
handle->set_allocated_namespace_(new NameSpace(*name_space));
}
SubscriberConfig sub_info;
sub_info.set_allocated_clientid(new ClientId(*source));
sub_info.set_allocated_signals(signals);
ClientContext ctx;
Empty empty;
std::cout << "Subscribing" << std::endl;
Signals signals_returned;
std::unique_ptr<ClientReader<Signals>> reader(
stub->SubscribeToSignals(&ctx, sub_info));
while (reader->Read(&signals_returned))
{
for (int i = 0; i < signals_returned.signal_size(); i++)
{
auto name = signals_returned.signal(i).id().name();
std::cout << name << std::endl;
auto value_d = signals_returned.signal(i).double_();
std::cout << value_d << std::endl;
auto value_i = signals_returned.signal(i).integer();
std::cout << value_i << std::endl;
}
}
Status status = reader->Finish();
std::cout << "Subscribing end. Subscribing on invalid signals or stream stopped." << std::endl;
}
void GrpcConnection::publisher()
{
// TODO we could use startvalue here as default
// https://github.com/remotivelabs/remotivelabs-apis/blob/main/proto/common.proto#L34
auto start_value = 12;
auto N = 10;
for (auto i = 0; i < N; i = ((i + 1) % N))
{
// std::cout << i << std::endl;
auto signals = new Signals();
{
auto signal_id = new SignalId();
signal_id->set_allocated_name(new std::string("SteeringAngle129"));
signal_id->set_allocated_namespace_(new NameSpace(*name_space));
auto handle = signals->add_signal();
handle->set_allocated_id(signal_id);
handle->set_integer(start_value + i);
}
{
// append any number of signals here! (duplicate above code)
}
PublisherConfig pub_info;
pub_info.set_allocated_clientid(new ClientId(*source));
pub_info.set_allocated_signals(signals);
pub_info.set_frequency(0);
ClientContext ctx;
Empty empty;
stub->PublishSignals(&ctx, pub_info, &empty);
// TODO we should derive this period from proto buffer,
// https://github.com/remotivelabs/remotivelabs-apis/blob/main/proto/common.proto#L33
usleep(30);
}
}
class MyCustomAuthenticator : public grpc::MetadataCredentialsPlugin
{
public:
MyCustomAuthenticator(const grpc::string &ticket) : ticket_(ticket) {}
grpc::Status GetMetadata(
grpc::string_ref service_url, grpc::string_ref method_name,
const grpc::AuthContext &channel_auth_context,
std::multimap<grpc::string, grpc::string> *metadata) override
{
metadata->insert(std::make_pair("x-api-key", ticket_));
return grpc::Status::OK;
}
private:
grpc::string ticket_;
};
const char CUSTOM_CERTIFICATE[] = R"(
-----BEGIN CERTIFICATE-----
MIIDdTCCAl2gAwIBAgILBAAAAAABFUtaw5QwDQYJKoZIhvcNAQEFBQAwVzELMAkG
A1UEBhMCQkUxGTAXBgNVBAoTEEdsb2JhbFNpZ24gbnYtc2ExEDAOBgNVBAsTB1Jv
b3QgQ0ExGzAZB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
)";
int main(int argc, char *argv[])
{
// use local certificate
grpc::SslCredentialsOptions sslops;
sslops.pem_root_certs = CUSTOM_CERTIFICATE;
auto channel_creds_ = ::grpc::SslCredentials(sslops);
// or use certificate from host
// auto channel_creds_ = ::grpc::SslCredentials(::grpc::SslCredentialsOptions());
auto call_creds = grpc::MetadataCredentialsFromPlugin(
std::unique_ptr<grpc::MetadataCredentialsPlugin>(
new MyCustomAuthenticator(argv[2])));
auto compsited_creds = ::grpc::CompositeChannelCredentials(channel_creds_, call_creds);
grpc::ChannelArguments cargs;
auto connection = new GrpcConnection(CreateChannel(argv[1], compsited_creds));
std::thread subscriber(&GrpcConnection::subscriber, connection);
subscriber.join();
// std::thread publisher(&GrpcConnection::publisher, connection);
// publisher.join();
return 0;
}