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 : RIALTO_SERVER_LOG_MIL("ApplicationManagementServer initialized");
83 5 : return true;
84 : }
85 :
86 2 : bool ApplicationManagementServer::sendStateChangedEvent(const common::SessionServerState &state)
87 : {
88 2 : if (!m_ipcClient->isConnected())
89 : {
90 1 : return false;
91 : }
92 1 : auto stateChangedEvent = std::make_shared<::rialto::StateChangedEvent>();
93 1 : stateChangedEvent->set_sessionserverstate(convertSessionServerState(state));
94 1 : m_ipcClient->sendEvent(stateChangedEvent);
95 1 : return true;
96 : }
97 :
98 1 : void ApplicationManagementServer::start()
99 : {
100 1 : m_ipcServerThread = std::thread(
101 1 : [this]()
102 : {
103 1 : while (m_ipcServer->process() && m_ipcClient && m_ipcClient->isConnected())
104 : {
105 0 : m_ipcServer->wait(-1);
106 : }
107 1 : });
108 : }
109 :
110 7 : void ApplicationManagementServer::stop()
111 : {
112 7 : if (m_ipcClient && m_ipcClient->isConnected())
113 : {
114 1 : m_ipcClient->disconnect();
115 : }
116 7 : }
117 :
118 0 : void ApplicationManagementServer::onClientDisconnected(const std::shared_ptr<::firebolt::rialto::ipc::IClient> &client)
119 : {
120 0 : RIALTO_SERVER_LOG_WARN("Rialto Server Manager disconnected");
121 : }
122 : } // namespace firebolt::rialto::server::ipc
|