2 * Copyright (c) 2015 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 "network-performance-client.h"
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>
42 const char UNKNOWN_CMD[]= "Command or parameter invalid, type help for list of commands\n";
46 * helper class to store data along with the automation callback.
48 class AutomationCallback: public CallbackBase
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.
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 ),
72 void AssignSetPropertyCommand( std::string setPropertyCommand )
74 mCommandId = SET_PROPERTY;
75 mPropertyCommand = setPropertyCommand;
77 void AssignDumpSceneCommand()
79 mCommandId = DUMP_SCENE;
88 Automation::SetProperty( mPropertyCommand );
93 Automation::DumpScene( mClientId, &mSendDataInterface);
98 DALI_ASSERT_DEBUG( 0 && "Unknown command");
103 static void Dispatcher( CallbackBase& base )
105 AutomationCallback& automationCallback( static_cast< AutomationCallback& >( base) );
106 automationCallback.RunCallback();
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
117 } // unnamed namespace
119 NetworkPerformanceClient::NetworkPerformanceClient( pthread_t* thread,
120 SocketInterface *socket,
121 unsigned int clientId,
122 TriggerEventFactoryInterface& triggerEventFactory,
123 ClientSendDataInterface& sendDataInterface,
124 SocketFactoryInterface& socketFactory )
127 mMarkerBitmask( PerformanceMarker::FILTERING_DISABLED ),
128 mTriggerEventFactory( triggerEventFactory ),
129 mSendDataInterface( sendDataInterface ),
130 mSocketFactoryInterface( socketFactory ),
131 mClientId( clientId ),
132 mConsoleClient(false)
137 NetworkPerformanceClient::~NetworkPerformanceClient()
139 if( mSocket->SocketIsOpen() )
141 mSocket->CloseSocket();
143 mSocketFactoryInterface.DestroySocket( mSocket );
146 unsigned int NetworkPerformanceClient::GetId() const
151 SocketInterface& NetworkPerformanceClient::GetSocket()
156 bool NetworkPerformanceClient::WriteSocket( const void* buffer, unsigned int bufferSizeInBytes )
158 return mSocket->Write( buffer, bufferSizeInBytes );
161 bool NetworkPerformanceClient::TransmitMarker( const PerformanceMarker& marker, const char* const description )
163 if( ! marker.IsFilterEnabled( mMarkerBitmask ) )
169 // write out the time stamp
171 int size = snprintf( buffer, sizeof(buffer),"%d.%06d (seconds), %s\n",
172 marker.GetTimeStamp().seconds,
173 marker.GetTimeStamp().microseconds,
176 return mSocket->Write( buffer, size );
180 // todo serialize the data
184 void NetworkPerformanceClient::ExitSelect()
186 mSocket->ExitSelect();
189 pthread_t* NetworkPerformanceClient::GetThread()
194 void NetworkPerformanceClient::ProcessCommand( char* buffer, unsigned int bufferSizeInBytes )
196 // if connected via console, then strip off the carriage return, and switch to console mode
197 if( buffer[ bufferSizeInBytes - 1] == '\n')
199 buffer[ bufferSizeInBytes - 1] = 0;
200 mConsoleClient = true;
202 unsigned int param(0);
203 std::string stringParam;
204 PerformanceProtocol::CommandId commandId( PerformanceProtocol::UNKNOWN_COMMAND );
206 bool ok = PerformanceProtocol::GetCommandId( buffer, bufferSizeInBytes, commandId, param, stringParam );
209 WriteSocket( UNKNOWN_CMD, sizeof(UNKNOWN_CMD) );
212 std::string response;
216 case PerformanceProtocol::HELP_MESSAGE:
218 response = PerformanceProtocol::GetHelpMessage();
222 case PerformanceProtocol::ENABLE_TIME_MARKER_BIT_MASK:
224 mMarkerBitmask = static_cast< PerformanceMarker::MarkerFilter >( param );
225 response = "enable time marker ";
229 case PerformanceProtocol::DUMP_SCENE_GRAPH:
231 // this needs to be run on the main thread, use the trigger event....
232 AutomationCallback* callback = new AutomationCallback( mClientId, mSendDataInterface );
233 callback->AssignDumpSceneCommand();
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 );
238 // asynchronous call, the call back will be run sometime later on the main thread
239 interface->Trigger();
243 case PerformanceProtocol::SET_PROPERTIES:
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 );
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 );
252 // asynchronous call, the call back will be run sometime later on the main thread
253 interface->Trigger();
257 case PerformanceProtocol::LIST_METRICS_AVAILABLE:
258 case PerformanceProtocol::ENABLE_METRIC:
259 case PerformanceProtocol::DISABLE_METRIC:
261 response="Metrics currently not supported";
266 response = UNKNOWN_CMD;
270 if( ! response.empty() )
272 // add a carriage return for console clients
277 WriteSocket( response.c_str(), response.length() );
283 } // namespace Internal
285 } // namespace Adaptor