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 0 : case firebolt::rialto::common::SessionServerState::SUSPENDED:
42 0 : return rialto::SessionServerState::SUSPENDED;
43 : }
44 0 : return rialto::SessionServerState::ERROR;
45 : }
46 : } // namespace
47 :
48 : namespace firebolt::rialto::server::ipc
49 : {
50 6 : ApplicationManagementServer::ApplicationManagementServer(
51 : const std::shared_ptr<firebolt::rialto::ipc::IServerFactory> &serverFactory,
52 : const std::shared_ptr<firebolt::rialto::server::ipc::IServerManagerModuleServiceFactory> &serverManagerModuleFactory,
53 6 : service::ISessionServerManager &sessionServerManager)
54 6 : : m_ipcServer{serverFactory->create()}, m_service{serverManagerModuleFactory->create(sessionServerManager)}
55 : {
56 : }
57 :
58 12 : ApplicationManagementServer::~ApplicationManagementServer()
59 : {
60 6 : stop();
61 6 : if (m_ipcServerThread.joinable())
62 : {
63 1 : m_ipcServerThread.join();
64 : }
65 12 : }
66 :
67 6 : bool ApplicationManagementServer::initialize(int socket)
68 : {
69 6 : RIALTO_SERVER_LOG_INFO("Initializing ApplicationManagementServer, socket: %d", socket);
70 6 : if (!m_ipcServer)
71 : {
72 0 : RIALTO_SERVER_LOG_ERROR("Failed to initialize ApplicationManagementServer - Ipc server instance is NULL");
73 0 : return false;
74 : }
75 :
76 12 : m_ipcClient = m_ipcServer->addClient(socket, std::bind(&ApplicationManagementServer::onClientDisconnected, this,
77 6 : std::placeholders::_1));
78 6 : if (!m_ipcClient)
79 : {
80 1 : RIALTO_SERVER_LOG_ERROR("Failed to initialize ApplicationManagementServer - Client is NULL");
81 1 : return false;
82 : }
83 5 : m_ipcClient->exportService(m_service);
84 5 : RIALTO_SERVER_LOG_MIL("ApplicationManagementServer initialized");
85 5 : return true;
86 : }
87 :
88 2 : bool ApplicationManagementServer::sendStateChangedEvent(const common::SessionServerState &state)
89 : {
90 2 : if (!m_ipcClient->isConnected())
91 : {
92 1 : return false;
93 : }
94 1 : auto stateChangedEvent = std::make_shared<::rialto::StateChangedEvent>();
95 1 : stateChangedEvent->set_sessionserverstate(convertSessionServerState(state));
96 1 : m_ipcClient->sendEvent(stateChangedEvent);
97 1 : return true;
98 : }
99 :
100 1 : void ApplicationManagementServer::start()
101 : {
102 1 : m_ipcServerThread = std::thread(
103 1 : [this]()
104 : {
105 1 : while (m_ipcServer->process() && m_ipcClient && m_ipcClient->isConnected())
106 : {
107 0 : m_ipcServer->wait(-1);
108 : }
109 1 : });
110 : }
111 :
112 7 : void ApplicationManagementServer::stop()
113 : {
114 7 : if (m_ipcClient && m_ipcClient->isConnected())
115 : {
116 1 : m_ipcClient->disconnect();
117 : }
118 7 : }
119 :
120 0 : void ApplicationManagementServer::onClientDisconnected(const std::shared_ptr<::firebolt::rialto::ipc::IClient> &client)
121 : {
122 0 : RIALTO_SERVER_LOG_WARN("Rialto Server Manager disconnected");
123 : }
124 : } // namespace firebolt::rialto::server::ipc
|