-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
234 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, void> Promise; | ||
|
||
virtual ~IPinger() = default; | ||
virtual void ping(Promise::Pointer promise) = 0; | ||
virtual void pong() = 0; | ||
virtual void cancel() = 0; | ||
}; | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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, 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_; | ||
Promise::Pointer promise_; | ||
int64_t pingsCount_; | ||
int64_t pongsCount_; | ||
}; | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* 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) | ||
, pingsCount_(0) | ||
, pongsCount_(0) | ||
{ | ||
|
||
} | ||
|
||
void Pinger::ping(Promise::Pointer promise) | ||
{ | ||
strand_.dispatch([this, self = this->shared_from_this(), promise = std::move(promise)]() mutable { | ||
++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(!error && promise_ != nullptr) | ||
{ | ||
if(std::abs(pingsCount_ - pongsCount_) > 1) | ||
{ | ||
promise_->reject(); | ||
} | ||
else | ||
{ | ||
promise_->resolve(); | ||
} | ||
|
||
promise_.reset(); | ||
} | ||
} | ||
|
||
void Pinger::cancel() | ||
{ | ||
strand_.dispatch([this, self = this->shared_from_this()]() { | ||
promise_.reset(); | ||
timer_.cancel(); | ||
}); | ||
} | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters