2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/network/common/network-performance-client.h>
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>
42 const float MICROSECONDS_TO_SECOND = 1e-6;
43 const char UNKNOWN_CMD[]= "Command or parameter invalid, type help for list of commands\n";
47 * helper class to store data along with the automation callback.
49 class AutomationCallback: public CallbackBase
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.
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 ),
73 void AssignSetPropertyCommand( std::string setPropertyCommand )
75 mCommandId = SET_PROPERTY;
76 mPropertyCommand = setPropertyCommand;
78 void AssignDumpSceneCommand()
80 mCommandId = DUMP_SCENE;
89 Automation::SetProperty( mPropertyCommand );
94 Automation::DumpScene( mClientId, &mSendDataInterface);
99 DALI_ASSERT_DEBUG( 0 && "Unknown command");
104 static void Dispatcher( CallbackBase& base )
106 AutomationCallback& automationCallback( static_cast< AutomationCallback& >( base) );
107 automationCallback.RunCallback();
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
118 } // unnamed namespace
120 NetworkPerformanceClient::NetworkPerformanceClient( pthread_t* thread,
121 SocketInterface *socket,
122 unsigned int clientId,
123 TriggerEventFactoryInterface& triggerEventFactory,
124 ClientSendDataInterface& sendDataInterface,
125 SocketFactoryInterface& socketFactory )
128 mMarkerBitmask( PerformanceMarker::FILTERING_DISABLED ),
129 mTriggerEventFactory( triggerEventFactory ),
130 mSendDataInterface( sendDataInterface ),
131 mSocketFactoryInterface( socketFactory ),
132 mClientId( clientId ),
133 mConsoleClient(false)
138 NetworkPerformanceClient::~NetworkPerformanceClient()
140 if( mSocket->SocketIsOpen() )
142 mSocket->CloseSocket();
144 mSocketFactoryInterface.DestroySocket( mSocket );
147 unsigned int NetworkPerformanceClient::GetId() const
152 SocketInterface& NetworkPerformanceClient::GetSocket()
157 bool NetworkPerformanceClient::WriteSocket( const void* buffer, unsigned int bufferSizeInBytes )
159 return mSocket->Write( buffer, bufferSizeInBytes );
162 bool NetworkPerformanceClient::TransmitMarker( const PerformanceMarker& marker, const char* const description )
164 if( ! marker.IsFilterEnabled( mMarkerBitmask ) )
170 // write out the time stamp
172 double usec = marker.GetTimeStamp().microseconds;
173 int size = snprintf( buffer, sizeof(buffer),"%.6f (seconds), %s\n",
174 usec * MICROSECONDS_TO_SECOND,
177 return mSocket->Write( buffer, size );
182 // todo serialize the data
186 void NetworkPerformanceClient::ExitSelect()
188 mSocket->ExitSelect();
191 pthread_t* NetworkPerformanceClient::GetThread()
196 void NetworkPerformanceClient::ProcessCommand( char* buffer, unsigned int bufferSizeInBytes )
198 // if connected via console, then strip off the carriage return, and switch to console mode
199 if( buffer[ bufferSizeInBytes - 1] == '\n')
201 buffer[ bufferSizeInBytes - 1] = 0;
202 mConsoleClient = true;
204 unsigned int param(0);
205 std::string stringParam;
206 PerformanceProtocol::CommandId commandId( PerformanceProtocol::UNKNOWN_COMMAND );
208 bool ok = PerformanceProtocol::GetCommandId( buffer, bufferSizeInBytes, commandId, param, stringParam );
211 WriteSocket( UNKNOWN_CMD, sizeof(UNKNOWN_CMD) );
214 std::string response;
218 case PerformanceProtocol::HELP_MESSAGE:
220 response = PerformanceProtocol::GetHelpMessage();
224 case PerformanceProtocol::ENABLE_TIME_MARKER_BIT_MASK:
226 mMarkerBitmask = static_cast< PerformanceMarker::MarkerFilter >( param );
227 response = "enable time marker ";
231 case PerformanceProtocol::DUMP_SCENE_GRAPH:
233 // this needs to be run on the main thread, use the trigger event....
234 AutomationCallback* callback = new AutomationCallback( mClientId, mSendDataInterface );
235 callback->AssignDumpSceneCommand();
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 );
240 // asynchronous call, the call back will be run sometime later on the main thread
241 interface->Trigger();
245 case PerformanceProtocol::SET_PROPERTIES:
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 );
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 );
254 // asynchronous call, the call back will be run sometime later on the main thread
255 interface->Trigger();
259 case PerformanceProtocol::LIST_METRICS_AVAILABLE:
260 case PerformanceProtocol::ENABLE_METRIC:
261 case PerformanceProtocol::DISABLE_METRIC:
263 response="Metrics currently not supported";
268 response = UNKNOWN_CMD;
272 if( ! response.empty() )
274 // add a carriage return for console clients
279 WriteSocket( response.c_str(), response.length() );
285 } // namespace Internal
287 } // namespace Adaptor