Skip to content
4 changes: 3 additions & 1 deletion aether/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ list(APPEND actions_srcs
"actions/action_trigger.cpp"
"actions/action_registry.cpp"
"actions/action_context.cpp"
"actions/action_processor.cpp")
"actions/action_processor.cpp"
"actions/timer_action.cpp"
)

list(APPEND adapters_srcs
"adapters/adapter_factory.cpp"
Expand Down
65 changes: 0 additions & 65 deletions aether/actions/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,71 +161,6 @@ class Action : public IAction {
ActionTrigger* action_trigger_{};
ActionRegistry::IndexShare index_;
};

template <typename T = void>
class NotifyAction : public Action<NotifyAction<T>> {
public:
using Action<NotifyAction<T>>::Action;
using Action<NotifyAction<T>>::operator=;

TimePoint Update(TimePoint current_time) override {
if (notify_success_) {
notify_success_ = false;
this->ResultRepeat(static_cast<T&>(*this));
}
if (notify_failed_) {
notify_failed_ = false;
this->Error(static_cast<T&>(*this));
}
return current_time;
}

void Notify() {
notify_success_ = true;
this->Trigger();
}
void Failed() {
notify_failed_ = true;
this->Trigger();
}

private:
bool notify_success_{};
bool notify_failed_{};
};

template <>
class NotifyAction<void> : public Action<NotifyAction<void>> {
public:
using Action<NotifyAction<void>>::Action;
using Action<NotifyAction<void>>::operator=;

TimePoint Update(TimePoint current_time) override {
if (notify_success_) {
notify_success_ = false;
this->ResultRepeat(*this);
}
if (notify_failed_) {
notify_failed_ = false;
this->Error(*this);
}
return current_time;
}

void Notify() {
notify_success_ = true;
this->Trigger();
}
void Failed() {
notify_failed_ = true;
this->Trigger();
}

private:
bool notify_success_{};
bool notify_failed_{};
};

} // namespace ae

#endif // AETHER_ACTIONS_ACTION_H_
88 changes: 88 additions & 0 deletions aether/actions/notify_action.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2025 Aethernet 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 AETHER_ACTIONS_NOTIFY_ACTION_H_
#define AETHER_ACTIONS_NOTIFY_ACTION_H_

#include "aether/actions/action.h"

namespace ae {
template <typename T = void>
class NotifyAction : public Action<NotifyAction<T>> {
public:
using Action<NotifyAction<T>>::Action;
using Action<NotifyAction<T>>::operator=;

TimePoint Update(TimePoint current_time) override {
if (notify_success_) {
notify_success_ = false;
this->ResultRepeat(static_cast<T&>(*this));
}
if (notify_failed_) {
notify_failed_ = false;
this->Error(static_cast<T&>(*this));
}
return current_time;
}

void Notify() {
notify_success_ = true;
this->Trigger();
}
void Failed() {
notify_failed_ = true;
this->Trigger();
}

private:
bool notify_success_{};
bool notify_failed_{};
};

template <>
class NotifyAction<void> : public Action<NotifyAction<void>> {
public:
using Action<NotifyAction<void>>::Action;
using Action<NotifyAction<void>>::operator=;

TimePoint Update(TimePoint current_time) override {
if (notify_success_) {
notify_success_ = false;
this->ResultRepeat(*this);
}
if (notify_failed_) {
notify_failed_ = false;
this->Error(*this);
}
return current_time;
}

void Notify() {
notify_success_ = true;
this->Trigger();
}
void Failed() {
notify_failed_ = true;
this->Trigger();
}

private:
bool notify_success_{};
bool notify_failed_{};
};
} // namespace ae

#endif // AETHER_ACTIONS_NOTIFY_ACTION_H_
68 changes: 68 additions & 0 deletions aether/actions/timer_action.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2025 Aethernet 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.
*/

#include "aether/actions/timer_action.h"

namespace ae {
TimerAction::TimerAction(TimerAction&& other) noexcept
: Action{std::move(other)},
timer_duration_{other.timer_duration_},
start_time_{other.start_time_},
state_{std::move(other.state_)},
state_changed_sub_{state_.changed_event().Subscribe(
[this](auto) { Action::Trigger(); })} {}

TimerAction& TimerAction::operator=(TimerAction&& other) noexcept {
if (this != &other) {
Action::operator=(std::move(other));
timer_duration_ = other.timer_duration_;
start_time_ = other.start_time_;
state_ = std::move(other.state_);
state_changed_sub_ =
state_.changed_event().Subscribe([this](auto) { Action::Trigger(); });
}
return *this;
}

TimePoint TimerAction::Update(TimePoint current_time) {
if (state_.get() == State::kWait) {
if ((start_time_ + timer_duration_) > current_time) {
return start_time_ + timer_duration_;
}
state_ = State::kTriggered;
}
if (state_.changed()) {
switch (state_.Acquire()) {
case State::kStart:
start_time_ = current_time;
state_ = State::kWait;
break;
case State::kWait:
break;
case State::kTriggered:
Action::Result(*this);
return current_time;
case State::kStopped:
Action::Stop(*this);
return current_time;
}
}
return current_time;
}

void Stop();

} // namespace ae
66 changes: 66 additions & 0 deletions aether/actions/timer_action.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2025 Aethernet 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 AETHER_ACTIONS_TIMER_ACTION_H_
#define AETHER_ACTIONS_TIMER_ACTION_H_

#include <cstdint>
#include <utility>

#include "aether/common.h"
#include "aether/state_machine.h"
#include "aether/actions/action.h"
#include "aether/events/event_subscription.h"

namespace ae {
class TimerAction : public Action<TimerAction> {
enum class State : std::uint8_t {
kStart,
kWait,
kTriggered,
kStopped,
};

public:
TimerAction() = default;

template <typename TActionContext>
TimerAction(TActionContext&& action_context, Duration duration)
: Action{std::forward<TActionContext>(action_context)},
timer_duration_{duration},
state_{State::kStart},
state_changed_sub_{state_.changed_event().Subscribe(
[this](auto) { Action::Trigger(); })} {}

TimerAction(TimerAction const& other) = delete;
TimerAction(TimerAction&& other) noexcept;

TimerAction& operator=(TimerAction const& other) = delete;
TimerAction& operator=(TimerAction&& other) noexcept;

TimePoint Update(TimePoint current_time) override;

void Stop();

private:
Duration timer_duration_;
TimePoint start_time_;
StateMachine<State> state_;
Subscription state_changed_sub_;
};
} // namespace ae

#endif // AETHER_ACTIONS_TIMER_ACTION_H_
1 change: 1 addition & 0 deletions aether/ae_actions/registration/registration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ void Registration::OnConfirmRegistration(
ephemeral_uid_ = message.registration_response.ephemeral_uid;
uid_ = message.registration_response.uid;
cloud_ = message.registration_response.cloud;
assert(cloud_.size() > 0);

state_ = State::kRequestCloudResolving;
}
Expand Down
Loading
Loading