19cfb753819b9cbf0599f4dfd5e83b4e09f0fcf4
[apps/camera-gstreamer.git] / app / AglShellGrpcClient.h
1 #pragma once
2
3 #include <cstdio>
4
5 #include <mutex>
6 #include <condition_variable>
7 #include <grpc/grpc.h>
8 #include <grpcpp/grpcpp.h>
9 #include <grpcpp/server.h>
10 #include <grpcpp/server_builder.h>
11 #include <grpcpp/server_context.h>
12
13 #include <grpcpp/ext/proto_server_reflection_plugin.h>
14 #include <grpcpp/health_check_service_interface.h>
15
16 #include "agl_shell.grpc.pb.h"
17
18 typedef void (*Callback)(agl_shell_ipc::AppStateResponse app_response, void *data);
19
20 class Reader : public grpc::ClientReadReactor<::agl_shell_ipc::AppStateResponse> {
21 public:
22         Reader(agl_shell_ipc::AglShellManagerService::Stub *stub)
23                 : m_stub(stub)
24         {
25         }
26
27         void AppStatusState(Callback callback, void *_data)
28         {
29                 ::agl_shell_ipc::AppStateRequest request;
30
31                 // set up the callback
32                 m_callback = callback;
33                 m_data = _data;
34                 m_stub->async()->AppStatusState(&m_context, &request, this);
35
36                 StartRead(&m_app_state);
37                 StartCall();
38         }
39
40         void OnReadDone(bool ok) override
41         {
42                 if (ok) {
43                         m_callback(m_app_state, m_data);
44
45                         // blocks in StartRead() if the server doesn't send
46                         // antyhing
47                         StartRead(&m_app_state);
48                 }
49         }
50
51         void SetDone()
52         {
53                 fprintf(stderr, "%s()\n", __func__);
54                 std::unique_lock<std::mutex> l(m_mutex);
55                 m_done = true;
56         }
57
58         void OnDone(const grpc::Status& s) override
59         {
60                 fprintf(stderr, "%s()\n", __func__);
61                 std::unique_lock<std::mutex> l(m_mutex);
62
63                 m_status = s;
64
65                 fprintf(stderr, "%s() done\n", __func__);
66                 m_cv.notify_one();
67         }
68
69         grpc::Status Await()
70         {
71                 std::unique_lock<std::mutex> l(m_mutex);
72
73                 m_cv.wait(l, [this] { return m_done; });
74
75                 return std::move(m_status);
76         }
77 private:
78         grpc::ClientContext m_context;
79         ::agl_shell_ipc::AppStateResponse m_app_state;
80         void *m_data;
81         agl_shell_ipc::AglShellManagerService::Stub *m_stub;
82
83         Callback m_callback;
84
85
86         std::mutex m_mutex;
87         std::condition_variable m_cv;
88         grpc::Status m_status;
89         bool m_done = false;
90 };
91
92 class GrpcClient {
93 public:
94         GrpcClient();
95         void WaitForConnected(int wait_time_ms, int tries_timeout);
96         bool ActivateApp(const std::string& app_id, const std::string& output_name);
97         bool DeactivateApp(const std::string& app_id);
98         bool SetAppFloat(const std::string& app_id, int32_t x_pos, int32_t y_pos);
99         bool SetAppFullscreen(const std::string& app_id);
100         bool SetAppOnOutput(const std::string& app_id, const std::string& output);
101         bool SetAppNormal(const std::string& app_id);
102         bool SetAppPosition(const std::string& app_id, int32_t x, int32_t y);
103         bool SetAppScale(const std::string& app_id, int32_t width, int32_t height);
104         std::vector<std::string> GetOutputs();
105         void GetAppState();
106         void AppStatusState(Callback callback, void *data);
107         grpc::Status Wait();
108
109 private:
110         Reader *reader;
111         std::unique_ptr<agl_shell_ipc::AglShellManagerService::Stub> m_stub;
112         std::shared_ptr<grpc::Channel> m_channel;
113 };
114