646b5944ea9caec147e3bab80af8ee73599064b9
[platform/core/uifw/dali-adaptor.git] / adaptors / base / performance-logging / networking / network-performance-client.h
1 #ifndef __DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_CLIENT_H__
2 #define __DALI_INTERNAL_ADAPTOR_NETWORK_PERFORMANCE_CLIENT_H__
3
4 /*
5  * Copyright (c) 2015 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 // INTERNAL INCLUDES
22 #include <base/performance-logging/performance-marker.h>
23 #include <trigger-event-factory-interface.h>
24 #include <base/performance-logging/networking/client-send-data-interface.h>
25 #include <base/interfaces/socket-factory-interface.h>
26
27 namespace boost
28 {
29 class thread;
30 }
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 namespace Adaptor
39 {
40
41 /**
42  *  @brief Network Performance client
43  *
44  *  Every time a client connects to Dali, a NetworkPerformanceClient object is created.
45  *  It is responsible for processing incoming commands, and storing the client state
46  *  (e.g. what performance markers it wants).
47  *
48  *  Certain commands such as dump-scene need to be run on the main Dali event thread.
49  *  To achieve this, a trigger event is used which executes a function on the main thread.
50  *  The sendDataInterface is then used with the client id to transmit the data to the client.
51  *  The reason for using a client id is because the client
52  *  can be deleted in between receiving a command and sending a response.
53  *  E.g.
54  *  NetworkPerformanceClient (own thread, id 5)  <---  Dump Scene Command
55  *  delete NetworkPerformanceClient              <---  Connection closed
56  *  MainThread. Send scene data to client 5. Client 5 has been deleted so don't send the data.
57  *
58  */
59 class NetworkPerformanceClient
60 {
61 public:
62
63   /**
64    * @brief Constructor
65    * @param socket socket interface
66    * @param clientId unique client id
67    * @param triggerEventFactory used to create trigger events
68    * @param sendDataInterface used to send data to the socket from main thread
69    * @param SocketFactoryInterface used to delete the socket when the client is destroyed
70    */
71   NetworkPerformanceClient( SocketInterface *socket,
72                             unsigned int clientId,
73                             TriggerEventFactoryInterface& triggerEventFactory,
74                             ClientSendDataInterface& sendDataInterface,
75                             SocketFactoryInterface& socketFactory );
76
77   /**
78    * @brief Destructor
79    */
80   ~NetworkPerformanceClient();
81
82   /**
83    * @return client unique id
84    */
85   unsigned int GetId() const;
86
87   /**
88    * @return socket interface
89    */
90   SocketInterface& GetSocket();
91
92   /**
93    * @brief Write data to a socket. Can be called from any thread
94    * @copydoc Dali::SocketInterface::Send
95    */
96   bool WriteSocket( const void* buffer, unsigned int bufferSizeInBytes );
97
98   /**
99    * @brief Process a command
100    * @param buffer pointer to command data
101    * @param bufferSizeInBytes how big the buffer is in bytes
102    */
103   void ProcessCommand( char* buffer, unsigned int bufferSizeInBytes );
104
105   /**
106    * @brief Write a marker to the socket, if this client is filtering this marker.
107    * @param marker
108    */
109   bool TransmitMarker( const PerformanceMarker& marker, const char* const description );
110
111   /**
112    * @brief If the client is waiting inside a select statement, this will cause it
113    * to break out.
114    */
115   void ExitSelect();
116
117 private:
118
119   SocketInterface* mSocket;                             ///< socket interface
120   PerformanceMarker::MarkerFilter mMarkerBitmask;       ///< What markers are currently filtered
121   TriggerEventFactoryInterface& mTriggerEventFactory;   ///< Trigger event factory
122   ClientSendDataInterface& mSendDataInterface;          ///< used to send data to a client from the main event thread
123   SocketFactoryInterface& mSocketFactoryInterface;      ///< used to delete the socket
124   unsigned int mClientId;                               ///< unique client id
125   bool mConsoleClient;                                  ///< if connected via a console then all responses are in ASCII, not binary packed data.
126
127 };
128
129
130 } // namespace Internal
131
132 } // namespace Adaptor
133
134 } // namespace Dali
135
136 #endif