Merge "Changed 'virtual' function override declarations to 'override' in automated...
[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) 2019 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 <pthread.h>
23 #include <dali/devel-api/threading/mutex.h>
24 #include <dali/public-api/common/dali-vector.h>
25
26 // INTERNAL INCLUDES
27 #include <dali/internal/system/common/environment-options.h>
28 #include <dali/internal/network/common/network-performance-client.h>
29 #include <dali/internal/adaptor/common/adaptor-internal-services.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39
40 class SocketInterface;
41 class PerformanceMarker;
42
43 /**
44  *  @brief  The class listens for incoming connections on a dedicated thread.
45  *
46  *  When a new connection is established a client thread is spawned to
47  *  handle that connection, along with a NetworkPerformanceClient object.
48  *  The NetworkPerformanceClient object performs processing of incoming
49  *  commands and holds the per-client state information for performance monitoring.
50  *
51  *  Server->Start()
52  *  - Open socket
53  *  - Spawns a thread to listen for incoming connections
54  *  <---- New connection
55  *  - Spawns a client thread to communicate with new client
56  *
57  *  Server->Stop()
58  *  - Stops listening thread
59  *  - Stops all client threads
60  */
61 class NetworkPerformanceServer : public ClientSendDataInterface
62 {
63
64 public:
65
66   /**
67    * @brief Constructor
68    * @param[in] adaptorServices adaptor internal services
69    * @param[in] logOptions log options
70    */
71   NetworkPerformanceServer( AdaptorInternalServices& adaptorServices, const EnvironmentOptions& logOptions );
72
73
74   /**
75    * @brief Start the server, to be called form Dali main thread
76    * @pre Can only be called form Dali main thread
77    */
78   void Start();
79
80   /**
81    * @brief Stop the server
82    * @pre Can only be called form Dali main thread
83    */
84   void Stop();
85
86   /**
87    * @return true if the server is running
88    */
89   bool IsRunning() const;
90
91   /**
92    * @brief Transmit a marker to any clients are listening for this marker.
93    * @param[in] marker performance marker
94    * @param[in] description marker description
95    * @pre Can be called from any thread
96    *
97    */
98   void TransmitMarker( const PerformanceMarker& marker, const char* const description );
99
100   /**
101    * Destructor
102    */
103    ~NetworkPerformanceServer();
104
105 protected:  // ClientSendDataInterface
106
107   /**
108    * @copydoc ClientSendDataInterface::ClientSendDataInterface()
109    */
110   void SendData( const char* const data, unsigned int bufferSizeInBytes, unsigned int clientId ) override;
111
112 private:
113
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   const EnvironmentOptions& mLogOptions;                  ///< log options
165   Dali::Vector< NetworkPerformanceClient* > mClients;     ///< list of connected clients
166   pthread_t mServerThread;                                ///< thread that listens for new connections
167   SocketInterface* mListeningSocket;                      ///< socket used to listen for new connections
168   Dali::Mutex mClientListMutex;                           ///< mutex
169   unsigned int mClientUniqueId;                           ///< increments for every client connection
170   volatile unsigned int mClientCount;                     ///< client count
171   bool mLogFunctionInstalled;                             ///< whether the log function is installed
172
173 };
174
175
176 } // namespace Internal
177
178 } // namespace Adaptor
179
180 } // namespace Dali
181
182 #endif //DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_SERVER_H