cb8adcbde7805d8c9afe622c8ef341969ce2e285
[src/agl-compositor.git] / grpc-proxy / grpc-async-cb.h
1 /*
2  * Copyright © 2022 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #pragma once
27
28 #include <memory>
29
30 #define GRPC_CALLBACK_API_NONEXPERIMENTAL
31
32 #include <grpc/grpc.h>
33 #include <grpcpp/grpcpp.h>
34 #include <grpcpp/server.h>
35 #include <grpcpp/server_builder.h>
36 #include <grpcpp/server_context.h>
37
38 #include <mutex>
39 #include <condition_variable>
40
41 #include <grpcpp/ext/proto_server_reflection_plugin.h>
42 #include <grpcpp/health_check_service_interface.h>
43
44 #include "shell.h"
45 #include "agl_shell.grpc.pb.h"
46
47 namespace {
48        const char kDefaultGrpcServiceAddress[] = "127.0.0.1:14005";
49 }
50
51 class Lister : public grpc::ServerWriteReactor<::agl_shell_ipc::AppStateResponse> {
52 public:
53         Lister(Shell *aglShell);
54         void OnDone() override;
55         void OnWriteDone(bool ok) override;
56         void NextWrite(void);
57         bool Writting(void);
58 private:
59         Shell *m_shell;
60         bool m_writting;
61 };
62
63 class GrpcServiceImpl final : public agl_shell_ipc::AglShellManagerService::CallbackService {
64 public:
65         GrpcServiceImpl(Shell *aglShell) : m_aglShell(aglShell) {}
66
67         grpc::ServerUnaryReactor *ActivateApp(grpc::CallbackServerContext *context,
68                         const ::agl_shell_ipc::ActivateRequest* request,
69                         ::agl_shell_ipc::ActivateResponse* /*response*/) override;
70
71         grpc::ServerUnaryReactor *DeactivateApp(grpc::CallbackServerContext *context,
72                         const ::agl_shell_ipc::DeactivateRequest* request,
73                         ::agl_shell_ipc::DeactivateResponse* /*response*/) override;
74
75         grpc::ServerUnaryReactor *SetAppFloat(grpc::CallbackServerContext *context,
76                         const ::agl_shell_ipc::FloatRequest* request,
77                         ::agl_shell_ipc::FloatResponse* /*response*/) override;
78
79         grpc::ServerUnaryReactor *SetAppSplit(grpc::CallbackServerContext *context,
80                         const ::agl_shell_ipc::SplitRequest* request,
81                         ::agl_shell_ipc::SplitResponse* /*response*/) override;
82
83         grpc::ServerUnaryReactor *GetOutputs(grpc::CallbackServerContext *context,
84                         const ::agl_shell_ipc::OutputRequest* /* request */,
85                         ::agl_shell_ipc::ListOutputResponse* response) override;
86
87         grpc::ServerUnaryReactor *SetAppNormal(grpc::CallbackServerContext *context,
88                         const ::agl_shell_ipc::NormalRequest* request,
89                         ::agl_shell_ipc::NormalResponse* /*response*/) override;
90
91         grpc::ServerUnaryReactor *SetAppFullscreen(grpc::CallbackServerContext *context,
92                         const ::agl_shell_ipc::FullscreenRequest* request,
93                         ::agl_shell_ipc::FullscreenResponse* /*response*/) override;
94
95         grpc::ServerUnaryReactor *SetAppOnOutput(grpc::CallbackServerContext *context,
96                         const ::agl_shell_ipc::AppOnOutputRequest* request,
97                         ::agl_shell_ipc::AppOnOutputResponse* /*response*/) override;
98
99         grpc::ServerUnaryReactor *SetAppPosition(grpc::CallbackServerContext *context,
100                         const ::agl_shell_ipc::AppPositionRequest* request,
101                         ::agl_shell_ipc::AppPositionResponse* /*response*/) override;
102
103         grpc::ServerUnaryReactor *SetAppScale(grpc::CallbackServerContext *context,
104                         const ::agl_shell_ipc::AppScaleRequest* request,
105                         ::agl_shell_ipc::AppScaleResponse* /*response*/) override;
106
107         grpc::ServerWriteReactor< ::agl_shell_ipc::AppStateResponse>* AppStatusState(
108               ::grpc::CallbackServerContext* /*context*/,
109               const ::agl_shell_ipc::AppStateRequest* /*request*/)  override;
110
111 private:
112        Shell *m_aglShell;
113 };