Dali 3D User Interface Engine
network-performance-client.cpp
Go to the documentation of this file.
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
20 
21 // EXTERNAL INCLUDES
22 #include <stdio.h>
23 #include <string>
24 
25 // INTERNAL INCLUDES
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 
50 {
51 public:
52 
57  enum CommandId
58  {
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;
115  const unsigned int mClientId;
116 };
117 
118 } // unnamed namespace
119 
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 
139 {
140  if( mSocket->SocketIsOpen() )
141  {
142  mSocket->CloseSocket();
143  }
145 }
146 
148 {
149  return mClientId;
150 }
151 
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  int size = snprintf( buffer, sizeof(buffer),"%.6f (seconds), %s\n",
173  (float)( marker.GetTimeStamp().microseconds * MICROSECONDS_TO_SECOND ),
174  description );
175 
176  return mSocket->Write( buffer, size );
177 
178  }
179 
180  // todo serialize the data
181  return false;
182 }
183 
185 {
186  mSocket->ExitSelect();
187 }
188 
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;
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  {
217  {
219  break;
220  }
221 
223  {
224  mMarkerBitmask = static_cast< PerformanceMarker::MarkerFilter >( param );
225  response = "enable time marker ";
226  break;
227  }
228 
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 
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 
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
Dali Docs Home
Read more about Dali