Skip to content

Commit

Permalink
Merge pull request #1013 from GamovCoder/NetworkChangeDetector
Browse files Browse the repository at this point in the history
KAA-1373: Implement successful connection establishment detector
  • Loading branch information
rasendubi committed Sep 5, 2016
2 parents f98f290 + a09034a commit 04b43b0
Show file tree
Hide file tree
Showing 18 changed files with 175 additions and 123 deletions.
3 changes: 1 addition & 2 deletions client/client-multi/client-cpp/impl/Kaa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
#include "kaa/KaaClient.hpp"
#include "kaa/common/exception/KaaException.hpp"
#include "kaa/KaaClientPlatformContext.hpp"
#include "kaa/DummyKaaClientStateListener.hpp"

namespace kaa {

Botan::LibraryInitializer Kaa::botanInit_("thread_safe=true");

std::shared_ptr<IKaaClient> Kaa::newClient(IKaaClientPlatformContextPtr context
, IKaaClientStateListenerPtr listener)
, KaaClientStateListenerPtr listener)
{
if (!context) {
throw KaaException("Kaa client platform context is null");
Expand Down
31 changes: 13 additions & 18 deletions client/client-multi/client-cpp/impl/KaaClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,18 @@

#include "kaa/logging/Log.hpp"
#include "kaa/context/SimpleExecutorContext.hpp"
#include "kaa/DummyKaaClientStateListener.hpp"
#include "kaa/KaaClientProperties.hpp"
#include "kaa/logging/DefaultLogger.hpp"

namespace kaa {

KaaClient::KaaClient(IKaaClientPlatformContextPtr context, IKaaClientStateListenerPtr listener)
: logger_(new DefaultLogger(context->getProperties().getClientId(), context->getProperties().getLogFileName())),
context_(context->getProperties(), *logger_, context->getExecutorContext()),
KaaClient::KaaClient(IKaaClientPlatformContextPtr platformContext, KaaClientStateListenerPtr listener)
: logger_(new DefaultLogger(platformContext->getProperties().getClientId(), platformContext->getProperties().getLogFileName())),
context_(platformContext->getProperties(), *logger_, platformContext->getExecutorContext(), nullptr,
(listener == nullptr) ? std::make_shared<KaaClientStateListener>() : listener),
status_(new ClientStatus(context_)),
stateListener_(listener),
platformContext_(context)
platformContext_(platformContext)
{
if (!stateListener_) {
stateListener_ = std::make_shared<DummyKaaClientStateListener>();
}

init();
}

Expand Down Expand Up @@ -118,12 +113,12 @@ void KaaClient::start()
configurationManager_->init();
#endif
bootstrapManager_->receiveOperationsServerList();
stateListener_->onStarted();
context_.getClientStateListener().onStarted();

KAA_LOG_INFO("Kaa client started");
} catch (std::exception& e) {
KAA_LOG_ERROR(boost::format("Caught exception on start: %s") % e.what());
stateListener_->onStartFailure(KaaException(e));
context_.getClientStateListener().onStartFailure(KaaException(e));
}
});

Expand All @@ -147,12 +142,12 @@ void KaaClient::stop()
try {
channelManager_->shutdown();
status_->save();
stateListener_->onStopped();
context_.getClientStateListener().onStopped();

KAA_LOG_INFO("Kaa client stopped");
} catch (std::exception& e) {
KAA_LOG_ERROR(boost::format("Caught exception on stop: %s") % e.what());
stateListener_->onStopFailure(KaaException(e));
context_.getClientStateListener().onStopFailure(KaaException(e));
}
});

Expand All @@ -171,12 +166,12 @@ void KaaClient::pause()
try {
status_->save();
channelManager_->pause();
stateListener_->onPaused();
context_.getClientStateListener().onPaused();

KAA_LOG_INFO("Kaa client paused");
} catch (std::exception& e) {
KAA_LOG_ERROR(boost::format("Caught exception on pause: %s") % e.what());
stateListener_->onPauseFailure(KaaException(e));
context_.getClientStateListener().onPauseFailure(KaaException(e));
}
});

Expand All @@ -193,12 +188,12 @@ void KaaClient::resume()
{
try {
channelManager_->resume();
stateListener_->onResumed();
context_.getClientStateListener().onResumed();

KAA_LOG_INFO("Kaa client resumed");
} catch (std::exception& e) {
KAA_LOG_ERROR(boost::format("Caught exception on resume: %s") % e.what());
stateListener_->onResumeFailure(KaaException(e));
context_.getClientStateListener().onResumeFailure(KaaException(e));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ void KaaChannelManager::setFailoverStrategy(IFailoverStrategyPtr strategy) {
}
}

void KaaChannelManager::onConnected(const EndpointConnectionInfo& connection)
{
context_.getClientStateListener().onConnectionEstablished(connection);
}

void KaaChannelManager::onServerFailed(ITransportConnectionInfoPtr connectionInfo, KaaFailoverReason reason) {

if (isShutdown_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ void AbstractHttpChannel::processTypes(const std::map<TransportType, ChannelDire

try {
// Sending http request
auto response = httpClient_.sendRequest(*postRequest);
EndpointConnectionInfo connection("", "", getServerType());
auto response = httpClient_.sendRequest(*postRequest, &connection);
channelManager_.onConnected(connection);

KAA_MUTEX_LOCKING("channelGuard_");
KAA_MUTEX_UNIQUE_DECLARE(lockInternal, channelGuard_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ void DefaultOperationTcpChannel::openConnection()
return;
}

channelManager_.onConnected({sock_->local_endpoint().address().to_string(), ep.address().to_string(), getServerType()});

KAA_MUTEX_LOCKING("channelGuard_");
KAA_LOCK(channelGuard_);
KAA_MUTEX_LOCKED("channelGuard_");
Expand Down
7 changes: 6 additions & 1 deletion client/client-multi/client-cpp/impl/http/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void HttpClient::checkError(const boost::system::error_code& code)
throw TransportException(code);
}

std::shared_ptr<IHttpResponse> HttpClient::sendRequest(const IHttpRequest& request)
std::shared_ptr<IHttpResponse> HttpClient::sendRequest(const IHttpRequest& request, EndpointConnectionInfo* connection)
{
KAA_MUTEX_LOCKING("httpClientGuard_");
KAA_MUTEX_UNIQUE_DECLARE(httpClientGuardLock, httpClientGuard_);
Expand All @@ -66,6 +66,11 @@ std::shared_ptr<IHttpResponse> HttpClient::sendRequest(const IHttpRequest& reque
sock_.connect(ep, errorCode);
checkError(errorCode);

if (connection != nullptr) {
connection->endpointIp_ = sock_.local_endpoint().address().to_string();
connection->serverIp_ = ep.address().to_string();
}

const auto& data = request.getRequestData();
boost::asio::write(sock_, boost::asio::buffer(data.data(), data.size()), errorCode);
checkError(errorCode);
Expand Down
41 changes: 0 additions & 41 deletions client/client-multi/client-cpp/kaa/DummyKaaClientStateListener.hpp

This file was deleted.

39 changes: 39 additions & 0 deletions client/client-multi/client-cpp/kaa/EndpointConnectionInfo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef KAA_ENDPOINT_CONNECTION_INFO_HPP_
#define KAA_ENDPOINT_CONNECTION_INFO_HPP_

#include <kaa/channel/ServerType.hpp>

namespace kaa {
/**
* The structure represents connection information in readable format.
*/
struct EndpointConnectionInfo {
std::string endpointIp_; /**< The ip address of the endpoint. */
std::string serverIp_; /**< The ip address of the server */
ServerType serverType_; /**< The type of server, see @c ServerType */

EndpointConnectionInfo(const std::string& endpointIp, const std::string& serverIp, const ServerType serverType)
: endpointIp_(endpointIp),
serverIp_(serverIp),
serverType_(serverType) {}
};

};

#endif /* KAA_ENDPOINT_CONNECTION_INFO_HPP_ */
12 changes: 6 additions & 6 deletions client/client-multi/client-cpp/kaa/IKaaClientContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#ifndef IKAACLIENTCONTEXT
#define IKAACLIENTCONTEXT

#include <memory>
#include <kaa/IKaaClientStateStorage.hpp>
#include <kaa/KaaClientStateListener.hpp>

namespace kaa {

Expand All @@ -28,15 +28,15 @@ class IExecutorContext;

class IKaaClientContext {
public:
virtual KaaClientProperties &getProperties() = 0;
virtual ILogger &getLogger() = 0;
virtual IKaaClientStateStorage &getStatus() = 0;
virtual IExecutorContext &getExecutorContext() = 0;
virtual KaaClientProperties &getProperties() = 0;
virtual ILogger &getLogger() = 0;
virtual IKaaClientStateStorage &getStatus() = 0;
virtual IExecutorContext &getExecutorContext() = 0;
virtual KaaClientStateListener &getClientStateListener() = 0;

virtual ~IKaaClientContext() = default;
};

}

#endif // IKAACLIENTCONTEXT

9 changes: 5 additions & 4 deletions client/client-multi/client-cpp/kaa/Kaa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
#include <botan/init.h>

#include "kaa/IKaaClient.hpp"
#include "kaa/IKaaClientStateListener.hpp"
#include "kaa/KaaClientStateListener.hpp"
#include "kaa/IKaaClientPlatformContext.hpp"
#include "kaa/KaaClientPlatformContext.hpp"

namespace kaa {

/**
* @brief Creates a new Kaa client based on @link IKaaClientPlatformContext @endlink and optional
* @link IKaaClientStateListener @endlink.
* @link KaaClientStateListener @endlink.
*
*/
class Kaa
Expand Down Expand Up @@ -60,8 +60,9 @@ class Kaa
*
* @return New instance of Kaa client.
*/
static std::shared_ptr<IKaaClient> newClient(IKaaClientPlatformContextPtr context = std::make_shared<KaaClientPlatformContext>()
, IKaaClientStateListenerPtr listener = IKaaClientStateListenerPtr());
static std::shared_ptr<IKaaClient> newClient(
IKaaClientPlatformContextPtr context = std::make_shared<KaaClientPlatformContext>(),
KaaClientStateListenerPtr listener = std::make_shared<KaaClientStateListener>());

private:
static Botan::LibraryInitializer botanInit_;
Expand Down
6 changes: 3 additions & 3 deletions client/client-multi/client-cpp/kaa/KaaClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "kaa/configuration/manager/ConfigurationManager.hpp"
#include "kaa/log/LogCollector.hpp"
#include "kaa/context/IExecutorContext.hpp"
#include "kaa/IKaaClientStateListener.hpp"
#include "kaa/KaaClientStateListener.hpp"
#include "kaa/IKaaClientPlatformContext.hpp"
#include "kaa/KaaClientProperties.hpp"
#include "kaa/KaaClientContext.hpp"
Expand All @@ -47,7 +47,7 @@ namespace kaa {
class KaaClient : public IKaaClient,
public std::enable_shared_from_this<KaaClient> {
public:
KaaClient(IKaaClientPlatformContextPtr context, IKaaClientStateListenerPtr listener);
KaaClient(IKaaClientPlatformContextPtr platformContext, KaaClientStateListenerPtr listener);

virtual void start();
virtual void stop();
Expand Down Expand Up @@ -104,6 +104,7 @@ class KaaClient : public IKaaClient,
virtual IKaaDataMultiplexer& getBootstrapMultiplexer();
virtual IKaaDataDemultiplexer& getBootstrapDemultiplexer();
virtual IKaaClientContext& getKaaClientContext();

private:
void init();

Expand Down Expand Up @@ -165,7 +166,6 @@ class KaaClient : public IKaaClient,
std::unique_ptr<LogCollector> logCollector_;
#endif

IKaaClientStateListenerPtr stateListener_;
IKaaClientPlatformContextPtr platformContext_;
};

Expand Down
32 changes: 20 additions & 12 deletions client/client-multi/client-cpp/kaa/KaaClientContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,35 @@
#include <kaa/IKaaClientStateStorage.hpp>
#include <kaa/context/IExecutorContext.hpp>


namespace kaa {

class KaaClientContext : public IKaaClientContext
{
public:
KaaClientContext(KaaClientProperties &properties, ILogger &logger,
IExecutorContext &executorContext, IKaaClientStateStoragePtr state = nullptr)
: properties_(properties), logger_(logger), executorContext_(executorContext), state_(state) {}

virtual KaaClientProperties &getProperties() { return properties_; }
virtual ILogger &getLogger() { return logger_; }
virtual IKaaClientStateStorage &getStatus() { return *state_; }
KaaClientContext(KaaClientProperties &properties,
ILogger &logger,
IExecutorContext &executorContext,
IKaaClientStateStoragePtr state = nullptr,
KaaClientStateListenerPtr stateListener = std::make_shared<KaaClientStateListener>())
: properties_(properties),
logger_(logger),
executorContext_(executorContext),
state_(state),
stateListener_(stateListener) {}

virtual KaaClientProperties &getProperties() { return properties_; }
virtual ILogger &getLogger() { return logger_; }
virtual IKaaClientStateStorage &getStatus() { return *state_; }
virtual IExecutorContext &getExecutorContext() { return executorContext_; }
virtual KaaClientStateListener &getClientStateListener() { return *stateListener_; }
void setStatus(IKaaClientStateStoragePtr status) { state_ = status; }

private:
KaaClientProperties &properties_;
ILogger &logger_;
IExecutorContext &executorContext_;
IKaaClientStateStoragePtr state_;
KaaClientProperties &properties_;
ILogger &logger_;
IExecutorContext &executorContext_;
IKaaClientStateStoragePtr state_;
KaaClientStateListenerPtr stateListener_;
};

}
Expand Down

0 comments on commit 04b43b0

Please sign in to comment.