Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2022 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #include "ApplicationManagementServer.h"
21 : #include "IServerManagerModuleServiceFactory.h"
22 : #include "RialtoServerLogging.h"
23 : #include <IIpcServerFactory.h>
24 :
25 : namespace
26 : {
27 1 : rialto::SessionServerState convertSessionServerState(const firebolt::rialto::common::SessionServerState &state)
28 : {
29 1 : switch (state)
30 : {
31 0 : case firebolt::rialto::common::SessionServerState::UNINITIALIZED:
32 0 : return rialto::SessionServerState::UNINITIALIZED;
33 0 : case firebolt::rialto::common::SessionServerState::INACTIVE:
34 0 : return rialto::SessionServerState::INACTIVE;
35 1 : case firebolt::rialto::common::SessionServerState::ACTIVE:
36 1 : return rialto::SessionServerState::ACTIVE;
37 0 : case firebolt::rialto::common::SessionServerState::NOT_RUNNING:
38 0 : return rialto::SessionServerState::NOT_RUNNING;
39 0 : case firebolt::rialto::common::SessionServerState::ERROR:
40 0 : return rialto::SessionServerState::ERROR;
41 : }
42 0 : return rialto::SessionServerState::ERROR;
43 : }
44 : } // namespace
45 :
46 : namespace firebolt::rialto::server::ipc
47 : {
48 6 : ApplicationManagementServer::ApplicationManagementServer(
49 : const std::shared_ptr<firebolt::rialto::ipc::IServerFactory> &serverFactory,
50 : const std::shared_ptr<firebolt::rialto::server::ipc::IServerManagerModuleServiceFactory> &serverManagerModuleFactory,
51 6 : service::ISessionServerManager &sessionServerManager)
52 6 : : m_ipcServer{serverFactory->create()}, m_service{serverManagerModuleFactory->create(sessionServerManager)}
53 : {
54 : }
55 :
56 12 : ApplicationManagementServer::~ApplicationManagementServer()
57 : {
58 6 : stop();
59 6 : if (m_ipcServerThread.joinable())
60 : {
61 1 : m_ipcServerThread.join();
62 : }
63 12 : }
64 :
65 6 : bool ApplicationManagementServer::initialize(int socket)
66 : {
67 6 : RIALTO_SERVER_LOG_INFO("Initializing ApplicationManagementServer, socket: %d", socket);
68 6 : if (!m_ipcServer)
69 : {
70 0 : RIALTO_SERVER_LOG_ERROR("Failed to initialize ApplicationManagementServer - Ipc server instance is NULL");
71 0 : return false;
72 : }
73 :
74 12 : m_ipcClient = m_ipcServer->addClient(socket, std::bind(&ApplicationManagementServer::onClientDisconnected, this,
75 6 : std::placeholders::_1));
76 6 : if (!m_ipcClient)
77 : {
78 1 : RIALTO_SERVER_LOG_ERROR("Failed to initialize ApplicationManagementServer - Client is NULL");
79 1 : return false;
80 : }
81 5 : m_ipcClient->exportService(m_service);
82 5 : return true;
83 : }
84 :
85 2 : bool ApplicationManagementServer::sendStateChangedEvent(const common::SessionServerState &state)
86 : {
87 2 : if (!m_ipcClient->isConnected())
88 : {
89 1 : return false;
90 : }
91 1 : auto stateChangedEvent = std::make_shared<::rialto::StateChangedEvent>();
92 1 : stateChangedEvent->set_sessionserverstate(convertSessionServerState(state));
93 1 : m_ipcClient->sendEvent(stateChangedEvent);
94 1 : return true;
95 : }
96 :
97 1 : void ApplicationManagementServer::start()
98 : {
99 2 : m_ipcServerThread = std::thread(
100 2 : [this]()
101 : {
102 1 : while (m_ipcServer->process() && m_ipcClient && m_ipcClient->isConnected())
103 : {
104 0 : m_ipcServer->wait(-1);
105 : }
106 1 : });
107 : }
108 :
109 7 : void ApplicationManagementServer::stop()
110 : {
111 7 : if (m_ipcClient && m_ipcClient->isConnected())
112 : {
113 1 : m_ipcClient->disconnect();
114 : }
115 7 : }
116 :
117 0 : void ApplicationManagementServer::onClientDisconnected(const std::shared_ptr<::firebolt::rialto::ipc::IClient> &client)
118 : {
119 0 : RIALTO_SERVER_LOG_INFO("Rialto Server Manager disconnected");
120 : }
121 : } // namespace firebolt::rialto::server::ipc
|