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