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( SocketInterface *socket,
120 unsigned int clientId,
121 TriggerEventFactoryInterface& triggerEventFactory,
122 ClientSendDataInterface& sendDataInterface,
123 SocketFactoryInterface& socketFactory )
125 mMarkerBitmask( PerformanceMarker::FILTERING_DISABLED ),
126 mTriggerEventFactory( triggerEventFactory ),
127 mSendDataInterface( sendDataInterface ),
128 mSocketFactoryInterface( socketFactory ),
129 mClientId( clientId ),
130 mConsoleClient(false)
135 NetworkPerformanceClient::~NetworkPerformanceClient()
137 if( mSocket->SocketIsOpen() )
139 mSocket->CloseSocket();
141 mSocketFactoryInterface.DestroySocket( mSocket );
144 unsigned int NetworkPerformanceClient::GetId() const
149 SocketInterface& NetworkPerformanceClient::GetSocket()
154 bool NetworkPerformanceClient::WriteSocket( const void* buffer, unsigned int bufferSizeInBytes )
156 return mSocket->Write( buffer, bufferSizeInBytes );
159 bool NetworkPerformanceClient::TransmitMarker( const PerformanceMarker& marker, const char* const description )
161 if( ! marker.IsFilterEnabled( mMarkerBitmask ) )
167 // write out the time stamp
169 int size = snprintf( buffer, sizeof(buffer),"%d.%06d (seconds), %s\n",
170 marker.GetTimeStamp().seconds,
171 marker.GetTimeStamp().microseconds,
174 return mSocket->Write( buffer, size );
178 // todo serialize the data
182 void NetworkPerformanceClient::ExitSelect()
184 mSocket->ExitSelect();
188 void NetworkPerformanceClient::ProcessCommand( char* buffer, unsigned int bufferSizeInBytes )
190 // if connected via console, then strip off the carriage return, and switch to console mode
191 if( buffer[ bufferSizeInBytes - 1] == '\n')
193 buffer[ bufferSizeInBytes - 1] = 0;
194 mConsoleClient = true;
196 unsigned int param(0);
197 std::string stringParam;
198 PerformanceProtocol::CommandId commandId( PerformanceProtocol::UNKNOWN_COMMAND );
200 bool ok = PerformanceProtocol::GetCommandId( buffer, bufferSizeInBytes, commandId, param, stringParam );
203 WriteSocket( UNKNOWN_CMD, sizeof(UNKNOWN_CMD) );
206 std::string response;
210 case PerformanceProtocol::HELP_MESSAGE:
212 response = PerformanceProtocol::GetHelpMessage();
216 case PerformanceProtocol::ENABLE_TIME_MARKER_BIT_MASK:
218 mMarkerBitmask = static_cast< PerformanceMarker::MarkerFilter >( param );
219 response = "enable time marker ";
223 case PerformanceProtocol::DUMP_SCENE_GRAPH:
225 // this needs to be run on the main thread, use the trigger event....
226 AutomationCallback* callback = new AutomationCallback( mClientId, mSendDataInterface );
227 callback->AssignDumpSceneCommand();
229 // create a trigger event that automatically deletes itself after the callback has run in the main thread
230 TriggerEventInterface *interface = mTriggerEventFactory.CreateTriggerEvent( callback, TriggerEventInterface::DELETE_AFTER_TRIGGER );
232 // asynchronous call, the call back will be run sometime later on the main thread
233 interface->Trigger();
237 case PerformanceProtocol::SET_PROPERTIES:
239 // this needs to be run on the main thread, use the trigger event....
240 AutomationCallback* callback = new AutomationCallback( mClientId, mSendDataInterface );
241 callback->AssignSetPropertyCommand( stringParam );
243 // create a trigger event that automatically deletes itself after the callback has run in the main thread
244 TriggerEventInterface *interface = mTriggerEventFactory.CreateTriggerEvent( callback, TriggerEventInterface::DELETE_AFTER_TRIGGER );
246 // asynchronous call, the call back will be run sometime later on the main thread
247 interface->Trigger();
251 case PerformanceProtocol::LIST_METRICS_AVAILABLE:
252 case PerformanceProtocol::ENABLE_METRIC:
253 case PerformanceProtocol::DISABLE_METRIC:
255 response="Metrics currently not supported";
260 response = UNKNOWN_CMD;
264 if( ! response.empty() )
266 // add a carriage return for console clients
271 WriteSocket( response.c_str(), response.length() );
277 } // namespace Internal
279 } // namespace Adaptor