Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions aether/ae_actions/ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ UpdateStatus Ping::Update() {
}

if (state_.get() == State::kWaitResponse) {
return UpdateStatus::Delay(ToSyncTime(WaitResponse()));
return UpdateStatus::Delay(WaitResponse());
}
if (state_.get() == State::kWaitInterval) {
return UpdateStatus::Delay(ToSyncTime(WaitInterval()));
return UpdateStatus::Delay(WaitInterval());
}

return {};
Expand All @@ -76,7 +76,7 @@ void Ping::SendPing() {
ping_interval_)
.count()));
// save the ping request
auto current_time = SystemTime();
auto current_time = Now();
auto channel_ptr = channel_.Lock();
assert(channel_ptr);
auto expected_ping_time = channel_ptr->ResponseTimeout();
Expand All @@ -100,8 +100,8 @@ void Ping::SendPing() {
state_ = State::kWaitResponse;
}

SystemTimePoint Ping::WaitInterval() {
auto current_time = SystemTime();
TimePoint Ping::WaitInterval() {
auto current_time = Now();
auto const& ping_time = ping_requests_.back().start;
if ((ping_time + ping_interval_) > current_time) {
return ping_time + ping_interval_;
Expand All @@ -110,8 +110,8 @@ SystemTimePoint Ping::WaitInterval() {
return current_time;
}

SystemTimePoint Ping::WaitResponse() {
auto current_time = SystemTime();
TimePoint Ping::WaitResponse() {
auto current_time = Now();

auto const& expected_ping_time = ping_requests_.back().expected_end;
if (expected_ping_time > current_time) {
Expand All @@ -125,7 +125,7 @@ SystemTimePoint Ping::WaitResponse() {
}

void Ping::PingResponse(RequestId request_id) {
auto request_time = std::invoke([&]() -> std::optional<SystemTimePoint> {
auto request_time = std::invoke([&]() -> std::optional<TimePoint> {
for (auto const& [time, _, req_id] : ping_requests_) {
if (req_id == request_id) {
return time;
Expand All @@ -139,7 +139,7 @@ void Ping::PingResponse(RequestId request_id) {
return;
}

auto current_time = SystemTime();
auto current_time = Now();
auto ping_duration =
std::chrono::duration_cast<Duration>(current_time - *request_time);

Expand Down
8 changes: 4 additions & 4 deletions aether/ae_actions/ping.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class Ping : public Action<Ping> {
static constexpr std::uint8_t kMaxStorePingTimes = 10;

struct PingRequest {
SystemTimePoint start;
SystemTimePoint expected_end;
TimePoint start;
TimePoint expected_end;
RequestId request_id;
};

Expand All @@ -69,8 +69,8 @@ class Ping : public Action<Ping> {

private:
void SendPing();
SystemTimePoint WaitInterval();
SystemTimePoint WaitResponse();
TimePoint WaitInterval();
TimePoint WaitResponse();
void PingResponse(RequestId request_id);

PtrView<Channel> channel_;
Expand Down
27 changes: 14 additions & 13 deletions aether/ae_actions/time_sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ class TimeSyncRequest : public Action<TimeSyncRequest> {
AE_TELED_DEBUG("Make time sync request");
ApiPromisePtr<std::uint64_t> promise = api->get_time_utc();
response_sub_ = promise->StatusEvent().Subscribe(
OnResult{[this, request_time{Clock::now()}](auto& p) {
OnResult{[this, request_time{Now()}](auto& p) {
HandleResponse(
std::chrono::milliseconds{
static_cast<std::int64_t>(p.value())},
request_time, Clock::now());
request_time, Now());
// time synced
state_ = State::kResult;
}});
Expand All @@ -163,19 +163,19 @@ class TimeSyncRequest : public Action<TimeSyncRequest> {
client_ptr->cloud_connection(), RequestPolicy::MainServer{});

// use raw time to avoid sync jumps
request_time_ = SystemTime();
request_time_ = Now();
state_ = State::kWaitResponse;
}

UpdateStatus WaitResponse() {
auto current_time = SystemTime();
auto current_time = Now();
auto timeout = request_time_ + kRequestTimeout;
if (current_time > timeout) {
AE_TELED_ERROR("Time sync response timeout");
state_ = State::kFailed;
return {};
}
return UpdateStatus::Delay(ToSyncTime(timeout));
return UpdateStatus::Delay(timeout);
}

static void HandleResponse(std::chrono::milliseconds server_epoch,
Expand All @@ -194,14 +194,15 @@ class TimeSyncRequest : public Action<TimeSyncRequest> {
std::chrono::duration_cast<std::chrono::milliseconds>(diff_time)
.count());
// update diff time
Clock::SyncTimeDiff +=
std::chrono::duration_cast<decltype(Clock::SyncTimeDiff)>(diff_time);
SyncClock::SyncTimeDiff +=
std::chrono::duration_cast<decltype(SyncClock::SyncTimeDiff)>(
diff_time);
AE_TELED_DEBUG("Current time {:%Y-%m-%d %H:%M:%S}", Now());
}

PtrView<Client> client_;
StateMachine<State> state_;
SystemTimePoint request_time_;
TimePoint request_time_;
int tries_{};
Subscription link_state_sub_;
Subscription response_sub_;
Expand All @@ -210,7 +211,7 @@ class TimeSyncRequest : public Action<TimeSyncRequest> {
} // namespace time_sync_internal

// set end of time - this means last_sync_time is not set
SystemTimePoint TimeSyncAction::last_sync_time = SystemTimePoint::max();
TimePoint TimeSyncAction::last_sync_time = TimePoint::max();

TimeSyncAction::TimeSyncAction(ActionContext action_context,
Ptr<Aether> const& aether,
Expand Down Expand Up @@ -273,24 +274,24 @@ void TimeSyncAction::MakeRequest() {
uap->RegisterAction(*time_sync_request_);

// use raw time to avoid sync jumps
last_sync_time = SystemTime();
last_sync_time = Now();
state_ = State::kWaitInterval;
}

UpdateStatus TimeSyncAction::WaitInterval() {
// the end of time!
if (last_sync_time == SystemTimePoint::max()) {
if (last_sync_time == TimePoint::max()) {
state_ = State::kMakeRequest;
return {};
}
auto current_time = SystemTime();
auto current_time = Now();
auto timeout = last_sync_time + sync_interval_;
if (current_time > timeout) {
AE_TELED_INFO("Time sync interval timeout, make new request");
state_ = State::kMakeRequest;
return {};
}
return UpdateStatus::Delay(ToSyncTime(timeout));
return UpdateStatus::Delay(timeout);
}

} // namespace ae
Expand Down
3 changes: 1 addition & 2 deletions aether/ae_actions/time_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
# include "aether/actions/action.h"
# include "aether/actions/action_ptr.h"
# include "aether/types/state_machine.h"
# include "aether/events/event_subscription.h"

namespace ae {
class Aether;
Expand Down Expand Up @@ -60,7 +59,7 @@ class TimeSyncAction : public Action<TimeSyncAction> {
StateMachine<State> state_;
ActionPtr<time_sync_internal::TimeSyncRequest> time_sync_request_;

static RTC_STORAGE_ATTR SystemTimePoint last_sync_time;
static RTC_STORAGE_ATTR TimePoint last_sync_time;
};
} // namespace ae
#endif
Expand Down
28 changes: 12 additions & 16 deletions aether/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ using std::chrono::duration_cast;
using std::chrono::system_clock;

template <typename ChronoClock>
class Clock {
class SyncClock {
public:
static RTC_STORAGE_ATTR system_clock::duration SyncTimeDiff;

Expand All @@ -37,7 +37,7 @@ class Clock {
using period = typename ChronoClock::period;
using duration = typename ChronoClock::duration;

using time_point = std::chrono::time_point<Clock, duration>;
using time_point = std::chrono::time_point<SyncClock, duration>;

static constexpr bool is_steady = ChronoClock::is_steady;

Expand All @@ -52,15 +52,16 @@ class Clock {
* \brief Return sync time
*/
template <typename C, typename D>
static std::enable_if_t<
std::is_same_v<Clock, C> || std::is_same_v<internal_clock, C>, time_point>
static std::enable_if_t<std::is_same_v<SyncClock, C> ||
std::is_same_v<internal_clock, C>,
time_point>
sync_time(std::chrono::time_point<C, D> const& tp) {
return time_point{tp.time_since_epoch() + SyncTimeDiff};
}
};

template <typename ChronoClock>
std::chrono::system_clock::duration Clock<ChronoClock>::SyncTimeDiff =
std::chrono::system_clock::duration SyncClock<ChronoClock>::SyncTimeDiff =
std::chrono::milliseconds{0};
} // namespace ae::clock_internal

Expand All @@ -76,19 +77,14 @@ using std::chrono_literals::operator""ns;
*/
using Duration = std::chrono::duration<std::uint32_t, std::micro>;
using SystemClock = std::chrono::system_clock;
using Clock = clock_internal::Clock<SystemClock>;
using TimePoint = typename Clock::time_point;
using SystemTimePoint = typename Clock::internal_clock::time_point;
using SyncClock = clock_internal::SyncClock<SystemClock>;
using TimePoint = typename SystemClock::time_point;
using SyncTimePoint = typename SyncClock::time_point;

// synchronised time
inline auto Now() { return Clock::now(); }
// current system clock time, without synchorinization
inline auto SystemTime() { return Clock::internal_clock::now(); }
// make non sync time point sync with current time
template <typename C, typename D>
inline auto ToSyncTime(std::chrono::time_point<C, D> tp) {
return Clock::sync_time(tp);
}
inline auto Now() { return SystemClock::now(); }
// synchronised time
inline auto SyncTime() { return SyncClock::now(); }
} // namespace ae

#endif // AETHER_CLOCK_H_
17 changes: 6 additions & 11 deletions aether/meta/arg_at.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static inline constexpr auto ArgAt_v = ArgAt<I, Args...>::value;
# include "tests/inline.h"

namespace test::arg_at_h {
inline void test_VarAt() {
AE_TEST_INLINE(test_VarAt) {
constexpr int x = 42;
constexpr double y = 3.14;
constexpr bool z = true;
Expand All @@ -65,22 +65,22 @@ inline void test_VarAt() {
static_assert(ae::VarAt<2>(x, y, z));

int a = 10;
float b = 3.14f;
float b = 3.14F;
bool c = true;

TEST_ASSERT_EQUAL(10, ae::VarAt<0>(a, b, c));
TEST_ASSERT_EQUAL_FLOAT(3.14f, ae::VarAt<1>(a, b, c));
TEST_ASSERT_EQUAL_FLOAT(3.14F, ae::VarAt<1>(a, b, c));
TEST_ASSERT_EQUAL(true, ae::VarAt<2>(a, b, c));
}

template <auto... Args>
inline void testTemplateArgs() {
TEST_ASSERT_EQUAL(10, (ae::ArgAt_v<0, Args...>));
TEST_ASSERT_EQUAL_FLOAT(3.14f, (ae::ArgAt_v<1, Args...>));
TEST_ASSERT_EQUAL_FLOAT(3.14F, (ae::ArgAt_v<1, Args...>));
TEST_ASSERT_EQUAL(true, (ae::ArgAt_v<2, Args...>));
}

inline void test_ArgAt() {
AE_TEST_INLINE(test_ArgAt) {
constexpr int x = 42;
constexpr double y = 3.14;
constexpr bool z = true;
Expand All @@ -89,15 +89,10 @@ inline void test_ArgAt() {
static_assert(ae::ArgAt_v<1, x, y, z> == 3.14);
static_assert(ae::ArgAt_v<2, x, y, z>);

testTemplateArgs<10, 3.14f, true>();
testTemplateArgs<10, 3.14F, true>();
}

} // namespace test::arg_at_h

AE_TEST_INLINE {
RUN_TEST(test::arg_at_h::test_VarAt);
RUN_TEST(test::arg_at_h::test_ArgAt);
}
#endif

#endif // AETHER_META_ARG_AT_H_
7 changes: 2 additions & 5 deletions aether/meta/as_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ using ae::as_type;
struct Base {};
struct Derived : Base {};

inline void test_AsType() {
AE_TEST_INLINE(test_AsType) {
Derived d;
decltype(auto) b1 = as_type<Base>(d);
static_assert(std::is_same_v<Base&, decltype(b1)>);
Expand All @@ -64,10 +64,7 @@ inline void test_AsType() {
static_assert(std::is_same_v<Base&&, decltype(b3)>);

TEST_PASS();
}
};
} // namespace test::as_type_h

AE_TEST_INLINE { TEST(test::as_type_h::test_AsType); }

#endif
#endif // AETHER_META_AS_TYPE_H_
5 changes: 1 addition & 4 deletions aether/meta/index_sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ constexpr auto make_range_sequence() {
# include "tests/inline.h"

namespace tests::index_sequence_h {
inline void test_ReverseSequence() {
AE_TEST_INLINE(test_ReverseSequence) {
constexpr auto rev_indices =
ae::reverse_sequence(std::make_index_sequence<10>());
static_assert(
Expand All @@ -75,9 +75,6 @@ inline void test_ReverseSequence() {
TEST_PASS();
}
} // namespace tests::index_sequence_h

AE_TEST_INLINE { RUN_TEST(tests::index_sequence_h::test_ReverseSequence); }

#endif

#endif // AETHER_META_INDEX_SEQUENCE_H_
5 changes: 1 addition & 4 deletions aether/meta/tag_invoke.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ struct Bar {
}
};

inline void test_TagInvoke() {
AE_TEST_INLINE(test_TagInvoke) {
using TestCpoType = ae::tag_t<test_cpo>;
static_assert(std::is_same_v<TestCpo, TestCpoType>);
using TestGetIntType = ae::tag_t<test_get_int>;
Expand Down Expand Up @@ -133,9 +133,6 @@ inline void test_TagInvoke() {
TEST_ASSERT_EQUAL(42, value);
}
} // namespace test::tag_invoke_h

AE_TEST_INLINE { TEST(test::tag_invoke_h::test_TagInvoke); }

#endif

#endif // AETHER_META_TAG_INVOKE_H_
Loading