aboutsummaryrefslogtreecommitdiffstats
path: root/src/main-grpc.cc
blob: 29e01f158e74736b65cc34fec37420d6f4b369e4 (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
27
28
29
30
31
32<
FILESEXTRAPATHS:append := ":${THISDIR}/qtwayland"

SRC_URI:append = "\
    file://0001-qwaylandwindow-Short-circuit-isExposed.patch \
    file://0001-qwaylandintegration-Keep-previous-check-related-to-i.patch \
    "
h { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */ }
// SPDX-License-Identifier: Apache-2.0
/*
 * Copyright (C) 2023 Konsulko Group
 */

#include <thread>
#include <chrono>
#include <glib.h>
#include <glib-unix.h>

#include "RadioImpl.h"

GMainLoop *main_loop = NULL;

RadioImpl *g_service = NULL;

static gboolean quit_cb(gpointer user_data)
{
    g_info("Quitting...");

    if (main_loop)
        g_idle_add(G_SOURCE_FUNC(g_main_loop_quit), main_loop);
    else
        exit(0);

    return G_SOURCE_REMOVE;
}

void RunGrpcServer(std::shared_ptr<Server> &server)
{
    // Start server and wait for shutdown
    server->Wait();
}

int main(int argc, char *argv[])
{
    main_loop = g_main_loop_new(NULL, FALSE);

    grpc::EnableDefaultHealthCheckService(true);
    grpc::reflection::InitProtoReflectionServerBuilderPlugin();
    ServerBuilder builder;

    // Listen on the given address without any authentication mechanism (for now)
    std::string server_address("localhost:50053");
    builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());

    // Register "service" as the instance through which we'll communicate with
    // clients. In this case it corresponds to a *synchronous* service.
    RadioImpl *service = new RadioImpl();
    if (!service->Detect()) {
        exit(1);
    }
    builder.RegisterService(service);

    // Finally assemble the server.
    std::shared_ptr<Server> server(builder.BuildAndStart());
    if (!server) {
        exit(1);
    }
    std::cout << "Server listening on " << server_address << std::endl;

    g_unix_signal_add(SIGTERM, quit_cb, (gpointer) &server);
    g_unix_signal_add(SIGINT, quit_cb, (gpointer) &server);

    // Start gRPC API server on its own thread
    std::thread grpc_thread(RunGrpcServer, std::ref(server));

    g_main_loop_run(main_loop);

    // Service implementation may have threads blocked from client streaming
    // RPCs, make sure those exit.
    service->Shutdown();

    // Need to set a deadline to avoid blocking on clients doing streaming
    // RPC reads
    server->Shutdown(std::chrono::system_clock::now() + std::chrono::milliseconds(500));

    grpc_thread.join();

    g_main_loop_unref(main_loop);

    return 0;
}