summaryrefslogtreecommitdiff
path: root/src/components/transport_manager/src/tcp
diff options
context:
space:
mode:
authorJacob Keeler <jacob.keeler@livioradio.com>2016-09-20 11:44:51 -0400
committerJacob Keeler <jacob.keeler@livioradio.com>2016-09-20 14:37:25 -0400
commit039581419749dbdd32590804e8b14a77fcaf9636 (patch)
treeced927fc4bef4686f7373a5f45dd692bd9fc9de0 /src/components/transport_manager/src/tcp
parent1b96b7c02d2e338b846a7314c6fa0bdaa31f7c5e (diff)
parentaff7f09a4ec07becfb64dc78992580061d3342ff (diff)
downloadsdl_core-039581419749dbdd32590804e8b14a77fcaf9636.tar.gz
Merge branch 'master'
Diffstat (limited to 'src/components/transport_manager/src/tcp')
-rw-r--r--src/components/transport_manager/src/tcp/dnssd_service_browser.cc378
-rw-r--r--src/components/transport_manager/src/tcp/tcp_client_listener.cc60
-rw-r--r--src/components/transport_manager/src/tcp/tcp_connection_factory.cc20
-rw-r--r--src/components/transport_manager/src/tcp/tcp_device.cc23
-rw-r--r--src/components/transport_manager/src/tcp/tcp_socket_connection.cc33
-rw-r--r--src/components/transport_manager/src/tcp/tcp_transport_adapter.cc49
6 files changed, 86 insertions, 477 deletions
diff --git a/src/components/transport_manager/src/tcp/dnssd_service_browser.cc b/src/components/transport_manager/src/tcp/dnssd_service_browser.cc
deleted file mode 100644
index 3571ac8156..0000000000
--- a/src/components/transport_manager/src/tcp/dnssd_service_browser.cc
+++ /dev/null
@@ -1,378 +0,0 @@
-/*
- *
- * Copyright (c) 2013, Ford Motor Company
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following
- * disclaimer in the documentation and/or other materials provided with the
- * distribution.
- *
- * Neither the name of the Ford Motor Company nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <algorithm>
-#include <map>
-#include "utils/logger.h"
-
-#include "transport_manager/transport_adapter/transport_adapter_impl.h"
-#include "transport_manager/tcp/tcp_device.h"
-#include "transport_manager/tcp/dnssd_service_browser.h"
-
-
-namespace transport_manager {
-namespace transport_adapter {
-
-CREATE_LOGGERPTR_GLOBAL(logger_, "TransportManager")
-
-
-bool operator==(const DnssdServiceRecord& a, const DnssdServiceRecord& b) {
- return a.name == b.name && a.type == b.type && a.interface == b.interface
- && a.protocol == b.protocol && a.domain_name == b.domain_name;
-}
-
-void DnssdServiceBrowser::Terminate() {
- LOG4CXX_AUTO_TRACE(logger_);
- if (0 != avahi_threaded_poll_) {
- avahi_threaded_poll_stop(avahi_threaded_poll_);
- }
- if (0 != avahi_service_browser_) {
- avahi_service_browser_free(avahi_service_browser_);
- avahi_service_browser_ = NULL;
- }
- if (0 != avahi_client_) {
- avahi_client_free(avahi_client_);
- avahi_client_ = NULL;
- }
- if (0 != avahi_threaded_poll_) {
- avahi_threaded_poll_free(avahi_threaded_poll_);
- avahi_threaded_poll_ = NULL;
- }
-}
-
-bool DnssdServiceBrowser::IsInitialised() const {
- return initialised_;
-}
-
-DnssdServiceBrowser::DnssdServiceBrowser(TransportAdapterController* controller)
- : controller_(controller),
- avahi_service_browser_(0),
- avahi_threaded_poll_(0),
- avahi_client_(0),
- service_records_(),
- mutex_(),
- initialised_(false) {
-}
-
-DnssdServiceBrowser::~DnssdServiceBrowser() {
-}
-
-void DnssdServiceBrowser::OnClientConnected() {
- initialised_ = true;
- LOG4CXX_INFO(logger_, "AvahiClient ready");
-}
-
-void DnssdServiceBrowser::OnClientFailure() {
- LOG4CXX_AUTO_TRACE(logger_);
- const int avahi_errno = avahi_client_errno(avahi_client_);
- if (avahi_errno == AVAHI_ERR_DISCONNECTED) {
- LOG4CXX_DEBUG(logger_, "AvahiClient disconnected");
- CreateAvahiClientAndBrowser();
- } else {
- LOG4CXX_ERROR(logger_,
- "AvahiClient failure: " << avahi_strerror(avahi_errno));
- }
-}
-
-void AvahiClientCallback(AvahiClient* avahi_client,
- AvahiClientState avahi_client_state, void* data) {
- LOG4CXX_AUTO_TRACE(logger_);
- LOG4CXX_DEBUG(
- logger_,
- "avahi_client " << avahi_client << ", avahi_client_state " << avahi_client_state << ", data " << data);
- DnssdServiceBrowser* dnssd_service_browser =
- static_cast<DnssdServiceBrowser*>(data);
-
- switch (avahi_client_state) {
- case AVAHI_CLIENT_S_RUNNING:
- dnssd_service_browser->OnClientConnected();
- LOG4CXX_DEBUG(logger_, "avahi_client_state: AVAHI_CLIENT_S_RUNNING");
- break;
- case AVAHI_CLIENT_FAILURE:
- dnssd_service_browser->OnClientFailure();
- LOG4CXX_DEBUG(logger_, "avahi_client_state: AVAHI_CLIENT_FAILURE");
- break;
- default: {
- LOG4CXX_ERROR(logger_,
- "Unknown avahi_client_state: " << avahi_client_state);
- }
- }
-}
-
-void AvahiServiceBrowserCallback(AvahiServiceBrowser* avahi_service_browser,
- AvahiIfIndex interface, AvahiProtocol protocol,
- AvahiBrowserEvent event, const char* name,
- const char* type, const char* domain,
- AvahiLookupResultFlags flags, void* data) {
- LOG4CXX_AUTO_TRACE(logger_);
- LOG4CXX_DEBUG(
- logger_,
- "avahi_service_browser " << avahi_service_browser << " interface " << interface <<
- " protocol " << protocol << " event " << event << " name " << name <<
- " type " << type << " domain " << domain << " flags " << flags << " data " << data);
- DnssdServiceBrowser* dnssd_service_browser =
- static_cast<DnssdServiceBrowser*>(data);
-
- switch (event) {
- case AVAHI_BROWSER_FAILURE:
- LOG4CXX_ERROR(
- logger_,
- "AvahiServiceBrowser failure: " << avahi_strerror(avahi_client_errno(
- avahi_service_browser_get_client(avahi_service_browser))));
- break;
-
- case AVAHI_BROWSER_NEW:
- dnssd_service_browser->AddService(interface, protocol, name, type,
- domain);
- LOG4CXX_DEBUG(logger_, "event: AVAHI_BROWSER_NEW");
- break;
-
- case AVAHI_BROWSER_REMOVE:
- dnssd_service_browser->RemoveService(interface, protocol, name, type,
- domain);
- LOG4CXX_DEBUG(logger_, "event: AVAHI_BROWSER_REMOVE");
- break;
-
- case AVAHI_BROWSER_ALL_FOR_NOW:
- LOG4CXX_DEBUG(logger_, "event: AVAHI_BROWSER_ALL_FOR_NOW");
- break;
- case AVAHI_BROWSER_CACHE_EXHAUSTED:
- LOG4CXX_DEBUG(logger_, "event: AVAHI_BROWSER_CACHE_EXHAUSTED");
- break;
- }
- LOG4CXX_TRACE(logger_, "exit");
-}
-
-void DnssdServiceBrowser::ServiceResolved(
- const DnssdServiceRecord& service_record) {
- LOG4CXX_AUTO_TRACE(logger_);
- sync_primitives::AutoLock locker(mutex_);
- ServiceRecords::iterator service_record_it = std::find(
- service_records_.begin(), service_records_.end(), service_record);
- if (service_record_it != service_records_.end()) {
- *service_record_it = service_record;
- }
- DeviceVector device_vector = PrepareDeviceVector();
- controller_->SearchDeviceDone(device_vector);
-}
-
-void DnssdServiceBrowser::ServiceResolveFailed(
- const DnssdServiceRecord& service_record) {
- LOG4CXX_AUTO_TRACE(logger_);
- LOG4CXX_DEBUG(logger_,
- "AvahiServiceResolver failure for: " << service_record.name);
- sync_primitives::AutoLock locker(mutex_);
- ServiceRecords::iterator service_record_it = std::find(
- service_records_.begin(), service_records_.end(), service_record);
- if (service_record_it != service_records_.end()) {
- service_records_.erase(service_record_it);
- }
-}
-
-void AvahiServiceResolverCallback(AvahiServiceResolver* avahi_service_resolver,
- AvahiIfIndex interface,
- AvahiProtocol protocol,
- AvahiResolverEvent event, const char* name,
- const char* type, const char* domain,
- const char* host_name,
- const AvahiAddress* avahi_address,
- uint16_t port, AvahiStringList* txt,
- AvahiLookupResultFlags flags, void* data) {
- LOG4CXX_AUTO_TRACE(logger_);
- LOG4CXX_DEBUG(
- logger_,
- "avahi_service_resolver " << avahi_service_resolver << " interface " << interface <<
- " protocol " << protocol << " event " << event << " name " << name <<
- " type " << type << " domain " << domain << " host_name " << host_name <<
- " avahi_address " << avahi_address << " port " << port <<
- " txt " << txt << " flags " << flags << " data " << data);
- DnssdServiceBrowser* dnssd_service_browser =
- static_cast<DnssdServiceBrowser*>(data);
-
- DnssdServiceRecord service_record;
- service_record.interface = interface;
- service_record.protocol = protocol;
- service_record.domain_name = domain;
- service_record.host_name = host_name;
- service_record.name = name;
- service_record.type = type;
- switch (event) {
- case AVAHI_RESOLVER_FOUND:
- service_record.addr = avahi_address->data.ipv4.address;
- service_record.port = port;
- dnssd_service_browser->ServiceResolved(service_record);
- LOG4CXX_DEBUG(logger_, "event: AVAHI_RESOLVER_FOUND");
- break;
- case AVAHI_RESOLVER_FAILURE:
- dnssd_service_browser->ServiceResolveFailed(service_record);
- LOG4CXX_DEBUG(logger_, "event: AVAHI_RESOLVER_FAILURE");
- break;
- }
-
- avahi_service_resolver_free(avahi_service_resolver);
-}
-
-TransportAdapter::Error DnssdServiceBrowser::CreateAvahiClientAndBrowser() {
- LOG4CXX_AUTO_TRACE(logger_);
- if (0 != avahi_service_browser_) {
- avahi_service_browser_free(avahi_service_browser_);
- avahi_service_browser_ = NULL;
- }
- if (0 != avahi_client_) {
- avahi_client_free(avahi_client_);
- avahi_client_ = NULL;
- }
-
- int avahi_error;
- avahi_client_ = avahi_client_new(
- avahi_threaded_poll_get(avahi_threaded_poll_), AVAHI_CLIENT_NO_FAIL,
- AvahiClientCallback, this, &avahi_error);
- if (0 == avahi_client_) {
- LOG4CXX_ERROR(
- logger_,
- "Failed to create AvahiClient: " << avahi_strerror(avahi_error));
- return TransportAdapter::FAIL;
- }
-
- mutex_.Acquire();
- service_records_.clear();
- mutex_.Release();
-
- avahi_service_browser_ = avahi_service_browser_new(
- avahi_client_, AVAHI_IF_UNSPEC, /* TODO use only required iface */
- AVAHI_PROTO_INET, DNSSD_DEFAULT_SERVICE_TYPE, NULL, /* use default domain */
- static_cast<AvahiLookupFlags>(0), AvahiServiceBrowserCallback, this);
- return TransportAdapter::OK;
-}
-
-TransportAdapter::Error DnssdServiceBrowser::Init() {
- LOG4CXX_AUTO_TRACE(logger_);
- avahi_threaded_poll_ = avahi_threaded_poll_new();
- if (0 == avahi_threaded_poll_) {
- LOG4CXX_ERROR(logger_, "Failed to create AvahiThreadedPoll");
- return TransportAdapter::FAIL;
- }
-
- const TransportAdapter::Error err = CreateAvahiClientAndBrowser();
- if (err != TransportAdapter::OK) {
- LOG4CXX_ERROR(logger_, "Error " << err);
- return err;
- }
-
- const int poll_start_status = avahi_threaded_poll_start(avahi_threaded_poll_);
- if (0 != poll_start_status) {
- LOG4CXX_ERROR(logger_, "Failed to start AvahiThreadedPoll");
- return TransportAdapter::FAIL;
- }
- return TransportAdapter::OK;
-}
-
-TransportAdapter::Error DnssdServiceBrowser::Scan() {
- return TransportAdapter::NOT_SUPPORTED;
-}
-
-void DnssdServiceBrowser::AddService(AvahiIfIndex interface,
- AvahiProtocol protocol, const char* name,
- const char* type, const char* domain) {
- LOG4CXX_AUTO_TRACE(logger_);
- LOG4CXX_DEBUG(
- logger_,
- "interface " << interface << " protocol " << protocol << " name " << name << " type " << type << " domain " << domain);
- DnssdServiceRecord record;
- record.interface = interface;
- record.protocol = protocol;
- record.domain_name = domain;
- record.name = name;
- record.type = type;
-
- sync_primitives::AutoLock locker(mutex_);
- if (service_records_.end()
- == std::find(service_records_.begin(), service_records_.end(), record)) {
- service_records_.push_back(record);
- avahi_service_resolver_new(avahi_client_, interface, protocol, name, type,
- domain, AVAHI_PROTO_INET,
- static_cast<AvahiLookupFlags>(0),
- AvahiServiceResolverCallback, this);
- }
-}
-
-void DnssdServiceBrowser::RemoveService(AvahiIfIndex interface,
- AvahiProtocol protocol,
- const char* name, const char* type,
- const char* domain) {
- LOG4CXX_AUTO_TRACE(logger_);
- LOG4CXX_DEBUG(
- logger_,
- "interface " << interface << " protocol " << protocol << " name " << name << " type " << type << " domain " << domain);
- DnssdServiceRecord record;
- record.interface = interface;
- record.protocol = protocol;
- record.name = name;
- record.type = type;
- record.domain_name = domain;
-
- sync_primitives::AutoLock locker(mutex_);
- service_records_.erase(
- std::remove(service_records_.begin(), service_records_.end(), record),
- service_records_.end());
-}
-
-DeviceVector DnssdServiceBrowser::PrepareDeviceVector() const {
- LOG4CXX_AUTO_TRACE(logger_);
- std::map<uint32_t, TcpDevice*> devices;
- for (ServiceRecords::const_iterator it = service_records_.begin();
- it != service_records_.end(); ++it) {
- const DnssdServiceRecord& service_record = *it;
- if (service_record.host_name.empty()) {
- continue;
- }
- if (devices[service_record.addr] == 0) {
- devices[service_record.addr] = new TcpDevice(service_record.addr,
- service_record.host_name);
- }
- if (devices[service_record.addr] != 0) {
- devices[service_record.addr]->AddDiscoveredApplication(
- service_record.port);
- }
- }
- DeviceVector device_vector;
- device_vector.reserve(devices.size());
- for (std::map<uint32_t, TcpDevice*>::const_iterator it = devices.begin();
- it != devices.end(); ++it) {
- device_vector.push_back(DeviceSptr(it->second));
- }
- return device_vector;
-}
-
-} // namespace transport_adapter
-} // namespace transport_manager
diff --git a/src/components/transport_manager/src/tcp/tcp_client_listener.cc b/src/components/transport_manager/src/tcp/tcp_client_listener.cc
index 2ee2e432cb..7a074d9ae9 100644
--- a/src/components/transport_manager/src/tcp/tcp_client_listener.cc
+++ b/src/components/transport_manager/src/tcp/tcp_client_listener.cc
@@ -42,12 +42,12 @@
#include <sys/sysctl.h>
#include <sys/socket.h>
#ifdef __linux__
-# include <linux/tcp.h>
+#include <linux/tcp.h>
#else // __linux__
-# include <sys/time.h>
-# include <netinet/in.h>
-# include <netinet/tcp.h>
-# include <netinet/tcp_var.h>
+#include <sys/time.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <netinet/tcp_var.h>
#endif // __linux__
#include <sstream>
@@ -66,12 +66,12 @@ CREATE_LOGGERPTR_GLOBAL(logger_, "TransportManager")
TcpClientListener::TcpClientListener(TransportAdapterController* controller,
const uint16_t port,
const bool enable_keepalive)
- : port_(port),
- enable_keepalive_(enable_keepalive),
- controller_(controller),
- thread_(0),
- socket_(-1),
- thread_stop_requested_(false) {
+ : port_(port)
+ , enable_keepalive_(enable_keepalive)
+ , controller_(controller)
+ , thread_(0)
+ , socket_(-1)
+ , thread_stop_requested_(false) {
thread_ = threads::CreateThread("TcpClientListener",
new ListeningThreadDelegate(this));
}
@@ -86,7 +86,7 @@ TransportAdapter::Error TcpClientListener::Init() {
return TransportAdapter::FAIL;
}
- sockaddr_in server_address = { 0 };
+ sockaddr_in server_address = {0};
server_address.sin_family = AF_INET;
server_address.sin_port = htons(port_);
server_address.sin_addr.s_addr = INADDR_ANY;
@@ -96,7 +96,8 @@ TransportAdapter::Error TcpClientListener::Init() {
LOG4CXX_WARN_WITH_ERRNO(logger_, "setsockopt SO_REUSEADDR failed");
}
- if (bind(socket_, reinterpret_cast<sockaddr*>(&server_address),
+ if (bind(socket_,
+ reinterpret_cast<sockaddr*>(&server_address),
sizeof(server_address)) != 0) {
LOG4CXX_ERROR_WITH_ERRNO(logger_, "bind() failed");
return TransportAdapter::FAIL;
@@ -191,7 +192,7 @@ void SetKeepaliveOptions(const int fd) {
tval.tv_sec = keepidle;
setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &yes, sizeof(yes));
setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &tval, sizeof(tval));
-#endif // __QNX__
+#endif // __QNX__
}
void TcpClientListener::Loop() {
@@ -199,9 +200,8 @@ void TcpClientListener::Loop() {
while (!thread_stop_requested_) {
sockaddr_in client_address;
socklen_t client_address_size = sizeof(client_address);
- const int connection_fd = accept(socket_,
- (struct sockaddr*) &client_address,
- &client_address_size);
+ const int connection_fd = accept(
+ socket_, (struct sockaddr*)&client_address, &client_address_size);
if (thread_stop_requested_) {
LOG4CXX_DEBUG(logger_, "thread_stop_requested_");
close(connection_fd);
@@ -220,7 +220,8 @@ void TcpClientListener::Loop() {
}
char device_name[32];
- strncpy(device_name, inet_ntoa(client_address.sin_addr),
+ strncpy(device_name,
+ inet_ntoa(client_address.sin_addr),
sizeof(device_name) / sizeof(device_name[0]));
LOG4CXX_INFO(logger_, "Connected client " << device_name);
@@ -228,16 +229,15 @@ void TcpClientListener::Loop() {
SetKeepaliveOptions(connection_fd);
}
- TcpDevice* tcp_device = new TcpDevice(client_address.sin_addr.s_addr,
- device_name);
+ TcpDevice* tcp_device =
+ new TcpDevice(client_address.sin_addr.s_addr, device_name);
DeviceSptr device = controller_->AddDevice(tcp_device);
tcp_device = static_cast<TcpDevice*>(device.get());
- const ApplicationHandle app_handle = tcp_device->AddIncomingApplication(
- connection_fd);
+ const ApplicationHandle app_handle =
+ tcp_device->AddIncomingApplication(connection_fd);
- TcpSocketConnection* connection(
- new TcpSocketConnection(device->unique_device_id(), app_handle,
- controller_));
+ TcpSocketConnection* connection(new TcpSocketConnection(
+ device->unique_device_id(), app_handle, controller_));
connection->set_socket(connection_fd);
const TransportAdapter::Error error = connection->Start();
if (error != TransportAdapter::OK) {
@@ -251,7 +251,7 @@ void TcpClientListener::StopLoop() {
thread_stop_requested_ = true;
// We need to connect to the listening socket to unblock accept() call
int byesocket = socket(AF_INET, SOCK_STREAM, 0);
- sockaddr_in server_address = { 0 };
+ sockaddr_in server_address = {0};
server_address.sin_family = AF_INET;
server_address.sin_port = htons(port_);
server_address.sin_addr.s_addr = INADDR_ANY;
@@ -270,8 +270,9 @@ void TcpClientListener::StopLoop() {
TransportAdapter::Error TcpClientListener::StartListening() {
LOG4CXX_AUTO_TRACE(logger_);
if (thread_->is_running()) {
- LOG4CXX_WARN(logger_,
- "TransportAdapter::BAD_STATE. Listener has already been started");
+ LOG4CXX_WARN(
+ logger_,
+ "TransportAdapter::BAD_STATE. Listener has already been started");
return TransportAdapter::BAD_STATE;
}
@@ -293,8 +294,7 @@ void TcpClientListener::ListeningThreadDelegate::threadMain() {
TcpClientListener::ListeningThreadDelegate::ListeningThreadDelegate(
TcpClientListener* parent)
- : parent_(parent) {
-}
+ : parent_(parent) {}
TransportAdapter::Error TcpClientListener::StopListening() {
LOG4CXX_AUTO_TRACE(logger_);
diff --git a/src/components/transport_manager/src/tcp/tcp_connection_factory.cc b/src/components/transport_manager/src/tcp/tcp_connection_factory.cc
index 7c6c06ddc0..5b1e9af6cb 100644
--- a/src/components/transport_manager/src/tcp/tcp_connection_factory.cc
+++ b/src/components/transport_manager/src/tcp/tcp_connection_factory.cc
@@ -42,8 +42,7 @@ CREATE_LOGGERPTR_GLOBAL(logger_, "TransportManager")
TcpConnectionFactory::TcpConnectionFactory(
TransportAdapterController* controller)
- : controller_(controller) {
-}
+ : controller_(controller) {}
TransportAdapter::Error TcpConnectionFactory::Init() {
return TransportAdapter::OK;
@@ -52,13 +51,12 @@ TransportAdapter::Error TcpConnectionFactory::Init() {
TransportAdapter::Error TcpConnectionFactory::CreateConnection(
const DeviceUID& device_uid, const ApplicationHandle& app_handle) {
LOG4CXX_AUTO_TRACE(logger_);
- LOG4CXX_DEBUG(
- logger_,
- "DeviceUID: " << &device_uid << ", ApplicationHandle: " << &app_handle);
+ LOG4CXX_DEBUG(logger_,
+ "DeviceUID: " << &device_uid
+ << ", ApplicationHandle: " << &app_handle);
TcpServerOiginatedSocketConnection* connection(
- new TcpServerOiginatedSocketConnection(device_uid, app_handle,
- controller_));
- controller_->ConnectionCreated(connection, device_uid, app_handle);
+ new TcpServerOiginatedSocketConnection(
+ device_uid, app_handle, controller_));
if (connection->Start() == TransportAdapter::OK) {
LOG4CXX_DEBUG(logger_, "TCP connection initialised");
return TransportAdapter::OK;
@@ -68,15 +66,13 @@ TransportAdapter::Error TcpConnectionFactory::CreateConnection(
}
}
-void TcpConnectionFactory::Terminate() {
-}
+void TcpConnectionFactory::Terminate() {}
bool TcpConnectionFactory::IsInitialised() const {
return true;
}
-TcpConnectionFactory::~TcpConnectionFactory() {
-}
+TcpConnectionFactory::~TcpConnectionFactory() {}
} // namespace transport_adapter
} // namespace transport_manager
diff --git a/src/components/transport_manager/src/tcp/tcp_device.cc b/src/components/transport_manager/src/tcp/tcp_device.cc
index 2dacd9e8a2..d3f132759a 100644
--- a/src/components/transport_manager/src/tcp/tcp_device.cc
+++ b/src/components/transport_manager/src/tcp/tcp_device.cc
@@ -33,19 +33,16 @@
#include "utils/logger.h"
#include "transport_manager/tcp/tcp_device.h"
-
namespace transport_manager {
namespace transport_adapter {
-//CREATE_LOGGERPTR_LOCAL(logger_, "TransportManager")
CREATE_LOGGERPTR_GLOBAL(logger_, "TransportManager")
-
TcpDevice::TcpDevice(const in_addr_t& in_addr, const std::string& name)
- : Device(name, name),
- applications_mutex_(),
- in_addr_(in_addr),
- last_handle_(0) {
+ : Device(name, name)
+ , applications_mutex_()
+ , in_addr_(in_addr)
+ , last_handle_(0) {
LOG4CXX_AUTO_TRACE(logger_);
}
@@ -70,7 +67,9 @@ ApplicationList TcpDevice::GetApplicationList() const {
sync_primitives::AutoLock locker(applications_mutex_);
ApplicationList app_list;
for (std::map<ApplicationHandle, Application>::const_iterator it =
- applications_.begin(); it != applications_.end(); ++it) {
+ applications_.begin();
+ it != applications_.end();
+ ++it) {
app_list.push_back(it->first);
}
return app_list;
@@ -118,8 +117,8 @@ TcpDevice::~TcpDevice() {
int TcpDevice::GetApplicationSocket(const ApplicationHandle app_handle) const {
LOG4CXX_AUTO_TRACE(logger_);
LOG4CXX_DEBUG(logger_, "ApplicationHandle: " << app_handle);
- std::map<ApplicationHandle, Application>::const_iterator it = applications_
- .find(app_handle);
+ std::map<ApplicationHandle, Application>::const_iterator it =
+ applications_.find(app_handle);
if (applications_.end() == it) {
LOG4CXX_WARN(logger_, "Application was not found");
return -1;
@@ -135,8 +134,8 @@ int TcpDevice::GetApplicationSocket(const ApplicationHandle app_handle) const {
int TcpDevice::GetApplicationPort(const ApplicationHandle app_handle) const {
LOG4CXX_AUTO_TRACE(logger_);
LOG4CXX_DEBUG(logger_, "ApplicationHandle: " << app_handle);
- std::map<ApplicationHandle, Application>::const_iterator it = applications_
- .find(app_handle);
+ std::map<ApplicationHandle, Application>::const_iterator it =
+ applications_.find(app_handle);
if (applications_.end() == it) {
LOG4CXX_WARN(logger_, "Application was not found");
return -1;
diff --git a/src/components/transport_manager/src/tcp/tcp_socket_connection.cc b/src/components/transport_manager/src/tcp/tcp_socket_connection.cc
index c5d0e88d84..acab9f555f 100644
--- a/src/components/transport_manager/src/tcp/tcp_socket_connection.cc
+++ b/src/components/transport_manager/src/tcp/tcp_socket_connection.cc
@@ -51,24 +51,21 @@ CREATE_LOGGERPTR_GLOBAL(logger_, "TransportManager")
TcpSocketConnection::TcpSocketConnection(const DeviceUID& device_uid,
const ApplicationHandle& app_handle,
TransportAdapterController* controller)
- : ThreadedSocketConnection(device_uid, app_handle, controller) {
-}
+ : ThreadedSocketConnection(device_uid, app_handle, controller) {}
-TcpSocketConnection::~TcpSocketConnection() {
-}
+TcpSocketConnection::~TcpSocketConnection() {}
bool TcpSocketConnection::Establish(ConnectError** error) {
return true;
}
TcpServerOiginatedSocketConnection::TcpServerOiginatedSocketConnection(
- const DeviceUID& device_uid, const ApplicationHandle& app_handle,
+ const DeviceUID& device_uid,
+ const ApplicationHandle& app_handle,
TransportAdapterController* controller)
- : ThreadedSocketConnection(device_uid, app_handle, controller) {
-}
+ : ThreadedSocketConnection(device_uid, app_handle, controller) {}
-TcpServerOiginatedSocketConnection::~TcpServerOiginatedSocketConnection() {
-}
+TcpServerOiginatedSocketConnection::~TcpServerOiginatedSocketConnection() {}
bool TcpServerOiginatedSocketConnection::Establish(ConnectError** error) {
LOG4CXX_AUTO_TRACE(logger_);
@@ -84,9 +81,9 @@ bool TcpServerOiginatedSocketConnection::Establish(ConnectError** error) {
const int port = tcp_device->GetApplicationPort(application_handle());
if (-1 == port) {
- LOG4CXX_ERROR(
- logger_,
- "Application port for " << application_handle() << " not found");
+ LOG4CXX_ERROR(logger_,
+ "Application port for " << application_handle()
+ << " not found");
*error = new ConnectError();
return false;
}
@@ -98,17 +95,17 @@ bool TcpServerOiginatedSocketConnection::Establish(ConnectError** error) {
return false;
}
- struct sockaddr_in addr = { 0 };
+ struct sockaddr_in addr = {0};
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = tcp_device->in_addr();
addr.sin_port = htons(port);
LOG4CXX_DEBUG(logger_,
- "Connecting " << inet_ntoa(addr.sin_addr) << ":" << port);
- if (::connect(socket, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
- LOG4CXX_ERROR(
- logger_,
- "Failed to connect for application " << application_handle() << ", error " << errno);
+ "Connecting " << inet_ntoa(addr.sin_addr) << ":" << port);
+ if (::connect(socket, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
+ LOG4CXX_ERROR(logger_,
+ "Failed to connect for application " << application_handle()
+ << ", error " << errno);
*error = new ConnectError();
::close(socket);
return false;
diff --git a/src/components/transport_manager/src/tcp/tcp_transport_adapter.cc b/src/components/transport_manager/src/tcp/tcp_transport_adapter.cc
index 3998c02ab9..a389deb517 100644
--- a/src/components/transport_manager/src/tcp/tcp_transport_adapter.cc
+++ b/src/components/transport_manager/src/tcp/tcp_transport_adapter.cc
@@ -42,33 +42,26 @@
#include "utils/logger.h"
#include "utils/threads/thread_delegate.h"
-#include "resumption/last_state.h"
#include "transport_manager/tcp/tcp_client_listener.h"
#include "transport_manager/tcp/tcp_connection_factory.h"
#include "transport_manager/tcp/tcp_device.h"
-#ifdef AVAHI_SUPPORT
-#include "transport_manager/tcp/dnssd_service_browser.h"
-#endif
-
namespace transport_manager {
namespace transport_adapter {
-CREATE_LOGGERPTR_GLOBAL(logger_, "TransportAdapterImpl")
+CREATE_LOGGERPTR_GLOBAL(logger_, "TransportManager")
-TcpTransportAdapter::TcpTransportAdapter(const uint16_t port)
- : TransportAdapterImpl(
-#ifdef AVAHI_SUPPORT
- new DnssdServiceBrowser(this),
-#else
- NULL,
-#endif
+TcpTransportAdapter::TcpTransportAdapter(
+ const uint16_t port,
+ resumption::LastState& last_state,
+ const TransportManagerSettings& settings)
+ : TransportAdapterImpl(NULL,
new TcpConnectionFactory(this),
- new TcpClientListener(this, port, true)) {
-}
+ new TcpClientListener(this, port, true),
+ last_state,
+ settings) {}
-TcpTransportAdapter::~TcpTransportAdapter() {
-}
+TcpTransportAdapter::~TcpTransportAdapter() {}
DeviceType TcpTransportAdapter::GetDeviceType() const {
return TCP;
@@ -80,14 +73,14 @@ void TcpTransportAdapter::Store() const {
Json::Value devices_dictionary;
DeviceList device_ids = GetDeviceList();
for (DeviceList::const_iterator i = device_ids.begin(); i != device_ids.end();
- ++i) {
+ ++i) {
DeviceUID device_id = *i;
DeviceSptr device = FindDevice(device_id);
if (!device) { // device could have been disconnected
continue;
}
- utils::SharedPtr<TcpDevice> tcp_device = DeviceSptr::static_pointer_cast<
- TcpDevice>(device);
+ utils::SharedPtr<TcpDevice> tcp_device =
+ DeviceSptr::static_pointer_cast<TcpDevice>(device);
Json::Value device_dictionary;
device_dictionary["name"] = tcp_device->name();
struct in_addr address;
@@ -96,7 +89,8 @@ void TcpTransportAdapter::Store() const {
Json::Value applications_dictionary;
ApplicationList app_ids = tcp_device->GetApplicationList();
for (ApplicationList::const_iterator j = app_ids.begin();
- j != app_ids.end(); ++j) {
+ j != app_ids.end();
+ ++j) {
ApplicationHandle app_handle = *j;
if (FindEstablishedConnection(tcp_device->unique_device_id(),
app_handle)) {
@@ -116,18 +110,19 @@ void TcpTransportAdapter::Store() const {
}
}
tcp_adapter_dictionary["devices"] = devices_dictionary;
- Json::Value& dictionary = resumption::LastState::instance()->dictionary;
+ Json::Value& dictionary = last_state().dictionary;
dictionary["TransportManager"]["TcpAdapter"] = tcp_adapter_dictionary;
}
bool TcpTransportAdapter::Restore() {
LOG4CXX_AUTO_TRACE(logger_);
bool errors_occurred = false;
- const Json::Value tcp_adapter_dictionary = resumption::LastState::instance()
- ->dictionary["TransportManager"]["TcpAdapter"];
+ const Json::Value tcp_adapter_dictionary =
+ last_state().dictionary["TransportManager"]["TcpAdapter"];
const Json::Value devices_dictionary = tcp_adapter_dictionary["devices"];
for (Json::Value::const_iterator i = devices_dictionary.begin();
- i != devices_dictionary.end(); ++i) {
+ i != devices_dictionary.end();
+ ++i) {
const Json::Value device_dictionary = *i;
std::string name = device_dictionary["name"].asString();
std::string address_record = device_dictionary["address"].asString();
@@ -138,7 +133,8 @@ bool TcpTransportAdapter::Restore() {
const Json::Value applications_dictionary =
device_dictionary["applications"];
for (Json::Value::const_iterator j = applications_dictionary.begin();
- j != applications_dictionary.end(); ++j) {
+ j != applications_dictionary.end();
+ ++j) {
const Json::Value application_dictionary = *j;
std::string port_record = application_dictionary["port"].asString();
int port = atoi(port_record.c_str());
@@ -155,4 +151,3 @@ bool TcpTransportAdapter::Restore() {
} // namespace transport_adapter
} // namespace transport_manager
-