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 <map>
25 : #include <memory>
26 : #include <mutex>
27 : #include <string>
28 :
29 : #include "IClientController.h"
30 : #include "IControlClient.h"
31 : #include "IControlIpc.h"
32 : #include "IPrivateMetricsIpc.h"
33 :
34 : namespace firebolt::rialto::client
35 : {
36 : class ClientControllerAccessor : public IClientControllerAccessor
37 : {
38 : public:
39 1 : ~ClientControllerAccessor() override = default;
40 : IClientController &getClientController() const override;
41 : };
42 :
43 : class ClientController : public IClientController, public IControlClient, public IPrivateMetricsIpcClient
44 : {
45 : public:
46 : explicit ClientController(const std::shared_ptr<IControlIpcFactory> &ControlIpcFactory,
47 : const std::shared_ptr<IPrivateMetricsIpcFactory> &privateMetricsIpcFactory);
48 : ~ClientController() override;
49 :
50 : int subscribeSharedMemoryHandle(SharedMemoryHandleObserver observer) override;
51 : void unsubscribeSharedMemoryHandle(int token) override;
52 : bool registerClient(std::weak_ptr<IControlClient> client, ApplicationState &appState) override;
53 : bool unregisterClient(std::weak_ptr<IControlClient> client) override;
54 :
55 : private:
56 : void notifyApplicationState(ApplicationState state) override;
57 : void reportClientMetrics(std::uint64_t sampleId, std::uint32_t reason) override;
58 :
59 : /**
60 : * @brief Initalised the shared memory for media playback.
61 : *
62 : * @retval true on success, false otherwise.
63 : */
64 : bool initSharedMemory();
65 :
66 : /**
67 : * @brief Terminates the shared memory.
68 : */
69 : void termSharedMemory();
70 :
71 : /**
72 : * @brief Coverts a ApplicationState to string.
73 : *
74 : * @param[in] state : The application state.
75 : *
76 : * @retval the application state string, or "" on error.
77 : */
78 : std::string stateToString(ApplicationState state);
79 :
80 : /**
81 : * @brief Forwards new ApplicationState to subscribed clients
82 : *
83 : * @param[in] state : The new application state.
84 : *
85 : */
86 : void changeStateAndNotifyClients(ApplicationState state);
87 :
88 : /**
89 : * @brief Forwards the current shared memory handle to subscribed observers.
90 : */
91 : void notifySharedMemoryHandleObservers();
92 :
93 : /**
94 : * @brief Gets the monotonic timestamp in milliseconds.
95 : */
96 : std::uint64_t getMonotonicTimeMs() const;
97 :
98 : /**
99 : * @brief Gets the epoch timestamp in milliseconds.
100 : */
101 : std::uint64_t getEpochTimeMs() const;
102 :
103 : /**
104 : * @brief Gets accumulated process CPU time in milliseconds.
105 : */
106 : std::uint64_t getProcessCpuTimeMs() const;
107 :
108 : /**
109 : * @brief Gets process RSS memory usage in kilobytes.
110 : */
111 : std::uint64_t getProcessMemoryKb() const;
112 :
113 : /**
114 : * @brief Gets the process name used for metrics reporting.
115 : */
116 : std::string getProcessName() const;
117 :
118 : private:
119 : /**
120 : * @brief Mutex protection for class attributes.
121 : */
122 : std::mutex m_mutex;
123 :
124 : /**
125 : * @brief The current application state
126 : */
127 : ApplicationState m_currentState;
128 :
129 : /**
130 : * @brief Flag indicating if registerRequest has to be sent to Rialto Server
131 : */
132 : bool m_registrationRequired;
133 :
134 : /**
135 : * @brief The shared memory buffer handle.
136 : */
137 : std::shared_ptr<ISharedMemoryHandle> m_shmHandle;
138 :
139 : /**
140 : * @brief The rialto control ipc factory.
141 : */
142 : std::shared_ptr<IControlIpc> m_controlIpc;
143 :
144 : /**
145 : * @brief The rialto private metrics ipc instance.
146 : */
147 : std::shared_ptr<IPrivateMetricsIpc> m_privateMetricsIpc;
148 :
149 : /**
150 : * @brief List of clients to notify.
151 : */
152 : std::list<std::weak_ptr<IControlClient>> m_clients;
153 :
154 : /**
155 : * @brief Registered shared memory handle observers, keyed by subscription token.
156 : */
157 : std::map<int, SharedMemoryHandleObserver> m_shmObservers;
158 :
159 : /**
160 : * @brief Next token to hand out from subscribeSharedMemoryHandle().
161 : */
162 : int m_nextShmObserverToken{0};
163 :
164 : /**
165 : * @brief Serialises subscribe/unsubscribe/notify delivery so unsubscribe cannot return
166 : * while an in-flight notification is still invoking the observer being removed.
167 : * Recursive so an observer may subscribe/unsubscribe itself from within its own
168 : * callback without deadlocking; other threads still block until delivery completes.
169 : */
170 : std::recursive_mutex m_shmNotifyMutex;
171 : };
172 : } // namespace firebolt::rialto::client
173 :
174 : #endif // FIREBOLT_RIALTO_CLIENT_CLIENT_CONTROLLER_H_
|