Merge pull request #61 from f1xpl/bugfix/58

bugfix/58
This commit is contained in:
Michal Szwaj 2018-04-06 00:57:59 +02:00 committed by GitHub
commit 85946e8025
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 256 additions and 14 deletions

View File

@ -26,6 +26,7 @@
#include <f1x/openauto/autoapp/Configuration/IConfiguration.hpp> #include <f1x/openauto/autoapp/Configuration/IConfiguration.hpp>
#include <f1x/openauto/autoapp/Projection/IAndroidAutoEntity.hpp> #include <f1x/openauto/autoapp/Projection/IAndroidAutoEntity.hpp>
#include <f1x/openauto/autoapp/Projection/IServiceFactory.hpp> #include <f1x/openauto/autoapp/Projection/IServiceFactory.hpp>
#include <f1x/openauto/autoapp/Projection/IPinger.hpp>
namespace f1x namespace f1x
{ {
@ -43,7 +44,8 @@ public:
aasdk::messenger::ICryptor::Pointer cryptor, aasdk::messenger::ICryptor::Pointer cryptor,
aasdk::transport::ITransport::Pointer transport, aasdk::transport::ITransport::Pointer transport,
configuration::IConfiguration::Pointer configuration, configuration::IConfiguration::Pointer configuration,
IServiceFactory& serviceFactory); IServiceFactory& serviceFactory,
IPinger::Pointer pinger);
~AndroidAutoEntity() override; ~AndroidAutoEntity() override;
void start(IAndroidAutoEntityEventHandler& eventHandler) override; void start(IAndroidAutoEntityEventHandler& eventHandler) override;
@ -55,17 +57,20 @@ public:
void onShutdownRequest(const aasdk::proto::messages::ShutdownRequest& request) override; void onShutdownRequest(const aasdk::proto::messages::ShutdownRequest& request) override;
void onShutdownResponse(const aasdk::proto::messages::ShutdownResponse& response) override; void onShutdownResponse(const aasdk::proto::messages::ShutdownResponse& response) override;
void onNavigationFocusRequest(const aasdk::proto::messages::NavigationFocusRequest& request) override; void onNavigationFocusRequest(const aasdk::proto::messages::NavigationFocusRequest& request) override;
void onPingResponse(const aasdk::proto::messages::PingResponse& response) override;
void onChannelError(const aasdk::error::Error& e) override; void onChannelError(const aasdk::error::Error& e) override;
private: private:
using std::enable_shared_from_this<AndroidAutoEntity>::shared_from_this; using std::enable_shared_from_this<AndroidAutoEntity>::shared_from_this;
void triggerQuit(); void triggerQuit();
void ping();
boost::asio::io_service::strand strand_; boost::asio::io_service::strand strand_;
aasdk::messenger::ICryptor::Pointer cryptor_; aasdk::messenger::ICryptor::Pointer cryptor_;
aasdk::transport::ITransport::Pointer transport_; aasdk::transport::ITransport::Pointer transport_;
configuration::IConfiguration::Pointer configuration_; configuration::IConfiguration::Pointer configuration_;
IServiceFactory& serviceFactory_; IServiceFactory& serviceFactory_;
IPinger::Pointer pinger_;
aasdk::messenger::IMessenger::Pointer messenger_; aasdk::messenger::IMessenger::Pointer messenger_;
aasdk::channel::control::ControlServiceChannel::Pointer controlServiceChannel_; aasdk::channel::control::ControlServiceChannel::Pointer controlServiceChannel_;
ServiceList serviceList_; ServiceList serviceList_;

View File

@ -0,0 +1,47 @@
/*
* This file is part of openauto project.
* Copyright (C) 2018 f1x.studio (Michal Szwaj)
*
* openauto is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* openauto is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with openauto. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <f1x/aasdk/IO/Promise.hpp>
namespace f1x
{
namespace openauto
{
namespace autoapp
{
namespace projection
{
class IPinger
{
public:
typedef std::shared_ptr<IPinger> Pointer;
typedef aasdk::io::Promise<void> Promise;
virtual ~IPinger() = default;
virtual void ping(Promise::Pointer promise) = 0;
virtual void pong() = 0;
virtual void cancel() = 0;
};
}
}
}
}

View File

@ -0,0 +1,58 @@
/*
* This file is part of openauto project.
* Copyright (C) 2018 f1x.studio (Michal Szwaj)
*
* openauto is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* openauto is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with openauto. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <f1x/openauto/autoapp/Projection/IPinger.hpp>
namespace f1x
{
namespace openauto
{
namespace autoapp
{
namespace projection
{
class Pinger: public IPinger, public std::enable_shared_from_this<Pinger>
{
public:
Pinger(boost::asio::io_service& ioService, time_t duration);
void ping(Promise::Pointer promise) override;
void pong() override;
void cancel() override;
private:
using std::enable_shared_from_this<Pinger>::shared_from_this;
void onTimerExceeded(const boost::system::error_code& error);
boost::asio::io_service::strand strand_;
boost::asio::deadline_timer timer_;
time_t duration_;
bool cancelled_;
Promise::Pointer promise_;
int64_t pingsCount_;
int64_t pongsCount_;
};
}
}
}
}

View File

@ -165,8 +165,8 @@ void App::onUSBHubError(const aasdk::error::Error& error)
{ {
OPENAUTO_LOG(error) << "[App] usb hub error: " << error.what(); OPENAUTO_LOG(error) << "[App] usb hub error: " << error.what();
if(error.getCode() != aasdk::error::ErrorCode::OPERATION_ABORTED && if(error != aasdk::error::ErrorCode::OPERATION_ABORTED &&
error.getCode() != aasdk::error::ErrorCode::OPERATION_IN_PROGRESS) error != aasdk::error::ErrorCode::OPERATION_IN_PROGRESS)
{ {
this->waitForDevice(); this->waitForDevice();
} }

View File

@ -35,12 +35,14 @@ AndroidAutoEntity::AndroidAutoEntity(boost::asio::io_service& ioService,
aasdk::messenger::ICryptor::Pointer cryptor, aasdk::messenger::ICryptor::Pointer cryptor,
aasdk::transport::ITransport::Pointer transport, aasdk::transport::ITransport::Pointer transport,
configuration::IConfiguration::Pointer configuration, configuration::IConfiguration::Pointer configuration,
IServiceFactory& serviceFactory) IServiceFactory& serviceFactory,
IPinger::Pointer pinger)
: strand_(ioService) : strand_(ioService)
, cryptor_(std::move(cryptor)) , cryptor_(std::move(cryptor))
, transport_(std::move(transport)) , transport_(std::move(transport))
, configuration_(std::move(configuration)) , configuration_(std::move(configuration))
, serviceFactory_(serviceFactory) , serviceFactory_(serviceFactory)
, pinger_(std::move(pinger))
, messenger_(std::make_shared<aasdk::messenger::Messenger>(ioService, , messenger_(std::make_shared<aasdk::messenger::Messenger>(ioService,
std::make_shared<aasdk::messenger::MessageInStream>(ioService, transport_, cryptor_), std::make_shared<aasdk::messenger::MessageInStream>(ioService, transport_, cryptor_),
std::make_shared<aasdk::messenger::MessageOutStream>(ioService, transport_, cryptor_))) std::make_shared<aasdk::messenger::MessageOutStream>(ioService, transport_, cryptor_)))
@ -58,17 +60,18 @@ void AndroidAutoEntity::start(IAndroidAutoEntityEventHandler& eventHandler)
{ {
strand_.dispatch([this, self = this->shared_from_this(), eventHandler = &eventHandler]() { strand_.dispatch([this, self = this->shared_from_this(), eventHandler = &eventHandler]() {
OPENAUTO_LOG(info) << "[AndroidAutoEntity] start."; OPENAUTO_LOG(info) << "[AndroidAutoEntity] start.";
eventHandler_ = eventHandler;
cryptor_->init(); cryptor_->init();
serviceList_ = serviceFactory_.create(messenger_); serviceList_ = serviceFactory_.create(messenger_);
std::for_each(serviceList_.begin(), serviceList_.end(), std::bind(&IService::start, std::placeholders::_1)); std::for_each(serviceList_.begin(), serviceList_.end(), std::bind(&IService::start, std::placeholders::_1));
this->ping();
controlServiceChannel_->receive(this->shared_from_this());
auto versionRequestPromise = aasdk::channel::SendPromise::defer(strand_); auto versionRequestPromise = aasdk::channel::SendPromise::defer(strand_);
versionRequestPromise->then([]() {}, std::bind(&AndroidAutoEntity::onChannelError, this->shared_from_this(), std::placeholders::_1)); versionRequestPromise->then([]() {}, std::bind(&AndroidAutoEntity::onChannelError, this->shared_from_this(), std::placeholders::_1));
controlServiceChannel_->sendVersionRequest(std::move(versionRequestPromise)); controlServiceChannel_->sendVersionRequest(std::move(versionRequestPromise));
eventHandler_ = eventHandler;
controlServiceChannel_->receive(this->shared_from_this());
}); });
} }
@ -77,12 +80,12 @@ void AndroidAutoEntity::stop()
strand_.dispatch([this, self = this->shared_from_this()]() { strand_.dispatch([this, self = this->shared_from_this()]() {
OPENAUTO_LOG(info) << "[AndroidAutoEntity] stop."; OPENAUTO_LOG(info) << "[AndroidAutoEntity] stop.";
eventHandler_ = nullptr;
pinger_->cancel();
std::for_each(serviceList_.begin(), serviceList_.end(), std::bind(&IService::stop, std::placeholders::_1)); std::for_each(serviceList_.begin(), serviceList_.end(), std::bind(&IService::stop, std::placeholders::_1));
messenger_->stop(); messenger_->stop();
cryptor_->deinit(); cryptor_->deinit();
transport_->stop(); transport_->stop();
eventHandler_ = nullptr;
}); });
} }
@ -205,13 +208,10 @@ void AndroidAutoEntity::onShutdownRequest(const aasdk::proto::messages::Shutdown
aasdk::proto::messages::ShutdownResponse response; aasdk::proto::messages::ShutdownResponse response;
auto promise = aasdk::channel::SendPromise::defer(strand_); auto promise = aasdk::channel::SendPromise::defer(strand_);
promise->then([this, self = this->shared_from_this()]() { promise->then(std::bind(&AndroidAutoEntity::triggerQuit, this->shared_from_this()),
this->triggerQuit(); std::bind(&AndroidAutoEntity::onChannelError, this->shared_from_this(), std::placeholders::_1));
},
std::bind(&AndroidAutoEntity::onChannelError, this->shared_from_this(), std::placeholders::_1));
controlServiceChannel_->sendShutdownResponse(response, std::move(promise)); controlServiceChannel_->sendShutdownResponse(response, std::move(promise));
controlServiceChannel_->receive(this->shared_from_this());
} }
void AndroidAutoEntity::onShutdownResponse(const aasdk::proto::messages::ShutdownResponse&) void AndroidAutoEntity::onShutdownResponse(const aasdk::proto::messages::ShutdownResponse&)
@ -233,6 +233,12 @@ void AndroidAutoEntity::onNavigationFocusRequest(const aasdk::proto::messages::N
controlServiceChannel_->receive(this->shared_from_this()); controlServiceChannel_->receive(this->shared_from_this());
} }
void AndroidAutoEntity::onPingResponse(const aasdk::proto::messages::PingResponse&)
{
pinger_->pong();
controlServiceChannel_->receive(this->shared_from_this());
}
void AndroidAutoEntity::onChannelError(const aasdk::error::Error& e) void AndroidAutoEntity::onChannelError(const aasdk::error::Error& e)
{ {
OPENAUTO_LOG(error) << "[AndroidAutoEntity] channel error: " << e.what(); OPENAUTO_LOG(error) << "[AndroidAutoEntity] channel error: " << e.what();
@ -247,6 +253,29 @@ void AndroidAutoEntity::triggerQuit()
} }
} }
void AndroidAutoEntity::ping()
{
auto promise = IPinger::Promise::defer(strand_);
promise->then([this, self = this->shared_from_this()]() {
auto promise = aasdk::channel::SendPromise::defer(strand_);
promise->then([]() {}, std::bind(&AndroidAutoEntity::onChannelError, this->shared_from_this(), std::placeholders::_1));
aasdk::proto::messages::PingRequest request;
controlServiceChannel_->sendPingRequest(request, std::move(promise));
this->ping();
},
[this, self = this->shared_from_this()](auto error) {
if(error != aasdk::error::ErrorCode::OPERATION_ABORTED &&
error != aasdk::error::ErrorCode::OPERATION_IN_PROGRESS)
{
OPENAUTO_LOG(error) << "[AndroidAutoEntity] ping timer exceeded.";
this->triggerQuit();
}
});
pinger_->ping(std::move(promise));
}
} }
} }
} }

View File

@ -23,6 +23,7 @@
#include <f1x/aasdk/Messenger/Cryptor.hpp> #include <f1x/aasdk/Messenger/Cryptor.hpp>
#include <f1x/openauto/autoapp/Projection/AndroidAutoEntityFactory.hpp> #include <f1x/openauto/autoapp/Projection/AndroidAutoEntityFactory.hpp>
#include <f1x/openauto/autoapp/Projection/AndroidAutoEntity.hpp> #include <f1x/openauto/autoapp/Projection/AndroidAutoEntity.hpp>
#include <f1x/openauto/autoapp/Projection/Pinger.hpp>
namespace f1x namespace f1x
{ {
@ -59,8 +60,9 @@ IAndroidAutoEntity::Pointer AndroidAutoEntityFactory::create(aasdk::transport::I
{ {
auto sslWrapper(std::make_shared<aasdk::transport::SSLWrapper>()); auto sslWrapper(std::make_shared<aasdk::transport::SSLWrapper>());
auto cryptor(std::make_shared<aasdk::messenger::Cryptor>(std::move(sslWrapper))); auto cryptor(std::make_shared<aasdk::messenger::Cryptor>(std::move(sslWrapper)));
auto pinger(std::make_shared<Pinger>(ioService_, 5000));
return std::make_shared<AndroidAutoEntity>(ioService_, std::move(cryptor), std::move(transport), configuration_, serviceFactory_); return std::make_shared<AndroidAutoEntity>(ioService_, std::move(cryptor), std::move(transport), configuration_, serviceFactory_, std::move(pinger));
} }
} }

View File

@ -0,0 +1,101 @@
/*
* This file is part of openauto project.
* Copyright (C) 2018 f1x.studio (Michal Szwaj)
*
* openauto is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* openauto is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with openauto. If not, see <http://www.gnu.org/licenses/>.
*/
#include <f1x/openauto/autoapp/Projection/Pinger.hpp>
namespace f1x
{
namespace openauto
{
namespace autoapp
{
namespace projection
{
Pinger::Pinger(boost::asio::io_service& ioService, time_t duration)
: strand_(ioService)
, timer_(ioService)
, duration_(duration)
, cancelled_(false)
, pingsCount_(0)
, pongsCount_(0)
{
}
void Pinger::ping(Promise::Pointer promise)
{
strand_.dispatch([this, self = this->shared_from_this(), promise = std::move(promise)]() mutable {
cancelled_ = false;
if(promise_ != nullptr)
{
promise_->reject(aasdk::error::Error(aasdk::error::ErrorCode::OPERATION_IN_PROGRESS));
}
else
{
++pingsCount_;
promise_ = std::move(promise);
timer_.expires_from_now(boost::posix_time::milliseconds(duration_));
timer_.async_wait(strand_.wrap(std::bind(&Pinger::onTimerExceeded, this->shared_from_this(), std::placeholders::_1)));
}
});
}
void Pinger::pong()
{
strand_.dispatch([this, self = this->shared_from_this()]() {
++pongsCount_;
});
}
void Pinger::onTimerExceeded(const boost::system::error_code& error)
{
if(promise_ == nullptr)
{
return;
}
else if(error == boost::asio::error::operation_aborted || cancelled_)
{
promise_->reject(aasdk::error::Error(aasdk::error::ErrorCode::OPERATION_ABORTED));
}
else if(pingsCount_ - pongsCount_ > 1)
{
promise_->reject(aasdk::error::Error());
}
else
{
promise_->resolve();
}
promise_.reset();
}
void Pinger::cancel()
{
strand_.dispatch([this, self = this->shared_from_this()]() {
cancelled_ = true;
timer_.cancel();
});
}
}
}
}
}