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 2023 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 "ControlService.h"
21 : #include "IHeartbeatProcedure.h"
22 : #include "RialtoServerLogging.h"
23 : #include <utility>
24 :
25 : namespace firebolt::rialto::server::service
26 : {
27 9 : ControlService::ControlService(const std::shared_ptr<IControlServerInternalFactory> &controlServerInternalFactory)
28 9 : : m_currentState{ApplicationState::UNKNOWN}, m_controlServerInternalFactory{controlServerInternalFactory}
29 : {
30 9 : RIALTO_SERVER_LOG_DEBUG("entry:");
31 : }
32 :
33 7 : void ControlService::addControl(int controlId, const std::shared_ptr<IControlClientServerInternal> &client)
34 : {
35 7 : RIALTO_SERVER_LOG_INFO("Creating new Control with id: %d", controlId);
36 7 : auto controlServerInternal{m_controlServerInternalFactory->createControlServerInternal(controlId, client)};
37 7 : std::unique_lock<std::mutex> lock{m_mutex};
38 7 : controlServerInternal->setApplicationState(m_currentState);
39 7 : m_controls.emplace(controlId, controlServerInternal);
40 : }
41 :
42 2 : void ControlService::removeControl(int controlId)
43 : {
44 2 : RIALTO_SERVER_LOG_INFO("Removing Control with id: %d", controlId);
45 2 : std::unique_lock<std::mutex> lock{m_mutex};
46 2 : m_controls.erase(controlId);
47 : }
48 :
49 4 : bool ControlService::ack(int controlId, std::uint32_t id)
50 : {
51 4 : std::unique_lock<std::mutex> lock{m_mutex};
52 4 : auto controlIter = m_controls.find(controlId);
53 4 : if (m_controls.end() == controlIter)
54 : {
55 3 : RIALTO_SERVER_LOG_ERROR("Control with id: %d not found", controlId);
56 3 : return false;
57 : }
58 1 : controlIter->second->ack(id);
59 1 : return true;
60 4 : }
61 :
62 4 : void ControlService::setApplicationState(const ApplicationState &state)
63 : {
64 4 : std::unique_lock<std::mutex> lock{m_mutex};
65 4 : m_currentState = state;
66 5 : for (const auto &control : m_controls)
67 : {
68 1 : control.second->setApplicationState(state);
69 : }
70 4 : }
71 :
72 1 : bool ControlService::ping(const std::shared_ptr<IHeartbeatProcedure> &heartbeatProcedure)
73 : {
74 1 : std::unique_lock<std::mutex> lock{m_mutex};
75 2 : for (const auto &control : m_controls)
76 : {
77 1 : control.second->ping(heartbeatProcedure->createHandler());
78 : }
79 1 : return true;
80 : }
81 : } // namespace firebolt::rialto::server::service
|