aboutsummaryrefslogtreecommitdiffstats
path: root/app/AglShellGrpcClient.h
blob: 19cfb753819b9cbf0599f4dfd5e83b4e09f0fcf4 (plain)
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.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Li
#pragma once

#include <cstdio>

#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 "agl_shell.grpc.pb.h"

typedef void (*Callback)(agl_shell_ipc::AppStateResponse app_response, void *data);

class Reader : public grpc::ClientReadReactor<::agl_shell_ipc::AppStateResponse> {
public:
	Reader(agl_shell_ipc::AglShellManagerService::Stub *stub)
		: m_stub(stub)
	{
	}

	void AppStatusState(Callback callback, void *_data)
	{
		::agl_shell_ipc::AppStateRequest request;

		// set up the callback
		m_callback = callback;
		m_data = _data;
		m_stub->async()->AppStatusState(&m_context, &request, this);

		StartRead(&m_app_state);
		StartCall();
	}

	void OnReadDone(bool ok) override
	{
		if (ok) {
			m_callback(m_app_state, m_data);

			// blocks in StartRead() if the server doesn't send
			// antyhing
			StartRead(&m_app_state);
		}
	}

	void SetDone()
	{
		fprintf(stderr, "%s()\n", __func__);
		std::unique_lock<std::mutex> l(m_mutex);
		m_done = true;
	}

	void OnDone(const grpc::Status& s) override
	{
		fprintf(stderr, "%s()\n", __func__);
		std::unique_lock<std::mutex> l(m_mutex);

		m_status = s;

		fprintf(stderr, "%s() done\n", __func__);
		m_cv.notify_one();
	}

	grpc::Status Await()
	{
		std::unique_lock<std::mutex> l(m_mutex);

		m_cv.wait(l, [this] { return m_done; });

		return std::move(m_status);
	}
private:
	grpc::ClientContext m_context;
	::agl_shell_ipc::AppStateResponse m_app_state;
	void *m_data;
	agl_shell_ipc::AglShellManagerService::Stub *m_stub;

	Callback m_callback;


	std::mutex m_mutex;
	std::condition_variable m_cv;
	grpc::Status m_status;
	bool m_done = false;
};

class GrpcClient {
public:
	GrpcClient();
	void WaitForConnected(int wait_time_ms, int tries_timeout);
	bool ActivateApp(const std::string& app_id, const std::string& output_name);
	bool DeactivateApp(const std::string& app_id);
	bool SetAppFloat(const std::string& app_id, int32_t x_pos, int32_t y_pos);
	bool SetAppFullscreen(const std::string& app_id);
	bool SetAppOnOutput(const std::string& app_id, const std::string& output);
	bool SetAppNormal(const std::string& app_id);
	bool SetAppPosition(const std::string& app_id, int32_t x, int32_t y);
	bool SetAppScale(const std::string& app_id, int32_t width, int32_t height);
	std::vector<std::string> GetOutputs();
	void GetAppState();
	void AppStatusState(Callback callback, void *data);
	grpc::Status Wait();

private:
	Reader *reader;
	std::unique_ptr<agl_shell_ipc::AglShellManagerService::Stub> m_stub;
	std::shared_ptr<grpc::Channel> m_channel;
};