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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
//include stuff here
#include <cstdio>
#include <time.h>
#include <mutex>
#include <condition_variable>
#include <grpc/grpc.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>
#include <grpcpp/server_context.h>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
#include <grpcpp/health_check_service_interface.h>
#include "AglShellGrpcClient.h"
#include "agl_shell.grpc.pb.h"
namespace {
const char kDefaultGrpcServiceAddress[] = "127.0.0.1:14005";
}
GrpcClient::GrpcClient(bool wait_for_ready)
{
int retries = 10;
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts))
throw std::runtime_error("Failed CLOCK_MONOTONIC");
auto channel = grpc::CreateChannel(kDefaultGrpcServiceAddress,
grpc::InsecureChannelCredentials());
ts.tv_nsec = 500 * 1000 * 1000;
ts.tv_sec = 0;
wait_for_ready_ = wait_for_ready;
deadline_ = std::chrono::milliseconds(2000);
auto state = channel->GetState(true);
// artificial delay otherwise to be sure req calls succeed
while (retries-- > 0) {
state = channel->GetState(true);
if (state == GRPC_CHANNEL_READY)
break;
nanosleep(&ts, NULL);
}
if (state != GRPC_CHANNEL_READY)
fprintf(stderr, "WARNING: channel not in ready state: %d\n", state);
m_stub = agl_shell_ipc::AglShellManagerService::NewStub(channel);
reader = new Reader(m_stub.get());
}
bool
GrpcClient::ActivateApp(const std::string& app_id, const std::string& output_name)
{
agl_shell_ipc::ActivateRequest request;
request.set_app_id(app_id);
request.set_output_name(output_name);
grpc::ClientContext context;
::agl_shell_ipc::ActivateResponse reply;
if (wait_for_ready_) {
context.set_wait_for_ready(true);
context.set_deadline(std::chrono::system_clock::now() + deadline_);
}
grpc::Status status = m_stub->ActivateApp(&context, request, &reply);
return status.ok();
}
bool
GrpcClient::DeactivateApp(const std::string& app_id)
{
agl_shell_ipc::DeactivateRequest request;
request.set_app_id(app_id);
grpc::ClientContext context;
::agl_shell_ipc::DeactivateResponse reply;
if (wait_for_ready_) {
context.set_wait_for_ready(true);
context.set_deadline(std::chrono::system_clock::now() + deadline_);
}
grpc::Status status = m_stub->DeactivateApp(&context, request, &reply);
return status.ok();
}
bool
GrpcClient::SetAppFloat(const std::string& app_id, int32_t x_pos, int32_t y_pos)
{
agl_shell_ipc::FloatRequest request;
request.set_app_id(app_id);
request.set_x_pos(x_pos);
request.set_y_pos(y_pos);
grpc::ClientContext context;
::agl_shell_ipc::FloatResponse reply;
if (wait_for_ready_) {
context.set_wait_for_ready(true);
context.set_deadline(std::chrono::system_clock::now() + deadline_);
}
grpc::Status status = m_stub->SetAppFloat(&context, request, &reply);
return status.ok();
}
bool
GrpcClient::SetAppNormal(const std::string& app_id)
{
agl_shell_ipc::NormalRequest request;
request.set_app_id(app_id);
grpc::ClientContext context;
::agl_shell_ipc::NormalResponse reply;
if (wait_for_ready_) {
context.set_wait_for_ready(true);
context.set_deadline(std::chrono::system_clock::now() + deadline_);
}
grpc::Status status = m_stub->SetAppNormal(&context, request, &reply);
return status.ok();
}
bool
GrpcClient::SetAppFullscreen(const std::string& app_id)
{
agl_shell_ipc::FullscreenRequest request;
request.set_app_id(app_id);
grpc::ClientContext context;
::agl_shell_ipc::FullscreenResponse reply;
if (wait_for_ready_) {
context.set_wait_for_ready(true);
context.set_deadline(std::chrono::system_clock::now() + deadline_);
}
grpc::Status status = m_stub->SetAppFullscreen(&context, request, &reply);
return status.ok();
}
bool
GrpcClient::SetAppOnOutput(const std::string& app_id, const std::string &output)
{
agl_shell_ipc::AppOnOutputRequest request;
request.set_app_id(app_id);
request.set_output(output);
grpc::ClientContext context;
::agl_shell_ipc::AppOnOutputResponse reply;
if (wait_for_ready_) {
context.set_wait_for_ready(true);
context.set_deadline(std::chrono::system_clock::now() + deadline_);
}
grpc::Status status = m_stub->SetAppOnOutput(&context, request, &reply);
return status.ok();
}
bool
GrpcClient::SetAppPosition(const std::string& app_id, int32_t x, int32_t y)
{
agl_shell_ipc::AppPositionRequest request;
request.set_app_id(app_id);
request.set_x(x);
request.set_y(y);
grpc::ClientContext context;
::agl_shell_ipc::AppPositionResponse reply;
if (wait_for_ready_) {
context.set_wait_for_ready(true);
context.set_deadline(std::chrono::system_clock::now() + deadline_);
}
grpc::Status status = m_stub->SetAppPosition(&context, request, &reply);
return status.ok();
}
bool
GrpcClient::SetAppScale(const std::string& app_id, int32_t width, int32_t height)
{
agl_shell_ipc::AppScaleRequest request;
request.set_app_id(app_id);
request.set_width(width);
request.set_height(height);
grpc::ClientContext context;
::agl_shell_ipc::AppScaleResponse reply;
if (wait_for_ready_) {
context.set_wait_for_ready(true);
context.set_deadline(std::chrono::system_clock::now() + deadline_);
}
grpc::Status status = m_stub->SetAppScale(&context, request, &reply);
return status.ok();
}
bool
GrpcClient::SetAppSplit(const std::string& app_id, uint32_t orientation,
int32_t width, int32_t sticky, const std::string& output_name)
{
agl_shell_ipc::SplitRequest request;
request.set_app_id(app_id);
request.set_output_name(output_name);
request.set_tile_orientation(orientation);
request.set_width(width);
request.set_sticky(sticky);
grpc::ClientContext context;
::agl_shell_ipc::SplitResponse reply;
if (wait_for_ready_) {
context.set_wait_for_ready(true);
context.set_deadline(std::chrono::system_clock::now() + deadline_);
}
grpc::Status status = m_stub->SetAppSplit(&context, request, &reply);
return status.ok();
}
grpc::Status
GrpcClient::Wait(void)
{
return reader->Await();
}
void
GrpcClient::AppStatusState(Callback callback)
{
reader->AppStatusState(callback);
}
std::vector<std::string>
GrpcClient::GetOutputs()
{
grpc::ClientContext context;
std::vector<std::string> v;
::agl_shell_ipc::OutputRequest request;
::agl_shell_ipc::ListOutputResponse response;
if (wait_for_ready_) {
context.set_wait_for_ready(true);
context.set_deadline(std::chrono::system_clock::now() + deadline_);
}
grpc::Status status = m_stub->GetOutputs(&context, request, &response);
if (!status.ok())
return std::vector<std::string>();
for (int i = 0; i < response.outputs_size(); i++) {
::agl_shell_ipc::OutputResponse rresponse = response.outputs(i);
v.push_back(rresponse.name());
}
return v;
}
|