diff options
Diffstat (limited to 'src/components/transport_manager/src/transport_manager_impl.cc')
-rw-r--r-- | src/components/transport_manager/src/transport_manager_impl.cc | 119 |
1 files changed, 75 insertions, 44 deletions
diff --git a/src/components/transport_manager/src/transport_manager_impl.cc b/src/components/transport_manager/src/transport_manager_impl.cc index 0c49401c80..07dd35bc2f 100644 --- a/src/components/transport_manager/src/transport_manager_impl.cc +++ b/src/components/transport_manager/src/transport_manager_impl.cc @@ -33,28 +33,28 @@ #include "transport_manager/transport_manager_impl.h" #include <stdint.h> +#include <algorithm> #include <cstring> +#include <functional> +#include <iostream> +#include <limits> #include <queue> #include <set> -#include <algorithm> -#include <limits> -#include <functional> #include <sstream> -#include <iostream> -#include "utils/macro.h" #include "utils/logger.h" +#include "utils/macro.h" -#include "utils/timer_task_impl.h" -#include "transport_manager/common.h" -#include "transport_manager/transport_manager_listener.h" -#include "transport_manager/transport_manager_listener_empty.h" -#include "transport_manager/transport_adapter/transport_adapter.h" +#include "config_profile/profile.h" #if defined(CLOUD_APP_WEBSOCKET_TRANSPORT_SUPPORT) #include "transport_manager/cloud/cloud_websocket_transport_adapter.h" #endif +#include "transport_manager/common.h" +#include "transport_manager/transport_adapter/transport_adapter.h" #include "transport_manager/transport_adapter/transport_adapter_event.h" -#include "config_profile/profile.h" +#include "transport_manager/transport_manager_listener.h" +#include "transport_manager/transport_manager_listener_empty.h" +#include "utils/timer_task_impl.h" using ::transport_manager::transport_adapter::TransportAdapter; @@ -67,6 +67,7 @@ struct ConnectionFinder { return id_ == connection.id; } }; + } // namespace namespace transport_manager { @@ -100,7 +101,10 @@ TransportManagerImpl::TransportManagerImpl( , device_switch_timer_( "Device reconection timer", new timer::TimerTaskImpl<TransportManagerImpl>( - this, &TransportManagerImpl::ReconnectionTimeout)) { + this, &TransportManagerImpl::ReconnectionTimeout)) + , events_processing_is_active_(true) + , events_processing_lock_() + , events_processing_cond_var_() { LOG4CXX_TRACE(logger_, "TransportManager has created"); } @@ -379,9 +383,9 @@ int TransportManagerImpl::Stop() { int TransportManagerImpl::SendMessageToDevice( const ::protocol_handler::RawMessagePtr message) { LOG4CXX_TRACE(logger_, "enter. RawMessageSptr: " << message); - LOG4CXX_INFO(logger_, - "Send message to device called with arguments " - << message.get()); + LOG4CXX_INFO( + logger_, + "Send message to device called with arguments " << message.get()); if (false == this->is_initialized_) { LOG4CXX_ERROR(logger_, "TM is not initialized."); LOG4CXX_TRACE(logger_, @@ -586,21 +590,36 @@ int TransportManagerImpl::Init(resumption::LastState& last_state) { return E_SUCCESS; } -int TransportManagerImpl::Reinit() { +void TransportManagerImpl::Deinit() { LOG4CXX_AUTO_TRACE(logger_); DisconnectAllDevices(); TerminateAllAdapters(); device_to_adapter_map_.clear(); connection_id_counter_ = 0; +} + +int TransportManagerImpl::Reinit() { int ret = InitAllAdapters(); return ret; } -int TransportManagerImpl::Visibility(const bool& on_off) const { - LOG4CXX_TRACE(logger_, "enter. On_off: " << &on_off); - TransportAdapter::Error ret; +void TransportManagerImpl::StopEventsProcessing() { + LOG4CXX_AUTO_TRACE(logger_); + events_processing_is_active_ = false; +} - LOG4CXX_DEBUG(logger_, "Visibility change requested to " << on_off); +void TransportManagerImpl::StartEventsProcessing() { + LOG4CXX_AUTO_TRACE(logger_); + events_processing_is_active_ = true; + events_processing_cond_var_.Broadcast(); +} + +int TransportManagerImpl::PerformActionOnClients( + const TransportAction required_action) const { + LOG4CXX_TRACE(logger_, + "The following action requested: " + << static_cast<int>(required_action) + << " to be performed on connected clients"); if (!is_initialized_) { LOG4CXX_ERROR(logger_, "TM is not initialized"); LOG4CXX_TRACE(logger_, @@ -609,21 +628,19 @@ int TransportManagerImpl::Visibility(const bool& on_off) const { return E_TM_IS_NOT_INITIALIZED; } - for (std::vector<TransportAdapter*>::const_iterator it = - transport_adapters_.begin(); - it != transport_adapters_.end(); - ++it) { - if (on_off) { - ret = (*it)->StartClientListening(); - } else { - ret = (*it)->StopClientListening(); - } + TransportAdapter::Error ret = TransportAdapter::Error::UNKNOWN; + + for (auto adapter_ptr : transport_adapters_) { + ret = adapter_ptr->ChangeClientListening(required_action); + if (TransportAdapter::Error::NOT_SUPPORTED == ret) { LOG4CXX_DEBUG(logger_, - "Visibility change is not supported for adapter " - << *it << "[" << (*it)->GetDeviceType() << "]"); + "Requested action on client is not supported for adapter " + << adapter_ptr << "[" << adapter_ptr->GetDeviceType() + << "]"); } } + LOG4CXX_TRACE(logger_, "exit with E_SUCCESS"); return E_SUCCESS; } @@ -781,9 +798,9 @@ TransportManagerImpl::ConnectionInternal* TransportManagerImpl::GetActiveConnection( const DeviceUID& device, const ApplicationHandle& application) { LOG4CXX_AUTO_TRACE(logger_); - LOG4CXX_DEBUG(logger_, - "DeviceUID: " << device - << " ApplicationHandle: " << application); + LOG4CXX_DEBUG( + logger_, + "DeviceUID: " << device << " ApplicationHandle: " << application); for (std::vector<ConnectionInternal>::iterator it = connections_.begin(); it != connections_.end(); ++it) { @@ -932,9 +949,9 @@ bool TransportManagerImpl::UpdateDeviceMapping( item = device_to_adapter_map_.begin(); } - LOG4CXX_DEBUG(logger_, - "After cleanup. Device map size is " - << device_to_adapter_map_.size()); + LOG4CXX_DEBUG( + logger_, + "After cleanup. Device map size is " << device_to_adapter_map_.size()); for (DeviceList::const_iterator it = adapter_device_list.begin(); it != adapter_device_list.end(); @@ -944,10 +961,10 @@ bool TransportManagerImpl::UpdateDeviceMapping( device_to_adapter_map_.insert(std::make_pair(device_uid, ta)); if (!result.second) { LOG4CXX_WARN(logger_, - "Device UID " - << device_uid - << " is known already. Processing skipped." - "Connection type is: " << ta->GetConnectionType()); + "Device UID " << device_uid + << " is known already. Processing skipped." + "Connection type is: " + << ta->GetConnectionType()); continue; } DeviceHandle device_handle = @@ -959,9 +976,9 @@ bool TransportManagerImpl::UpdateDeviceMapping( RaiseEvent(&TransportManagerListener::OnDeviceFound, info); } - LOG4CXX_DEBUG(logger_, - "After update. Device map size is " - << device_to_adapter_map_.size()); + LOG4CXX_DEBUG( + logger_, + "After update. Device map size is " << device_to_adapter_map_.size()); return true; } @@ -987,6 +1004,13 @@ void TransportManagerImpl::OnDeviceListUpdated(TransportAdapter* ta) { void TransportManagerImpl::Handle(TransportAdapterEvent event) { LOG4CXX_TRACE(logger_, "enter"); + + if (!events_processing_is_active_) { + LOG4CXX_DEBUG(logger_, "Waiting for events handling unlock"); + sync_primitives::AutoLock auto_lock(events_processing_lock_); + events_processing_cond_var_.Wait(auto_lock); + } + switch (event.event_type) { case EventTypeEnum::ON_SEARCH_DONE: { RaiseEvent(&TransportManagerListener::OnScanDevicesFinished); @@ -1297,6 +1321,13 @@ void TransportManagerImpl::SetTelemetryObserver(TMTelemetryObserver* observer) { void TransportManagerImpl::Handle(::protocol_handler::RawMessagePtr msg) { LOG4CXX_TRACE(logger_, "enter"); + + if (!events_processing_is_active_) { + LOG4CXX_DEBUG(logger_, "Waiting for events handling unlock"); + sync_primitives::AutoLock auto_lock(events_processing_lock_); + events_processing_cond_var_.Wait(auto_lock); + } + sync_primitives::AutoReadLock lock(connections_lock_); ConnectionInternal* connection = GetConnection(msg->connection_key()); if (connection == NULL) { |