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