Boost removal from performance server
[platform/core/uifw/dali-adaptor.git] / adaptors / base / performance-logging / networking / network-performance-client.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "network-performance-client.h"
20
21 // EXTERNAL INCLUDES
22 #include <stdio.h>
23 #include <string>
24
25 // INTERNAL INCLUDES
26 #include <base/interfaces/socket-interface.h>
27 #include <base/performance-logging/networking/network-performance-protocol.h>
28 #include <base/performance-logging/networking/event/automation.h>
29
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39
40 namespace
41 {
42 const char UNKNOWN_CMD[]= "Command or parameter invalid, type help for list of commands\n";
43
44
45 /**
46  * helper class to store data along with the automation callback.
47  */
48 class AutomationCallback: public CallbackBase
49 {
50 public:
51
52   /**
53    * instead of using templates, or having different callback classes for each callback
54    * we use a command id that decides which static function to call on the Automation class.
55    */
56   enum CommandId
57   {
58     UNKNOWN_COMMAND,
59     SET_PROPERTY,
60     DUMP_SCENE
61   };
62
63   AutomationCallback(  unsigned int clientId, ClientSendDataInterface& sendDataInterface )
64   :CallbackBase( reinterpret_cast< void* >( this ),
65                  NULL, // we get the dispatcher to call function directly
66                  reinterpret_cast< CallbackBase::Dispatcher>( &AutomationCallback::Dispatcher) ),
67   mSendDataInterface( sendDataInterface ),
68   mCommandId( UNKNOWN_COMMAND ),
69   mClientId( clientId )
70   {}
71
72   void AssignSetPropertyCommand( std::string setPropertyCommand )
73   {
74     mCommandId = SET_PROPERTY;
75     mPropertyCommand = setPropertyCommand;
76   }
77   void AssignDumpSceneCommand()
78   {
79      mCommandId = DUMP_SCENE;
80   }
81
82   void RunCallback()
83   {
84     switch( mCommandId )
85     {
86       case SET_PROPERTY:
87       {
88         Automation::SetProperty( mPropertyCommand );
89         break;
90       }
91       case DUMP_SCENE:
92       {
93         Automation::DumpScene( mClientId, &mSendDataInterface);
94         break;
95       }
96       default:
97       {
98         DALI_ASSERT_DEBUG( 0 && "Unknown command");
99         break;
100       }
101     }
102   }
103   static void Dispatcher( CallbackBase& base )
104   {
105     AutomationCallback& automationCallback( static_cast< AutomationCallback& >( base) );
106     automationCallback.RunCallback();
107   }
108
109 private:
110
111   std::string mPropertyCommand;                   ///< property command
112   ClientSendDataInterface& mSendDataInterface;    ///< Abstract client send data interface
113   CommandId mCommandId;                           ///< command id
114   const unsigned int mClientId;                   ///< client id
115 };
116
117 } // unnamed namespace
118
119 NetworkPerformanceClient::NetworkPerformanceClient(  pthread_t* thread,
120                                                      SocketInterface *socket,
121                                                      unsigned int clientId,
122                                                      TriggerEventFactoryInterface& triggerEventFactory,
123                                                      ClientSendDataInterface& sendDataInterface,
124                                                      SocketFactoryInterface& socketFactory )
125 : mThread( thread ),
126   mSocket( socket ),
127   mMarkerBitmask( PerformanceMarker::FILTERING_DISABLED ),
128   mTriggerEventFactory( triggerEventFactory ),
129   mSendDataInterface( sendDataInterface ),
130   mSocketFactoryInterface( socketFactory ),
131   mClientId( clientId ),
132   mConsoleClient(false)
133 {
134
135 }
136
137 NetworkPerformanceClient::~NetworkPerformanceClient()
138 {
139   if( mSocket->SocketIsOpen() )
140   {
141     mSocket->CloseSocket();
142   }
143   mSocketFactoryInterface.DestroySocket( mSocket );
144 }
145
146 unsigned int NetworkPerformanceClient::GetId() const
147 {
148   return mClientId;
149 }
150
151 SocketInterface& NetworkPerformanceClient::GetSocket()
152 {
153   return *mSocket;
154 }
155
156 bool NetworkPerformanceClient::WriteSocket( const void* buffer, unsigned int bufferSizeInBytes )
157 {
158   return mSocket->Write( buffer, bufferSizeInBytes );
159 }
160
161 bool NetworkPerformanceClient::TransmitMarker( const PerformanceMarker& marker, const char* const description )
162 {
163   if( ! marker.IsFilterEnabled( mMarkerBitmask ) )
164   {
165     return true;
166   }
167   if( mConsoleClient )
168   {
169     // write out the time stamp
170     char buffer[64];
171     int size = snprintf( buffer, sizeof(buffer),"%d.%06d (seconds), %s\n",
172                                     marker.GetTimeStamp().seconds,
173                                     marker.GetTimeStamp().microseconds,
174                                     description );
175
176    return mSocket->Write( buffer, size );
177
178   }
179
180   // todo serialize the data
181   return false;
182 }
183
184 void NetworkPerformanceClient::ExitSelect()
185 {
186   mSocket->ExitSelect();
187 }
188
189 pthread_t* NetworkPerformanceClient::GetThread()
190 {
191   return mThread;
192 }
193
194 void NetworkPerformanceClient::ProcessCommand( char* buffer, unsigned int bufferSizeInBytes )
195 {
196   // if connected via console, then strip off the carriage return, and switch to console mode
197   if( buffer[ bufferSizeInBytes - 1] == '\n')
198   {
199     buffer[ bufferSizeInBytes - 1] = 0;
200     mConsoleClient = true;
201   }
202   unsigned int param(0);
203   std::string stringParam;
204   PerformanceProtocol::CommandId commandId( PerformanceProtocol::UNKNOWN_COMMAND );
205
206   bool ok =  PerformanceProtocol::GetCommandId( buffer, bufferSizeInBytes, commandId, param, stringParam );
207   if( !ok )
208   {
209     WriteSocket( UNKNOWN_CMD, sizeof(UNKNOWN_CMD) );
210     return;
211   }
212   std::string response;
213
214   switch( commandId )
215   {
216     case PerformanceProtocol::HELP_MESSAGE:
217     {
218       response = PerformanceProtocol::GetHelpMessage();
219       break;
220     }
221
222     case PerformanceProtocol::ENABLE_TIME_MARKER_BIT_MASK:
223     {
224       mMarkerBitmask  = static_cast<  PerformanceMarker::MarkerFilter >( param );
225       response = "enable time marker ";
226       break;
227     }
228
229     case PerformanceProtocol::DUMP_SCENE_GRAPH:
230     {
231       // this needs to be run on the main thread, use the trigger event....
232       AutomationCallback* callback = new AutomationCallback( mClientId, mSendDataInterface );
233       callback->AssignDumpSceneCommand();
234
235       // create a trigger event that automatically deletes itself after the callback has run in the main thread
236       TriggerEventInterface *interface = mTriggerEventFactory.CreateTriggerEvent( callback, TriggerEventInterface::DELETE_AFTER_TRIGGER );
237
238       // asynchronous call, the call back will be run sometime later on the main thread
239       interface->Trigger();
240       break;
241     }
242
243     case PerformanceProtocol::SET_PROPERTIES:
244     {
245       // this needs to be run on the main thread, use the trigger event....
246       AutomationCallback* callback = new AutomationCallback( mClientId, mSendDataInterface );
247       callback->AssignSetPropertyCommand( stringParam );
248
249       // create a trigger event that automatically deletes itself after the callback has run in the main thread
250       TriggerEventInterface *interface = mTriggerEventFactory.CreateTriggerEvent( callback, TriggerEventInterface::DELETE_AFTER_TRIGGER );
251
252       // asynchronous call, the call back will be run sometime later on the main thread
253       interface->Trigger();
254       break;
255     }
256
257     case PerformanceProtocol::LIST_METRICS_AVAILABLE:
258     case PerformanceProtocol::ENABLE_METRIC:
259     case PerformanceProtocol::DISABLE_METRIC:
260     {
261       response="Metrics currently not supported";
262       break;
263     }
264     default:
265     {
266       response = UNKNOWN_CMD;
267       break;
268     }
269   }
270   if( ! response.empty() )
271   {
272     // add a carriage return for console clients
273     if( mConsoleClient )
274     {
275       response+="\n";
276     }
277     WriteSocket( response.c_str(), response.length()  );
278   }
279 }
280
281
282
283 } // namespace Internal
284
285 } // namespace Adaptor
286
287 } // namespace Dali