From: Marius Vlad Date: Wed, 25 Oct 2023 20:41:46 +0000 (+0300) Subject: homescreen: Re-work gRPC client start-up X-Git-Tag: 17.90.0~4 X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?p=apps%2Fhomescreen.git;a=commitdiff_plain;h=537937138e9855408c6b8af4331725c8a1d52705 homescreen: Re-work gRPC client start-up And start the gRPC client as soon as possible and just wait for a connected status after we bounded to the agl-shell extension/interface. Bug-AGL: SPEC-4912 Signed-off-by: Marius Vlad Change-Id: I26da89dd6aaf1b5c2357d70dcaed7622c1892c31 --- diff --git a/homescreen/src/AglShellGrpcClient.cpp b/homescreen/src/AglShellGrpcClient.cpp index ee7510f..f3c72ca 100644 --- a/homescreen/src/AglShellGrpcClient.cpp +++ b/homescreen/src/AglShellGrpcClient.cpp @@ -25,28 +25,31 @@ namespace { GrpcClient::GrpcClient() { - struct timespec ts; - auto channel = grpc::CreateChannel(kDefaultGrpcServiceAddress, + m_channel = grpc::CreateChannel(kDefaultGrpcServiceAddress, grpc::InsecureChannelCredentials()); + // init the stub here + m_stub = agl_shell_ipc::AglShellManagerService::NewStub(m_channel); + reader = new Reader(m_stub.get()); +} + +void +GrpcClient::WaitForConnected(int wait_time_ms, int tries_timeout) +{ + struct timespec ts; + grpc_connectivity_state state; + int try_ = 0; + clock_gettime(CLOCK_MONOTONIC, &ts); ts.tv_sec = 0; ts.tv_nsec = 500 * 1000 * 1000; // 500ms - bool try_to_connect = true; - grpc_connectivity_state state = channel->GetState(try_to_connect); - - while (state != GRPC_CHANNEL_READY) { - state = channel->GetState(try_to_connect); - + while (((state = m_channel->GetState(true)) != GRPC_CHANNEL_READY) && + try_++ < tries_timeout) { HMI_DEBUG("HomesScreen", "waiting for channel state to be ready, current state %d", state); nanosleep(&ts, NULL); } - - // init the stub here - m_stub = agl_shell_ipc::AglShellManagerService::NewStub(channel); - reader = new Reader(m_stub.get()); } diff --git a/homescreen/src/AglShellGrpcClient.h b/homescreen/src/AglShellGrpcClient.h index bb74377..19cfb75 100644 --- a/homescreen/src/AglShellGrpcClient.h +++ b/homescreen/src/AglShellGrpcClient.h @@ -92,6 +92,7 @@ private: 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); @@ -108,5 +109,6 @@ public: private: Reader *reader; std::unique_ptr m_stub; + std::shared_ptr m_channel; }; diff --git a/homescreen/src/main.cpp b/homescreen/src/main.cpp index 66ddb75..ec0e1c5 100644 --- a/homescreen/src/main.cpp +++ b/homescreen/src/main.cpp @@ -321,8 +321,7 @@ run_in_thread(GrpcClient *client) static void load_agl_shell_app(QPlatformNativeInterface *native, QQmlApplicationEngine *engine, - struct shell_data shell_data, const char *screen_name, - bool is_demo, GrpcClient *client) + struct shell_data shell_data, const char *screen_name, bool is_demo) { QScreen *screen = nullptr; HomescreenHandler *homescreenHandler = shell_data.homescreenHandler; @@ -418,6 +417,9 @@ int main(int argc, char *argv[]) // we need to have an app_id app.setDesktopFileName("homescreen"); + GrpcClient *client = new GrpcClient(); + // create a new thread to listner for gRPC events + std::thread th = std::thread(run_in_thread, client); register_agl_shell(native, &shell_data); if (!shell_data.shell) { @@ -444,10 +446,6 @@ int main(int argc, char *argv[]) std::shared_ptr agl_shell{shell_data.shell, agl_shell_destroy}; - GrpcClient *client = new GrpcClient(); - // create a new thread to listner for gRPC events - std::thread th = std::thread(run_in_thread, client); - // Import C++ class to QML qmlRegisterType("HomeScreen", 1, 0, "StatusBarModel"); qmlRegisterType("MasterVolume", 1, 0, "MasterVolume"); @@ -459,6 +457,9 @@ int main(int argc, char *argv[]) shell_data.homescreenHandler = homescreenHandler; shell_data.homescreenHandler->setGrpcClient(client); + // blocks until we're sure connected with the server + HMI_DEBUG("HomescreenHandler", "Checking if connected to the gRPC server..."); + client->WaitForConnected(500, 10); client->AppStatusState(app_status_callback, homescreenHandler); QQmlApplicationEngine engine; @@ -470,7 +471,7 @@ int main(int argc, char *argv[]) context->setContextProperty("bluetooth", new Bluetooth(false, context)); load_agl_shell_app(native, &engine, shell_data, - screen_name, is_demo_val, client); + screen_name, is_demo_val); return app.exec(); }