[dali_2.3.25] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / network / common / network-performance-server.h
1 #ifndef DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_SERVER_H
2 #define DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_SERVER_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
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
21 // EXTERNAL INCLUDES
22 #include <dali/devel-api/threading/mutex.h>
23 #include <dali/public-api/common/dali-vector.h>
24 #include <pthread.h>
25
26 // INTERNAL INCLUDES
27 #include <dali/devel-api/adaptor-framework/event-thread-callback.h>
28 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
29 #include <dali/internal/network/common/network-performance-client.h>
30 #include <dali/internal/system/common/environment-options.h>
31
32 namespace Dali
33 {
34 namespace Internal
35 {
36 namespace Adaptor
37 {
38 class SocketInterface;
39 class PerformanceMarker;
40
41 /**
42  *  @brief  The class listens for incoming connections on a dedicated thread.
43  *
44  *  When a new connection is established a client thread is spawned to
45  *  handle that connection, along with a NetworkPerformanceClient object.
46  *  The NetworkPerformanceClient object performs processing of incoming
47  *  commands and holds the per-client state information for performance monitoring.
48  *
49  *  Server->Start()
50  *  - Open socket
51  *  - Spawns a thread to listen for incoming connections
52  *  <---- New connection
53  *  - Spawns a client thread to communicate with new client
54  *
55  *  Server->Stop()
56  *  - Stops listening thread
57  *  - Stops all client threads
58  */
59 class NetworkPerformanceServer : public ClientSendDataInterface
60 {
61 public:
62   /**
63    * @brief Constructor
64    * @param[in] adaptorServices adaptor internal services
65    * @param[in] logOptions log options
66    */
67   NetworkPerformanceServer(AdaptorInternalServices& adaptorServices, const EnvironmentOptions& logOptions);
68
69   /**
70    * @brief Start the server, to be called form Dali main thread
71    * @pre Can only be called form Dali main thread
72    */
73   void Start();
74
75   /**
76    * @brief Stop the server
77    * @pre Can only be called form Dali main thread
78    */
79   void Stop();
80
81   /**
82    * @return true if the server is running
83    */
84   bool IsRunning() const;
85
86   /**
87    * @brief Transmit a marker to any clients are listening for this marker.
88    * @param[in] marker performance marker
89    * @param[in] description marker description
90    * @pre Can be called from any thread
91    *
92    */
93   void TransmitMarker(const PerformanceMarker& marker, const char* const description);
94
95   /**
96    * Destructor
97    */
98   ~NetworkPerformanceServer();
99
100 protected: // ClientSendDataInterface
101   /**
102    * @copydoc ClientSendDataInterface::TriggerMainThreadAutomation()
103    */
104   void TriggerMainThreadAutomation(CallbackBase* callback) override;
105
106   /**
107    * @copydoc ClientSendDataInterface::ClientSendDataInterface()
108    */
109   void SendData(const char* const data, unsigned int bufferSizeInBytes, unsigned int clientId) override;
110
111   void AutomationCallback();
112
113 private:
114   /**
115    * Helper for the thread calling the entry function.
116    * @param[in] This A pointer to the current RenderThread object
117    */
118   static void* ConnectionListenerFunc(void* This)
119   {
120     (static_cast<NetworkPerformanceServer*>(This))->ConnectionListener();
121     return NULL;
122   }
123
124   /**
125    * Helper for the thread calling the entry function.
126    * @param[in] This A pointer to the current RenderThread object
127    */
128   static void* ClientThreadFunc(void* data);
129
130   /**
131    * @brief Client thread function
132    * @param client network client object
133    */
134   void ClientThread(NetworkPerformanceClient* client);
135
136   /**
137    * @brief Stop all client threads
138    */
139   void StopClients();
140
141   /**
142    * @brief Waits for new connections to be made
143    */
144   void ConnectionListener();
145
146   /**
147    * @brief Add a new client to the client list
148    * @param clientSocket client socket
149    * @param clientThread client thread
150    * @return client
151    */
152   NetworkPerformanceClient* AddClient(SocketInterface* clientSocket, pthread_t* clientThread);
153
154   /**
155    * @brief Delete a client from the client list
156    * @param client  network client
157    */
158   void DeleteClient(NetworkPerformanceClient* client);
159
160   NetworkPerformanceServer(const NetworkPerformanceServer&);            ///< undefined copy constructor
161   NetworkPerformanceServer& operator=(const NetworkPerformanceServer&); ///< undefined assignment operator
162
163   SocketFactoryInterface&              mSocketFactory;  ///< used to create sockets
164   std::unique_ptr<EventThreadCallback> mTrigger;        ///< For waking up main thread
165   CallbackBase*                        mClientCallback; ///< Wrong thread callback!
166
167   const EnvironmentOptions&               mLogOptions;           ///< log options
168   Dali::Vector<NetworkPerformanceClient*> mClients;              ///< list of connected clients
169   pthread_t                               mServerThread;         ///< thread that listens for new connections
170   SocketInterface*                        mListeningSocket;      ///< socket used to listen for new connections
171   Dali::Mutex                             mClientListMutex;      ///< mutex
172   unsigned int                            mClientUniqueId;       ///< increments for every client connection
173   volatile unsigned int                   mClientCount;          ///< client count
174   bool                                    mLogFunctionInstalled; ///< whether the log function is installed
175 };
176
177 } // namespace Adaptor
178
179 } // namespace Internal
180
181 } // namespace Dali
182
183 #endif //DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_SERVER_H