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 "Controller.h"
21 : #include "RialtoServerManagerLogging.h"
22 : #include <algorithm>
23 : #include <string>
24 : #include <utility>
25 :
26 : namespace rialto::servermanager::ipc
27 : {
28 21 : Controller::Controller(std::unique_ptr<common::ISessionServerAppManager> &sessionServerAppManager)
29 21 : : m_sessionServerAppManager{sessionServerAppManager}
30 : {
31 : }
32 :
33 42 : Controller::~Controller()
34 : {
35 21 : std::map<int, std::unique_ptr<Client>> clients;
36 : {
37 21 : std::unique_lock<std::mutex> lock{m_clientMutex};
38 21 : m_isShuttingDown = true;
39 21 : clients.swap(m_clients);
40 : }
41 21 : clients.clear();
42 42 : }
43 :
44 19 : bool Controller::createClient(int serverId, int appMgmtSocket)
45 : {
46 19 : std::unique_lock<std::mutex> lock{m_clientMutex};
47 19 : if (m_isShuttingDown)
48 : {
49 0 : RIALTO_SERVER_MANAGER_LOG_WARN("Not creating client for serverId: %d, controller is shutting down", serverId);
50 0 : return false;
51 : }
52 19 : if (m_clients.find(serverId) == m_clients.end())
53 : {
54 18 : auto newClient = std::make_unique<Client>(m_sessionServerAppManager, serverId, appMgmtSocket);
55 18 : if (newClient->connect())
56 : {
57 17 : m_clients.emplace(std::make_pair(serverId, std::move(newClient)));
58 17 : return true;
59 : }
60 1 : return false;
61 18 : }
62 1 : return false;
63 19 : }
64 :
65 1 : void Controller::removeClient(int serverId)
66 : {
67 1 : std::unique_lock<std::mutex> lock{m_clientMutex};
68 1 : m_clients.erase(serverId);
69 : }
70 :
71 6 : bool Controller::performSetState(int serverId, const firebolt::rialto::common::SessionServerState &state)
72 : {
73 6 : std::unique_lock<std::mutex> lock{m_clientMutex};
74 6 : auto client = m_clients.find(serverId);
75 6 : if (client != m_clients.end())
76 : {
77 4 : return client->second->performSetState(state);
78 : }
79 2 : return false;
80 6 : }
81 :
82 2 : bool Controller::performSetConfiguration(int serverId, const firebolt::rialto::common::SessionServerState &initialState,
83 : const std::string &socketName, const std::string &clientDisplayName,
84 : const firebolt::rialto::common::MaxResourceCapabilitites &maxResource,
85 : const unsigned int socketPermissions, const std::string &socketOwner,
86 : const std::string &socketGroup, const std::string &appName)
87 : {
88 2 : std::unique_lock<std::mutex> lock{m_clientMutex};
89 2 : auto client = m_clients.find(serverId);
90 2 : if (client != m_clients.end())
91 : {
92 2 : return client->second->performSetConfiguration(initialState, socketName, clientDisplayName, maxResource,
93 2 : socketPermissions, socketOwner, socketGroup, appName);
94 : }
95 0 : return false;
96 2 : }
97 :
98 2 : bool Controller::performSetConfiguration(int serverId, const firebolt::rialto::common::SessionServerState &initialState,
99 : int socketFd, const std::string &clientDisplayName,
100 : const firebolt::rialto::common::MaxResourceCapabilitites &maxResource,
101 : const std::string &appName)
102 : {
103 2 : std::unique_lock<std::mutex> lock{m_clientMutex};
104 2 : auto client = m_clients.find(serverId);
105 2 : if (client != m_clients.end())
106 : {
107 2 : return client->second->performSetConfiguration(initialState, socketFd, clientDisplayName, maxResource, appName);
108 : }
109 0 : return false;
110 2 : }
111 :
112 3 : bool Controller::performPing(int serverId, int pingId)
113 : {
114 3 : std::unique_lock<std::mutex> lock{m_clientMutex};
115 3 : auto client = m_clients.find(serverId);
116 3 : if (client != m_clients.end())
117 : {
118 2 : return client->second->performPing(pingId);
119 : }
120 1 : return false;
121 3 : }
122 :
123 2 : bool Controller::setLogLevels(const service::LoggingLevels &logLevels) const
124 : {
125 2 : std::unique_lock<std::mutex> lock{m_clientMutex};
126 2 : return std::all_of(m_clients.begin(), m_clients.end(),
127 6 : [&](const auto &client) { return client.second->setLogLevels(logLevels); });
128 2 : }
129 : } // namespace rialto::servermanager::ipc
|