summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAKalinich-Luxoft <AKalinich@luxoft.com>2017-12-22 15:13:21 +0200
committerAKalinich-Luxoft <AKalinich@luxoft.com>2018-01-29 09:04:14 +0200
commit7898292b5de8d80354ab3b9fecacbadcf9d41c61 (patch)
treecf588c13a8f522d5672c90316b31f87e42ee03f9
parent96a14a8de28ea2ad877745494f2e53211ffb34b9 (diff)
downloadsdl_core-7898292b5de8d80354ab3b9fecacbadcf9d41c61.tar.gz
Refactoring in connection/protocol handlers
NotifySessionStartedResult function were updated to use StartingSessionContext class for accessing to needed data instead of tones of separate parameters. Also in protocol handler some raw pointer data were replaced with smart objects.
-rw-r--r--src/components/connection_handler/include/connection_handler/connection_handler_impl.h15
-rw-r--r--src/components/connection_handler/src/connection_handler_impl.cc96
-rw-r--r--src/components/include/protocol_handler/protocol_handler.h16
-rw-r--r--src/components/include/protocol_handler/session_observer.h16
-rw-r--r--src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h13
-rw-r--r--src/components/protocol_handler/src/protocol_handler_impl.cc91
6 files changed, 113 insertions, 134 deletions
diff --git a/src/components/connection_handler/include/connection_handler/connection_handler_impl.h b/src/components/connection_handler/include/connection_handler/connection_handler_impl.h
index 7355838b63..ca5ffe1cc8 100644
--- a/src/components/connection_handler/include/connection_handler/connection_handler_impl.h
+++ b/src/components/connection_handler/include/connection_handler/connection_handler_impl.h
@@ -539,19 +539,6 @@ class ConnectionHandlerImpl
void OnConnectionEnded(const transport_manager::ConnectionUID connection_id);
- /**
- * \brief Convenient method to call NotifySessionStartedResult() with
- * negative result.
- * \param connection_handle Identifier of connection within which session
- * exists
- * \param session_id session ID passed to OnSessionStartedCallback()
- * \param is_protected whether the service would be protected
- **/
- void NotifySessionStartedFailure(
- const transport_manager::ConnectionUID connection_handle,
- const uint8_t session_id,
- bool is_protected);
-
const ConnectionHandlerSettings& settings_;
/**
* \brief Pointer to observer
@@ -587,7 +574,7 @@ class ConnectionHandlerImpl
utils::StlMapDeleter<ConnectionList> connection_list_deleter_;
sync_primitives::Lock start_service_context_map_lock_;
- std::map<uint32_t, protocol_handler::StartingSessionContext>
+ std::map<uint32_t, protocol_handler::SessionContext>
start_service_context_map_;
#ifdef BUILD_TESTS
diff --git a/src/components/connection_handler/src/connection_handler_impl.cc b/src/components/connection_handler/src/connection_handler_impl.cc
index c8f3dd8932..b5d0857514 100644
--- a/src/components/connection_handler/src/connection_handler_impl.cc
+++ b/src/components/connection_handler/src/connection_handler_impl.cc
@@ -412,18 +412,17 @@ void ConnectionHandlerImpl::OnSessionStartedCallback(
const BsonObject* params) {
LOG4CXX_AUTO_TRACE(logger_);
- uint32_t new_session_id = 0;
- uint32_t hash_id = protocol_handler::HASH_ID_WRONG;
+ std::vector<std::string> rejected_params;
+ protocol_handler::SessionContext context(connection_handle,
+ session_id,
+ 0,
+ service_type,
+ protocol_handler::HASH_ID_WRONG,
+ is_protected);
#ifdef ENABLE_SECURITY
if (!AllowProtection(get_settings(), service_type, is_protected)) {
- std::vector<std::string> empty;
- protocol_handler_->NotifySessionStartedResult(connection_handle,
- session_id,
- new_session_id,
- hash_id,
- is_protected,
- empty);
+ protocol_handler_->NotifySessionStarted(context, rejected_params);
return;
}
#endif // ENABLE_SECURITY
@@ -431,19 +430,22 @@ void ConnectionHandlerImpl::OnSessionStartedCallback(
ConnectionList::iterator it = connection_list_.find(connection_handle);
if (connection_list_.end() == it) {
LOG4CXX_ERROR(logger_, "Unknown connection!");
- NotifySessionStartedFailure(connection_handle, session_id, is_protected);
+ protocol_handler_->NotifySessionStarted(context, rejected_params);
return;
}
Connection* connection = it->second;
+ context.is_new_service_ =
+ !connection->IsSessionServiceExists(session_id, service_type);
+
if ((0 == session_id) && (protocol_handler::kRpc == service_type)) {
- new_session_id = connection->AddNewSession();
- if (0 == new_session_id) {
+ context.new_session_id_ = connection->AddNewSession();
+ if (0 == context.new_session_id_) {
LOG4CXX_ERROR(logger_, "Couldn't start new session!");
- NotifySessionStartedFailure(connection_handle, session_id, is_protected);
+ protocol_handler_->NotifySessionStarted(context, rejected_params);
return;
}
- hash_id = KeyFromPair(connection_handle, new_session_id);
+ context.hash_id_ = KeyFromPair(connection_handle, context.new_session_id_);
} else { // Could be create new service or protected exists one
if (!connection->AddNewService(session_id, service_type, is_protected)) {
LOG4CXX_ERROR(logger_,
@@ -453,22 +455,25 @@ void ConnectionHandlerImpl::OnSessionStartedCallback(
#endif // ENABLE_SECURITY
<< " service " << static_cast<int>(service_type)
<< " for session " << static_cast<int>(session_id));
- NotifySessionStartedFailure(connection_handle, session_id, is_protected);
+ protocol_handler_->NotifySessionStarted(context, rejected_params);
return;
}
- new_session_id = session_id;
- hash_id = protocol_handler::HASH_ID_NOT_SUPPORTED;
+ context.new_session_id_ = session_id;
+ context.hash_id_ = protocol_handler::HASH_ID_NOT_SUPPORTED;
}
sync_primitives::AutoReadLock read_lock(connection_handler_observer_lock_);
if (connection_handler_observer_) {
- const uint32_t session_key = KeyFromPair(connection_handle, new_session_id);
+ const uint32_t session_key =
+ KeyFromPair(connection_handle, context.new_session_id_);
+
+ uint32_t app_id = 0;
+ GetDataOnSessionKey(
+ session_key, &app_id, NULL, static_cast<DeviceHandle*>(NULL));
+ if (app_id > 0) {
+ context.is_ptu_required_ =
+ !connection_handler_observer_->CheckAppIsNavi(app_id);
+ }
- ServiceStartedContext context(connection_handle,
- session_id,
- new_session_id,
- service_type,
- hash_id,
- is_protected);
{
sync_primitives::AutoLock auto_lock(start_service_context_map_lock_);
start_service_context_map_[session_key] = context;
@@ -481,13 +486,7 @@ void ConnectionHandlerImpl::OnSessionStartedCallback(
params);
} else {
if (protocol_handler_) {
- std::vector<std::string> empty;
- protocol_handler_->NotifySessionStartedResult(connection_handle,
- session_id,
- new_session_id,
- hash_id,
- is_protected,
- empty);
+ protocol_handler_->NotifySessionStarted(context, rejected_params);
}
}
}
@@ -498,11 +497,10 @@ void ConnectionHandlerImpl::NotifyServiceStartedResult(
std::vector<std::string>& rejected_params) {
LOG4CXX_AUTO_TRACE(logger_);
- ServiceStartedContext context;
+ protocol_handler::SessionContext context;
{
sync_primitives::AutoLock auto_lock(start_service_context_map_lock_);
- std::map<uint32_t, ServiceStartedContext>::iterator it =
- start_service_context_map_.find(session_key);
+ auto it = start_service_context_map_.find(session_key);
if (it == start_service_context_map_.end()) {
LOG4CXX_ERROR(logger_, "context for start service not found!");
return;
@@ -514,8 +512,7 @@ void ConnectionHandlerImpl::NotifyServiceStartedResult(
Connection* connection = NULL;
{
sync_primitives::AutoReadLock lock(connection_list_lock_);
- ConnectionList::iterator it =
- connection_list_.find(context.connection_handle_);
+ ConnectionList::iterator it = connection_list_.find(context.connection_id_);
if (connection_list_.end() == it) {
LOG4CXX_ERROR(logger_, "connection not found");
return;
@@ -529,35 +526,14 @@ void ConnectionHandlerImpl::NotifyServiceStartedResult(
if (protocol_handler::kRpc == context.service_type_) {
connection->RemoveSession(context.new_session_id_);
} else {
- connection->RemoveService(context.session_id_, context.service_type_);
+ connection->RemoveService(context.initial_session_id_,
+ context.service_type_);
}
context.new_session_id_ = 0;
}
if (protocol_handler_ != NULL) {
- protocol_handler_->NotifySessionStartedResult(context.connection_handle_,
- context.session_id_,
- context.new_session_id_,
- context.hash_id_,
- context.is_protected_,
- rejected_params);
- }
-}
-
-void ConnectionHandlerImpl::NotifySessionStartedFailure(
- const transport_manager::ConnectionUID connection_handle,
- const uint8_t session_id,
- bool is_protected) {
- LOG4CXX_AUTO_TRACE(logger_);
- if (protocol_handler_) {
- std::vector<std::string> empty;
- protocol_handler_->NotifySessionStartedResult(
- connection_handle,
- session_id,
- 0,
- protocol_handler::HASH_ID_WRONG,
- is_protected,
- empty);
+ protocol_handler_->NotifySessionStarted(context, rejected_params);
}
}
diff --git a/src/components/include/protocol_handler/protocol_handler.h b/src/components/include/protocol_handler/protocol_handler.h
index 84121c59be..ed40e9e2d6 100644
--- a/src/components/include/protocol_handler/protocol_handler.h
+++ b/src/components/include/protocol_handler/protocol_handler.h
@@ -43,7 +43,7 @@ namespace protocol_handler {
class ProtocolObserver;
class SessionObserver;
-struct StartingSessionContext;
+struct SessionContext;
/**
* \class ProtocolHandler
@@ -123,7 +123,7 @@ class ProtocolHandler {
* Only valid when generated_session_id is 0. Note, even if
* generated_session_id is 0, the list may be empty.
*/
- virtual void NotifySessionStartedResult(
+ DEPRECATED virtual void NotifySessionStartedResult(
int32_t connection_id,
uint8_t session_id,
uint8_t generated_session_id,
@@ -131,6 +131,18 @@ class ProtocolHandler {
bool protection,
std::vector<std::string>& rejected_params) = 0;
+ /**
+ * @brief Called by connection handler to notify the result of
+ * OnSessionStartedCallback().
+ * @param context reference to structure with started session data
+ * @param rejected_params list of parameters name that are rejected.
+ * Only valid when generated_session_id is 0. Note, even if
+ * generated_session_id is 0, the list may be empty.
+ */
+ virtual void NotifySessionStarted(
+ const SessionContext& context,
+ std::vector<std::string>& rejected_params) = 0;
+
protected:
/**
* \brief Destructor
diff --git a/src/components/include/protocol_handler/session_observer.h b/src/components/include/protocol_handler/session_observer.h
index 36780da639..6cc0927639 100644
--- a/src/components/include/protocol_handler/session_observer.h
+++ b/src/components/include/protocol_handler/session_observer.h
@@ -58,7 +58,7 @@ enum { HASH_ID_NOT_SUPPORTED = 0, HASH_ID_WRONG = 0xFFFF0000 };
/**
* @brief Struct with data containing attributes of starting session
**/
-struct StartingSessionContext {
+struct SessionContext {
transport_manager::ConnectionUID connection_id_;
uint8_t initial_session_id_;
uint8_t new_session_id_;
@@ -71,7 +71,7 @@ struct StartingSessionContext {
/**
* @brief Constructor
*/
- StartingSessionContext()
+ SessionContext()
: connection_id_(0)
, initial_session_id_(0)
, new_session_id_(0)
@@ -93,12 +93,12 @@ struct StartingSessionContext {
* @param is_protected Whether service will be protected
* @param is_new_service Whether service was already established
**/
- StartingSessionContext(transport_manager::ConnectionUID connection_id,
- uint8_t session_id,
- uint8_t new_session_id,
- protocol_handler::ServiceType service_type,
- uint32_t hash_id,
- const bool is_protected)
+ SessionContext(transport_manager::ConnectionUID connection_id,
+ uint8_t session_id,
+ uint8_t new_session_id,
+ protocol_handler::ServiceType service_type,
+ uint32_t hash_id,
+ const bool is_protected)
: connection_id_(connection_id)
, initial_session_id_(session_id)
, new_session_id_(new_session_id)
diff --git a/src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h b/src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h
index dc1a119223..65f03900db 100644
--- a/src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h
+++ b/src/components/protocol_handler/include/protocol_handler/protocol_handler_impl.h
@@ -391,7 +391,7 @@ class ProtocolHandlerImpl
* Only valid when generated_session_id is 0. Note, even if
* generated_session_id is 0, the list may be empty.
*/
- void NotifySessionStartedResult(
+ DEPRECATED void NotifySessionStartedResult(
int32_t connection_id,
uint8_t session_id,
uint8_t generated_session_id,
@@ -399,6 +399,17 @@ class ProtocolHandlerImpl
bool protection,
std::vector<std::string>& rejected_params) OVERRIDE;
+ /**
+ * @brief Called by connection handler to notify the result of
+ * OnSessionStartedCallback().
+ * @param context reference to structure with started session data
+ * @param rejected_params list of parameters name that are rejected.
+ * Only valid when generated_session_id is 0. Note, even if
+ * generated_session_id is 0, the list may be empty.
+ */
+ void NotifySessionStarted(const SessionContext& context,
+ std::vector<std::string>& rejected_params) OVERRIDE;
+
#ifdef BUILD_TESTS
const impl::FromMobileQueue& get_from_mobile_queue() const {
return raw_ford_messages_from_mobile_;
diff --git a/src/components/protocol_handler/src/protocol_handler_impl.cc b/src/components/protocol_handler/src/protocol_handler_impl.cc
index 1ffbff3b08..3d97201ab4 100644
--- a/src/components/protocol_handler/src/protocol_handler_impl.cc
+++ b/src/components/protocol_handler/src/protocol_handler_impl.cc
@@ -1285,12 +1285,14 @@ RESULT_CODE ProtocolHandlerImpl::HandleControlMessageStartSession(
security_manager_->AddListener(
new HandshakeHandler(*this,
session_observer_,
+ connection_key,
connection_id,
session_id,
packet.protocol_version(),
hash_id,
service_type,
get_settings().force_protected_service(),
+ false,
*fullVersion,
NULL));
if (!ssl_context->IsHandshakePending()) {
@@ -1390,11 +1392,25 @@ void ProtocolHandlerImpl::NotifySessionStartedResult(
uint32_t hash_id,
bool protection,
std::vector<std::string>& rejected_params) {
+ LOG4CXX_AUTO_TRACE(logger_);
+ protocol_handler::SessionContext context(connection_id,
+ session_id,
+ generated_session_id,
+ ServiceType::kInvalidServiceType,
+ hash_id,
+ protection);
+ NotifySessionStarted(context, rejected_params);
+}
+
+void ProtocolHandlerImpl::NotifySessionStarted(
+ const SessionContext& context, std::vector<std::string>& rejected_params) {
+ LOG4CXX_AUTO_TRACE(logger_);
+
ProtocolFramePtr packet;
{
sync_primitives::AutoLock auto_lock(start_session_frame_map_lock_);
StartSessionFrameMap::iterator it = start_session_frame_map_.find(
- std::make_pair(connection_id, session_id));
+ std::make_pair(context.connection_id_, context.initial_session_id_));
if (it == start_session_frame_map_.end()) {
LOG4CXX_ERROR(logger_, "Cannot find Session Started packet");
return;
@@ -1406,11 +1422,11 @@ void ProtocolHandlerImpl::NotifySessionStartedResult(
const ServiceType service_type = ServiceTypeFromByte(packet->service_type());
const uint8_t protocol_version = packet->protocol_version();
- if (0 == generated_session_id) {
+ if (0 == context.new_session_id_) {
LOG4CXX_WARN(logger_,
"Refused by session_observer to create service "
<< static_cast<int32_t>(service_type) << " type.");
- SendStartSessionNAck(connection_id,
+ SendStartSessionNAck(context.connection_id_,
packet->session_id(),
protocol_version,
packet->service_type(),
@@ -1418,8 +1434,9 @@ void ProtocolHandlerImpl::NotifySessionStartedResult(
return;
}
- BsonObject start_session_ack_params;
- bson_object_initialize_default(&start_session_ack_params);
+ std::shared_ptr<BsonObject> start_session_ack_params(
+ new BsonObject(), [](BsonObject* obj) { bson_object_deinitialize(obj); });
+ bson_object_initialize_default(start_session_ack_params.get());
// when video service is successfully started, copy input parameters
// ("width", "height", "videoProtocol", "videoCodec") to the ACK packet
if (packet->service_type() == kMobileNav && packet->data() != NULL) {
@@ -1428,13 +1445,13 @@ void ProtocolHandlerImpl::NotifySessionStartedResult(
if ((element = bson_object_get(&req_param, strings::height)) != NULL &&
element->type == TYPE_INT32) {
- bson_object_put_int32(&start_session_ack_params,
+ bson_object_put_int32(start_session_ack_params.get(),
strings::height,
bson_object_get_int32(&req_param, strings::height));
}
if ((element = bson_object_get(&req_param, strings::width)) != NULL &&
element->type == TYPE_INT32) {
- bson_object_put_int32(&start_session_ack_params,
+ bson_object_put_int32(start_session_ack_params.get(),
strings::width,
bson_object_get_int32(&req_param, strings::width));
}
@@ -1442,17 +1459,17 @@ void ProtocolHandlerImpl::NotifySessionStartedResult(
bson_object_get_string(&req_param, strings::video_protocol);
if (protocol != NULL) {
bson_object_put_string(
- &start_session_ack_params, strings::video_protocol, protocol);
+ start_session_ack_params.get(), strings::video_protocol, protocol);
}
char* codec = bson_object_get_string(&req_param, strings::video_codec);
if (codec != NULL) {
bson_object_put_string(
- &start_session_ack_params, strings::video_codec, codec);
+ start_session_ack_params.get(), strings::video_codec, codec);
}
bson_object_deinitialize(&req_param);
}
- ProtocolPacket::ProtocolVersion* fullVersion;
+ std::shared_ptr<ProtocolPacket::ProtocolVersion> fullVersion;
// Can't check protocol_version because the first packet is v1, but there
// could still be a payload, in which case we can get the real protocol
@@ -1487,22 +1504,15 @@ void ProtocolHandlerImpl::NotifySessionStartedResult(
connection_key,
security_manager::SecurityManager::ERROR_INTERNAL,
error);
- // Start service without protection
- SendStartSessionAck(connection_id,
- generated_session_id,
- packet->protocol_version(),
- hash_id,
- packet->service_type(),
- PROTECTION_OFF,
- *fullVersion,
- start_session_ack_params);
- delete fullVersion;
- bson_object_deinitialize(&start_session_ack_params);
+
+ handler->OnHandshakeDone(
+ connection_key, security_manager::SSLContext::Handshake_Result_Fail);
+
return;
}
if (!rejected_params.empty()) {
- SendStartSessionNAck(connection_id,
+ SendStartSessionNAck(context.connection_id_,
packet->session_id(),
protocol_version,
packet->service_type(),
@@ -1511,36 +1521,21 @@ void ProtocolHandlerImpl::NotifySessionStartedResult(
// mark service as protected
session_observer_.SetProtectionFlag(connection_key, service_type);
// Start service as protected with current SSLContext
- SendStartSessionAck(connection_id,
- generated_session_id,
+ SendStartSessionAck(context.connection_id_,
+ context.new_session_id_,
packet->protocol_version(),
- hash_id,
+ context.hash_id_,
packet->service_type(),
PROTECTION_ON,
*fullVersion,
- start_session_ack_params);
+ *start_session_ack_params);
} else {
- // Need a copy because fullVersion will be deleted
- ProtocolPacket::ProtocolVersion fullVersionCopy(*fullVersion);
- security_manager_->AddListener(new StartSessionHandler(
- connection_key,
- this,
- session_observer_,
- connection_id,
- generated_session_id,
- packet->protocol_version(),
- hash_id,
- service_type,
- get_settings().force_protected_service(),
- fullVersionCopy,
- bson_object_to_bytes(&start_session_ack_params)));
+ security_manager_->AddListener(new HandshakeHandler(*handler));
if (!ssl_context->IsHandshakePending()) {
// Start handshake process
security_manager_->StartHandshake(connection_key);
}
}
- delete fullVersion;
- bson_object_deinitialize(&start_session_ack_params);
LOG4CXX_DEBUG(logger_,
"Protection establishing for connection "
<< connection_key << " is in progress");
@@ -1548,23 +1543,21 @@ void ProtocolHandlerImpl::NotifySessionStartedResult(
}
#endif // ENABLE_SECURITY
if (rejected_params.empty()) {
- SendStartSessionAck(connection_id,
- generated_session_id,
+ SendStartSessionAck(context.connection_id_,
+ context.new_session_id_,
packet->protocol_version(),
- hash_id,
+ context.hash_id_,
packet->service_type(),
PROTECTION_OFF,
*fullVersion,
- start_session_ack_params);
+ *start_session_ack_params);
} else {
- SendStartSessionNAck(connection_id,
+ SendStartSessionNAck(context.connection_id_,
packet->session_id(),
protocol_version,
packet->service_type(),
rejected_params);
}
- delete fullVersion;
- bson_object_deinitialize(&start_session_ack_params);
}
RESULT_CODE ProtocolHandlerImpl::HandleControlMessageHeartBeat(