Skip to content

Commit

Permalink
Standarize behavior of pinger cancel
Browse files Browse the repository at this point in the history
  • Loading branch information
f1xpl committed Apr 5, 2018
1 parent a0b2418 commit 28fc6a3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 27 deletions.
2 changes: 1 addition & 1 deletion include/f1x/openauto/autoapp/Projection/IPinger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class IPinger
{
public:
typedef std::shared_ptr<IPinger> Pointer;
typedef aasdk::io::Promise<void, void> Promise;
typedef aasdk::io::Promise<void> Promise;

virtual ~IPinger() = default;
virtual void ping(Promise::Pointer promise) = 0;
Expand Down
1 change: 1 addition & 0 deletions include/f1x/openauto/autoapp/Projection/Pinger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Pinger: public IPinger, public std::enable_shared_from_this<Pinger>
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_;
Expand Down
4 changes: 2 additions & 2 deletions src/autoapp/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ void App::onUSBHubError(const aasdk::error::Error& error)
{
OPENAUTO_LOG(error) << "[App] usb hub error: " << error.what();

if(error.getCode() != aasdk::error::ErrorCode::OPERATION_ABORTED &&
error.getCode() != aasdk::error::ErrorCode::OPERATION_IN_PROGRESS)
if(error != aasdk::error::ErrorCode::OPERATION_ABORTED &&
error != aasdk::error::ErrorCode::OPERATION_IN_PROGRESS)
{
this->waitForDevice();
}
Expand Down
19 changes: 11 additions & 8 deletions src/autoapp/Projection/AndroidAutoEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,18 @@ void AndroidAutoEntity::start(IAndroidAutoEntityEventHandler& eventHandler)
{
strand_.dispatch([this, self = this->shared_from_this(), eventHandler = &eventHandler]() {
OPENAUTO_LOG(info) << "[AndroidAutoEntity] start.";
eventHandler_ = eventHandler;

cryptor_->init();

serviceList_ = serviceFactory_.create(messenger_);
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_);
versionRequestPromise->then([]() {}, std::bind(&AndroidAutoEntity::onChannelError, this->shared_from_this(), std::placeholders::_1));
controlServiceChannel_->sendVersionRequest(std::move(versionRequestPromise));
eventHandler_ = eventHandler;
controlServiceChannel_->receive(this->shared_from_this());
});
}

Expand All @@ -79,12 +80,12 @@ void AndroidAutoEntity::stop()
strand_.dispatch([this, self = this->shared_from_this()]() {
OPENAUTO_LOG(info) << "[AndroidAutoEntity] stop.";

eventHandler_ = nullptr;
pinger_->cancel();
std::for_each(serviceList_.begin(), serviceList_.end(), std::bind(&IService::stop, std::placeholders::_1));
messenger_->stop();
cryptor_->deinit();
transport_->stop();
eventHandler_ = nullptr;
});
}

Expand Down Expand Up @@ -145,7 +146,6 @@ void AndroidAutoEntity::onHandshake(const aasdk::common::DataConstBuffer& payloa
auto authCompletePromise = aasdk::channel::SendPromise::defer(strand_);
authCompletePromise->then([]() {}, std::bind(&AndroidAutoEntity::onChannelError, this->shared_from_this(), std::placeholders::_1));
controlServiceChannel_->sendAuthComplete(authCompleteIndication, std::move(authCompletePromise));
this->ping();
}

controlServiceChannel_->receive(this->shared_from_this());
Expand Down Expand Up @@ -212,7 +212,6 @@ void AndroidAutoEntity::onShutdownRequest(const aasdk::proto::messages::Shutdown
std::bind(&AndroidAutoEntity::onChannelError, this->shared_from_this(), std::placeholders::_1));

controlServiceChannel_->sendShutdownResponse(response, std::move(promise));
controlServiceChannel_->receive(this->shared_from_this());
}

void AndroidAutoEntity::onShutdownResponse(const aasdk::proto::messages::ShutdownResponse&)
Expand Down Expand Up @@ -265,9 +264,13 @@ void AndroidAutoEntity::ping()
controlServiceChannel_->sendPingRequest(request, std::move(promise));
this->ping();
},
[this, self = this->shared_from_this()]() {
OPENAUTO_LOG(error) << "[AndroidAutoEntity] ping timer exceeded.";
this->triggerQuit();
[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));
Expand Down
47 changes: 31 additions & 16 deletions src/autoapp/Projection/Pinger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Pinger::Pinger(boost::asio::io_service& ioService, time_t duration)
: strand_(ioService)
, timer_(ioService)
, duration_(duration)
, cancelled_(false)
, pingsCount_(0)
, pongsCount_(0)
{
Expand All @@ -40,11 +41,20 @@ Pinger::Pinger(boost::asio::io_service& ioService, time_t duration)
void Pinger::ping(Promise::Pointer promise)
{
strand_.dispatch([this, self = this->shared_from_this(), promise = std::move(promise)]() mutable {
++pingsCount_;
cancelled_ = false;

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)));
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)));
}
});
}

Expand All @@ -57,25 +67,30 @@ void Pinger::pong()

void Pinger::onTimerExceeded(const boost::system::error_code& error)
{
if(!error && promise_ != nullptr)
if(promise_ == nullptr)
{
if(std::abs(pingsCount_ - pongsCount_) > 1)
{
promise_->reject();
}
else
{
promise_->resolve();
}

promise_.reset();
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()]() {
promise_.reset();
cancelled_ = true;
timer_.cancel();
});
}
Expand Down

0 comments on commit 28fc6a3

Please sign in to comment.