diff options
author | Elisey Zamakhov <EZamakhov@luxoft.com> | 2015-03-20 15:15:11 +0300 |
---|---|---|
committer | Elisey Zamakhov <EZamakhov@luxoft.com> | 2015-03-24 20:19:35 +0300 |
commit | f9202ede545b043f913685bcc139b1bc5af92bc9 (patch) | |
tree | cfca74f408057c65b3860fac8791542b6e3c6057 /src/components/transport_manager/src/transport_manager_impl.cc | |
parent | e1d6b9b88e412ea240e1cf9b5764bb5f67854bd9 (diff) | |
download | sdl_core-f9202ede545b043f913685bcc139b1bc5af92bc9.tar.gz |
Add TMIpl::connections_ synchronization
Diffstat (limited to 'src/components/transport_manager/src/transport_manager_impl.cc')
-rw-r--r-- | src/components/transport_manager/src/transport_manager_impl.cc | 95 |
1 files changed, 68 insertions, 27 deletions
diff --git a/src/components/transport_manager/src/transport_manager_impl.cc b/src/components/transport_manager/src/transport_manager_impl.cc index 6c3edd7b91..3cb7fb1b6e 100644 --- a/src/components/transport_manager/src/transport_manager_impl.cc +++ b/src/components/transport_manager/src/transport_manager_impl.cc @@ -155,6 +155,7 @@ int TransportManagerImpl::Disconnect(const ConnectionUID& cid) { return E_TM_IS_NOT_INITIALIZED; } + sync_primitives::AutoReadLock lock(connections_lock_); ConnectionInternal* connection = GetConnection(cid); if (NULL == connection) { LOG4CXX_ERROR(logger_, "TransportManagerImpl::Disconnect: Connection does not exist."); @@ -201,6 +202,7 @@ int TransportManagerImpl::DisconnectForce(const ConnectionUID& cid) { "exit with E_TM_IS_NOT_INITIALIZED. Condition: false == this->is_initialized_"); return E_TM_IS_NOT_INITIALIZED; } + sync_primitives::AutoReadLock lock(connections_lock_); const ConnectionInternal* connection = GetConnection(cid); if (NULL == connection) { LOG4CXX_ERROR( @@ -278,8 +280,9 @@ int TransportManagerImpl::SendMessageToDevice(const ::protocol_handler::RawMessa return E_TM_IS_NOT_INITIALIZED; } - const ConnectionInternal* connection = - GetConnection(message->connection_key()); + { + sync_primitives::AutoReadLock lock(connections_lock_); + const ConnectionInternal* connection = GetConnection(message->connection_key()); if (NULL == connection) { LOG4CXX_ERROR(logger_, "Connection with id " << message->connection_key() << " does not exist."); @@ -293,6 +296,7 @@ int TransportManagerImpl::SendMessageToDevice(const ::protocol_handler::RawMessa "exit with E_CONNECTION_IS_TO_SHUTDOWN. Condition: connection->shutDown"); return E_CONNECTION_IS_TO_SHUTDOWN; } + } #ifdef TIME_TESTER if (metric_observer_) { metric_observer_->StartRawMsg(message.get()); @@ -434,10 +438,10 @@ int TransportManagerImpl::Visibility(const bool& on_off) const { TransportAdapter::Error ret; LOG4CXX_DEBUG(logger_, "Visibility change requested to " << on_off); - if (false == this->is_initialized_) { + if (!is_initialized_) { LOG4CXX_ERROR(logger_, "TM is not initialized"); LOG4CXX_TRACE(logger_, - "exit with E_TM_IS_NOT_INITIALIZED. Condition: false == this->is_initialized_"); + "exit with E_TM_IS_NOT_INITIALIZED. Condition: false == is_initialized_"); return E_TM_IS_NOT_INITIALIZED; } @@ -517,13 +521,16 @@ void TransportManagerImpl::PostEvent(const TransportAdapterEvent& event) { } void TransportManagerImpl::AddConnection(const ConnectionInternal& c) { - LOG4CXX_TRACE(logger_, "enter ConnectionInternal: " << &c); + LOG4CXX_AUTO_TRACE(logger_); + LOG4CXX_DEBUG(logger_, "ConnectionInternal: " << &c); + sync_primitives::AutoWriteLock lock(connections_lock_); connections_.push_back(c); - LOG4CXX_TRACE(logger_, "exit"); } void TransportManagerImpl::RemoveConnection(uint32_t id) { - LOG4CXX_TRACE(logger_, "enter Id: " << id); + LOG4CXX_AUTO_TRACE(logger_); + LOG4CXX_DEBUG(logger_, "Id: " << id); + sync_primitives::AutoWriteLock lock(connections_lock_); for (std::vector<ConnectionInternal>::iterator it = connections_.begin(); it != connections_.end(); ++it) { if (it->id == id) { @@ -531,42 +538,41 @@ void TransportManagerImpl::RemoveConnection(uint32_t id) { break; } } - LOG4CXX_TRACE(logger_, "exit"); } TransportManagerImpl::ConnectionInternal* TransportManagerImpl::GetConnection( const ConnectionUID& id) { - LOG4CXX_TRACE(logger_, "enter. ConnectionUID: " << &id); + LOG4CXX_AUTO_TRACE(logger_); + LOG4CXX_DEBUG(logger_, "ConnectionUID: " << &id); for (std::vector<ConnectionInternal>::iterator it = connections_.begin(); it != connections_.end(); ++it) { if (it->id == id) { - LOG4CXX_TRACE(logger_, "exit with ConnectionInternal. It's address: " << &*it); + LOG4CXX_DEBUG(logger_, "ConnectionInternal. It's address: " << &*it); return &*it; } } - LOG4CXX_TRACE(logger_, "exit with NULL"); return NULL; } TransportManagerImpl::ConnectionInternal* TransportManagerImpl::GetConnection( const DeviceUID& device, const ApplicationHandle& application) { - LOG4CXX_TRACE(logger_, "enter DeviceUID: " << &device << "ApplicationHandle: " << + LOG4CXX_AUTO_TRACE(logger_); + LOG4CXX_DEBUG(logger_, "DeviceUID: " << &device << "ApplicationHandle: " << &application); for (std::vector<ConnectionInternal>::iterator it = connections_.begin(); it != connections_.end(); ++it) { if (it->device == device && it->application == application) { - LOG4CXX_TRACE(logger_, "exit with ConnectionInternal. It's address: " << &*it); + LOG4CXX_DEBUG(logger_, "ConnectionInternal. It's address: " << &*it); return &*it; } } - LOG4CXX_TRACE(logger_, "exit with NULL"); return NULL; } void TransportManagerImpl::OnDeviceListUpdated(TransportAdapter* ta) { LOG4CXX_TRACE(logger_, "enter. TransportAdapter: " << ta); const DeviceList device_list = ta->GetDeviceList(); - LOG4CXX_INFO(logger_, "DEVICE_LIST_UPDATED " << device_list.size()); + LOG4CXX_DEBUG(logger_, "DEVICE_LIST_UPDATED " << device_list.size()); for (DeviceList::const_iterator it = device_list.begin(); it != device_list.end(); ++it) { device_to_adapter_map_.insert(std::make_pair(*it, ta)); @@ -586,7 +592,6 @@ void TransportManagerImpl::OnDeviceListUpdated(TransportAdapter* ta) { void TransportManagerImpl::Handle(TransportAdapterEvent event) { LOG4CXX_TRACE(logger_, "enter"); - ConnectionInternal* connection = GetConnection(event.device_uid, event.application_id); switch (event.event_type) { case TransportAdapterListenerImpl::EventTypeEnum::ON_SEARCH_DONE: { RaiseEvent(&TransportManagerListener::OnScanDevicesFinished); @@ -633,15 +638,21 @@ void TransportManagerImpl::Handle(TransportAdapterEvent event) { break; } case TransportAdapterListenerImpl::EventTypeEnum::ON_DISCONNECT_DONE: { + connections_lock_.AcquireForReading(); + ConnectionInternal* connection = + GetConnection(event.device_uid, event.application_id); if (NULL == connection) { LOG4CXX_ERROR(logger_, "Connection not found"); LOG4CXX_DEBUG(logger_, "event_type = ON_DISCONNECT_DONE && NULL == connection"); + connections_lock_.Release(); break; } - RaiseEvent(&TransportManagerListener::OnConnectionClosed, - connection->id); - RemoveConnection(connection->id); + const ConnectionUID id = connection->id; + connections_lock_.Release(); + + RaiseEvent(&TransportManagerListener::OnConnectionClosed, id); + RemoveConnection(id); LOG4CXX_DEBUG(logger_, "event_type = ON_DISCONNECT_DONE"); break; } @@ -658,11 +669,15 @@ void TransportManagerImpl::Handle(TransportAdapterEvent event) { metric_observer_->StopRawMsg(event.event_data.get()); } #endif // TIME_TESTER + sync_primitives::AutoReadLock lock(connections_lock_); + ConnectionInternal* connection = + GetConnection(event.device_uid, event.application_id); if (connection == NULL) { LOG4CXX_ERROR(logger_, "Connection ('" << event.device_uid << ", " << event.application_id << ") not found"); - LOG4CXX_DEBUG(logger_, "event_type = ON_SEND_DONE. Condition: NULL == connection"); + LOG4CXX_DEBUG(logger_, + "event_type = ON_SEND_DONE. Condition: NULL == connection"); break; } RaiseEvent(&TransportManagerListener::OnTMMessageSend, event.event_data); @@ -680,13 +695,19 @@ void TransportManagerImpl::Handle(TransportAdapterEvent event) { metric_observer_->StopRawMsg(event.event_data.get()); } #endif // TIME_TESTER + { + sync_primitives::AutoReadLock lock(connections_lock_); + ConnectionInternal* connection = + GetConnection(event.device_uid, event.application_id); if (connection == NULL) { LOG4CXX_ERROR(logger_, "Connection ('" << event.device_uid << ", " << event.application_id << ") not found"); - LOG4CXX_DEBUG(logger_, "event_type = ON_SEND_FAIL. Condition: NULL == connection"); + LOG4CXX_DEBUG(logger_, + "event_type = ON_SEND_FAIL. Condition: NULL == connection"); break; } + } // TODO(YK): start timer here to wait before notify caller // and remove unsent messages @@ -702,14 +723,20 @@ void TransportManagerImpl::Handle(TransportAdapterEvent event) { break; } case TransportAdapterListenerImpl::EventTypeEnum::ON_RECEIVED_DONE: { + { + sync_primitives::AutoReadLock lock(connections_lock_); + ConnectionInternal* connection = + GetConnection(event.device_uid, event.application_id); if (connection == NULL) { LOG4CXX_ERROR(logger_, "Connection ('" << event.device_uid << ", " << event.application_id << ") not found"); - LOG4CXX_DEBUG(logger_, "event_type = ON_RECEIVED_DONE. Condition: NULL == connection"); + LOG4CXX_DEBUG(logger_, + "event_type = ON_RECEIVED_DONE. Condition: NULL == connection"); break; } event.event_data->set_connection_key(connection->id); + } #ifdef TIME_TESTER if (metric_observer_) { metric_observer_->StopRawMsg(event.event_data.get()); @@ -721,15 +748,21 @@ void TransportManagerImpl::Handle(TransportAdapterEvent event) { } case TransportAdapterListenerImpl::EventTypeEnum::ON_RECEIVED_FAIL: { LOG4CXX_DEBUG(logger_, "Event ON_RECEIVED_FAIL"); + connections_lock_.AcquireForReading(); + ConnectionInternal* connection = + GetConnection(event.device_uid, event.application_id); if (connection == NULL) { LOG4CXX_ERROR(logger_, "Connection ('" << event.device_uid << ", " << event.application_id << ") not found"); + connections_lock_.Release(); break; } + ConnectionUID connection_id = connection->id; + connections_lock_.Release(); RaiseEvent(&TransportManagerListener::OnTMMessageReceiveFailed, - connection->id, *static_cast<DataReceiveError*>(event.event_error.get())); + connection_id, *static_cast<DataReceiveError*>(event.event_error.get())); LOG4CXX_DEBUG(logger_, "event_type = ON_RECEIVED_FAIL"); break; } @@ -738,12 +771,18 @@ void TransportManagerImpl::Handle(TransportAdapterEvent event) { break; } case TransportAdapterListenerImpl::EventTypeEnum::ON_UNEXPECTED_DISCONNECT: { + connections_lock_.AcquireForReading(); + ConnectionInternal* connection = + GetConnection(event.device_uid, event.application_id); if (connection) { + const ConnectionUID id = connection->id; + connections_lock_.Release(); RaiseEvent(&TransportManagerListener::OnUnexpectedDisconnect, - connection->id, + id, *static_cast<CommunicationError*>(event.event_error.get())); - RemoveConnection(connection->id); + RemoveConnection(id); } else { + connections_lock_.Release(); LOG4CXX_ERROR(logger_, "Connection ('" << event.device_uid << ", " << event.application_id << ") not found"); @@ -763,6 +802,7 @@ void TransportManagerImpl::SetTimeMetricObserver(TMMetricObserver* observer) { void TransportManagerImpl::Handle(::protocol_handler::RawMessagePtr msg) { LOG4CXX_TRACE(logger_, "enter"); + sync_primitives::AutoReadLock lock(connections_lock_); ConnectionInternal* connection = GetConnection(msg->connection_key()); if (connection == NULL) { LOG4CXX_WARN(logger_, "Connection " << msg->connection_key() << " not found"); @@ -788,7 +828,7 @@ void TransportManagerImpl::Handle(::protocol_handler::RawMessagePtr msg) { if (TransportAdapter::OK == transport_adapter->SendData( connection->device, connection->application, msg)) { - LOG4CXX_INFO(logger_, "Data sent to adapter"); + LOG4CXX_TRACE(logger_, "Data sent to adapter"); } else { LOG4CXX_ERROR(logger_, "Data sent error"); RaiseEvent(&TransportManagerListener::OnTMMessageSendFailed, @@ -805,7 +845,8 @@ TransportManagerImpl::ConnectionInternal::ConnectionInternal( const DeviceHandle& device_handle) : transport_manager(transport_manager), transport_adapter(transport_adapter), - timer(new TimerInternal("TM DiscRoutine", this, &ConnectionInternal::DisconnectFailedRoutine)), + timer(new TimerInternal("TM DiscRoutine", this, + &ConnectionInternal::DisconnectFailedRoutine)), shutDown(false), device_handle_(device_handle), messages_count(0) { |