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 : #ifndef FIREBOLT_RIALTO_CLIENT_CLIENT_CONTROLLER_H_
21 : #define FIREBOLT_RIALTO_CLIENT_CLIENT_CONTROLLER_H_
22 :
23 : #include <list>
24 : #include <memory>
25 : #include <mutex>
26 : #include <string>
27 :
28 : #include "IClientController.h"
29 : #include "IControlClient.h"
30 : #include "IControlIpc.h"
31 : #include "IPrivateMetricsIpc.h"
32 :
33 : namespace firebolt::rialto::client
34 : {
35 : class ClientControllerAccessor : public IClientControllerAccessor
36 : {
37 : public:
38 1 : ~ClientControllerAccessor() override = default;
39 : IClientController &getClientController() const override;
40 : };
41 :
42 : class ClientController : public IClientController, public IControlClient, public IPrivateMetricsIpcClient
43 : {
44 : public:
45 : explicit ClientController(const std::shared_ptr<IControlIpcFactory> &ControlIpcFactory,
46 : const std::shared_ptr<IPrivateMetricsIpcFactory> &privateMetricsIpcFactory);
47 : ~ClientController() override;
48 :
49 : std::shared_ptr<ISharedMemoryHandle> getSharedMemoryHandle() override;
50 : bool registerClient(std::weak_ptr<IControlClient> client, ApplicationState &appState) override;
51 : bool unregisterClient(std::weak_ptr<IControlClient> client) override;
52 :
53 : private:
54 : void notifyApplicationState(ApplicationState state) override;
55 : void reportClientMetrics(std::uint64_t sampleId, std::uint32_t reason) override;
56 :
57 : /**
58 : * @brief Initalised the shared memory for media playback.
59 : *
60 : * @retval true on success, false otherwise.
61 : */
62 : bool initSharedMemory();
63 :
64 : /**
65 : * @brief Terminates the shared memory.
66 : */
67 : void termSharedMemory();
68 :
69 : /**
70 : * @brief Coverts a ApplicationState to string.
71 : *
72 : * @param[in] state : The application state.
73 : *
74 : * @retval the application state string, or "" on error.
75 : */
76 : std::string stateToString(ApplicationState state);
77 :
78 : /**
79 : * @brief Forwards new ApplicationState to subscribed clients
80 : *
81 : * @param[in] state : The new application state.
82 : *
83 : */
84 : void changeStateAndNotifyClients(ApplicationState state);
85 :
86 : /**
87 : * @brief Gets the monotonic timestamp in milliseconds.
88 : */
89 : std::uint64_t getMonotonicTimeMs() const;
90 :
91 : /**
92 : * @brief Gets the epoch timestamp in milliseconds.
93 : */
94 : std::uint64_t getEpochTimeMs() const;
95 :
96 : /**
97 : * @brief Gets accumulated process CPU time in milliseconds.
98 : */
99 : std::uint64_t getProcessCpuTimeMs() const;
100 :
101 : /**
102 : * @brief Gets process RSS memory usage in kilobytes.
103 : */
104 : std::uint64_t getProcessMemoryKb() const;
105 :
106 : /**
107 : * @brief Gets the process name used for metrics reporting.
108 : */
109 : std::string getProcessName() const;
110 :
111 : private:
112 : /**
113 : * @brief Mutex protection for class attributes.
114 : */
115 : std::mutex m_mutex;
116 :
117 : /**
118 : * @brief The current application state
119 : */
120 : ApplicationState m_currentState;
121 :
122 : /**
123 : * @brief Flag indicating if registerRequest has to be sent to Rialto Server
124 : */
125 : bool m_registrationRequired;
126 :
127 : /**
128 : * @brief The shared memory buffer handle.
129 : */
130 : std::shared_ptr<ISharedMemoryHandle> m_shmHandle;
131 :
132 : /**
133 : * @brief The rialto control ipc factory.
134 : */
135 : std::shared_ptr<IControlIpc> m_controlIpc;
136 :
137 : /**
138 : * @brief The rialto private metrics ipc instance.
139 : */
140 : std::shared_ptr<IPrivateMetricsIpc> m_privateMetricsIpc;
141 :
142 : /**
143 : * @brief List of clients to notify.
144 : */
145 : std::list<std::weak_ptr<IControlClient>> m_clients;
146 : };
147 : } // namespace firebolt::rialto::client
148 :
149 : #endif // FIREBOLT_RIALTO_CLIENT_CLIENT_CONTROLLER_H_
|