Skip to content

Commit db15cfd

Browse files
Merge pull request #114 from aethernetio/107-make-select-new-connection-disconnection
107 make select new connection disconnection
2 parents fb8a5cc + 129cf76 commit db15cfd

19 files changed

Lines changed: 343 additions & 118 deletions

aether/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ list(APPEND actions_srcs
100100
"actions/action_trigger.cpp"
101101
"actions/action_registry.cpp"
102102
"actions/action_context.cpp"
103-
"actions/action_processor.cpp")
103+
"actions/action_processor.cpp"
104+
"actions/timer_action.cpp"
105+
)
104106

105107
list(APPEND adapters_srcs
106108
"adapters/adapter_factory.cpp"

aether/actions/action.h

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -161,71 +161,6 @@ class Action : public IAction {
161161
ActionTrigger* action_trigger_{};
162162
ActionRegistry::IndexShare index_;
163163
};
164-
165-
template <typename T = void>
166-
class NotifyAction : public Action<NotifyAction<T>> {
167-
public:
168-
using Action<NotifyAction<T>>::Action;
169-
using Action<NotifyAction<T>>::operator=;
170-
171-
TimePoint Update(TimePoint current_time) override {
172-
if (notify_success_) {
173-
notify_success_ = false;
174-
this->ResultRepeat(static_cast<T&>(*this));
175-
}
176-
if (notify_failed_) {
177-
notify_failed_ = false;
178-
this->Error(static_cast<T&>(*this));
179-
}
180-
return current_time;
181-
}
182-
183-
void Notify() {
184-
notify_success_ = true;
185-
this->Trigger();
186-
}
187-
void Failed() {
188-
notify_failed_ = true;
189-
this->Trigger();
190-
}
191-
192-
private:
193-
bool notify_success_{};
194-
bool notify_failed_{};
195-
};
196-
197-
template <>
198-
class NotifyAction<void> : public Action<NotifyAction<void>> {
199-
public:
200-
using Action<NotifyAction<void>>::Action;
201-
using Action<NotifyAction<void>>::operator=;
202-
203-
TimePoint Update(TimePoint current_time) override {
204-
if (notify_success_) {
205-
notify_success_ = false;
206-
this->ResultRepeat(*this);
207-
}
208-
if (notify_failed_) {
209-
notify_failed_ = false;
210-
this->Error(*this);
211-
}
212-
return current_time;
213-
}
214-
215-
void Notify() {
216-
notify_success_ = true;
217-
this->Trigger();
218-
}
219-
void Failed() {
220-
notify_failed_ = true;
221-
this->Trigger();
222-
}
223-
224-
private:
225-
bool notify_success_{};
226-
bool notify_failed_{};
227-
};
228-
229164
} // namespace ae
230165

231166
#endif // AETHER_ACTIONS_ACTION_H_

aether/actions/notify_action.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2025 Aethernet Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef AETHER_ACTIONS_NOTIFY_ACTION_H_
18+
#define AETHER_ACTIONS_NOTIFY_ACTION_H_
19+
20+
#include "aether/actions/action.h"
21+
22+
namespace ae {
23+
template <typename T = void>
24+
class NotifyAction : public Action<NotifyAction<T>> {
25+
public:
26+
using Action<NotifyAction<T>>::Action;
27+
using Action<NotifyAction<T>>::operator=;
28+
29+
TimePoint Update(TimePoint current_time) override {
30+
if (notify_success_) {
31+
notify_success_ = false;
32+
this->ResultRepeat(static_cast<T&>(*this));
33+
}
34+
if (notify_failed_) {
35+
notify_failed_ = false;
36+
this->Error(static_cast<T&>(*this));
37+
}
38+
return current_time;
39+
}
40+
41+
void Notify() {
42+
notify_success_ = true;
43+
this->Trigger();
44+
}
45+
void Failed() {
46+
notify_failed_ = true;
47+
this->Trigger();
48+
}
49+
50+
private:
51+
bool notify_success_{};
52+
bool notify_failed_{};
53+
};
54+
55+
template <>
56+
class NotifyAction<void> : public Action<NotifyAction<void>> {
57+
public:
58+
using Action<NotifyAction<void>>::Action;
59+
using Action<NotifyAction<void>>::operator=;
60+
61+
TimePoint Update(TimePoint current_time) override {
62+
if (notify_success_) {
63+
notify_success_ = false;
64+
this->ResultRepeat(*this);
65+
}
66+
if (notify_failed_) {
67+
notify_failed_ = false;
68+
this->Error(*this);
69+
}
70+
return current_time;
71+
}
72+
73+
void Notify() {
74+
notify_success_ = true;
75+
this->Trigger();
76+
}
77+
void Failed() {
78+
notify_failed_ = true;
79+
this->Trigger();
80+
}
81+
82+
private:
83+
bool notify_success_{};
84+
bool notify_failed_{};
85+
};
86+
} // namespace ae
87+
88+
#endif // AETHER_ACTIONS_NOTIFY_ACTION_H_

aether/actions/timer_action.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2025 Aethernet Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "aether/actions/timer_action.h"
18+
19+
namespace ae {
20+
TimerAction::TimerAction(TimerAction&& other) noexcept
21+
: Action{std::move(other)},
22+
timer_duration_{other.timer_duration_},
23+
start_time_{other.start_time_},
24+
state_{std::move(other.state_)},
25+
state_changed_sub_{state_.changed_event().Subscribe(
26+
[this](auto) { Action::Trigger(); })} {}
27+
28+
TimerAction& TimerAction::operator=(TimerAction&& other) noexcept {
29+
if (this != &other) {
30+
Action::operator=(std::move(other));
31+
timer_duration_ = other.timer_duration_;
32+
start_time_ = other.start_time_;
33+
state_ = std::move(other.state_);
34+
state_changed_sub_ =
35+
state_.changed_event().Subscribe([this](auto) { Action::Trigger(); });
36+
}
37+
return *this;
38+
}
39+
40+
TimePoint TimerAction::Update(TimePoint current_time) {
41+
if (state_.get() == State::kWait) {
42+
if ((start_time_ + timer_duration_) > current_time) {
43+
return start_time_ + timer_duration_;
44+
}
45+
state_ = State::kTriggered;
46+
}
47+
if (state_.changed()) {
48+
switch (state_.Acquire()) {
49+
case State::kStart:
50+
start_time_ = current_time;
51+
state_ = State::kWait;
52+
break;
53+
case State::kWait:
54+
break;
55+
case State::kTriggered:
56+
Action::Result(*this);
57+
return current_time;
58+
case State::kStopped:
59+
Action::Stop(*this);
60+
return current_time;
61+
}
62+
}
63+
return current_time;
64+
}
65+
66+
void Stop();
67+
68+
} // namespace ae

aether/actions/timer_action.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2025 Aethernet Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef AETHER_ACTIONS_TIMER_ACTION_H_
18+
#define AETHER_ACTIONS_TIMER_ACTION_H_
19+
20+
#include <cstdint>
21+
#include <utility>
22+
23+
#include "aether/common.h"
24+
#include "aether/state_machine.h"
25+
#include "aether/actions/action.h"
26+
#include "aether/events/event_subscription.h"
27+
28+
namespace ae {
29+
class TimerAction : public Action<TimerAction> {
30+
enum class State : std::uint8_t {
31+
kStart,
32+
kWait,
33+
kTriggered,
34+
kStopped,
35+
};
36+
37+
public:
38+
TimerAction() = default;
39+
40+
template <typename TActionContext>
41+
TimerAction(TActionContext&& action_context, Duration duration)
42+
: Action{std::forward<TActionContext>(action_context)},
43+
timer_duration_{duration},
44+
state_{State::kStart},
45+
state_changed_sub_{state_.changed_event().Subscribe(
46+
[this](auto) { Action::Trigger(); })} {}
47+
48+
TimerAction(TimerAction const& other) = delete;
49+
TimerAction(TimerAction&& other) noexcept;
50+
51+
TimerAction& operator=(TimerAction const& other) = delete;
52+
TimerAction& operator=(TimerAction&& other) noexcept;
53+
54+
TimePoint Update(TimePoint current_time) override;
55+
56+
void Stop();
57+
58+
private:
59+
Duration timer_duration_;
60+
TimePoint start_time_;
61+
StateMachine<State> state_;
62+
Subscription state_changed_sub_;
63+
};
64+
} // namespace ae
65+
66+
#endif // AETHER_ACTIONS_TIMER_ACTION_H_

aether/ae_actions/registration/registration.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ void Registration::OnConfirmRegistration(
383383
ephemeral_uid_ = message.registration_response.ephemeral_uid;
384384
uid_ = message.registration_response.uid;
385385
cloud_ = message.registration_response.cloud;
386+
assert(cloud_.size() > 0);
386387

387388
state_ = State::kRequestCloudResolving;
388389
}

0 commit comments

Comments
 (0)