-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.hpp
More file actions
82 lines (68 loc) · 2.5 KB
/
Copy pathscheduler.hpp
File metadata and controls
82 lines (68 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Scheduler port, simulation adapter — arms one-shot timers as OMNeT++
// self-messages.
//
// `scheduleAt`/`cancelEvent` are protected members of the hosting cSimpleModule,
// so the module injects them as callbacks at construction. Each core timer is
// backed by one cMessage; the module's handleMessage routes a fired timer message
// back here via fire(). Timers are one-shot (the core re-arms by calling after()
// again), matching the harness FakeScheduler in test/harness/world.hpp.
#pragma once
#include <functional>
#include <map>
#include <utility>
#include <omnetpp.h>
#include "ports/scheduler.hpp"
namespace loti::sim {
class SimScheduler final : public ports::Scheduler {
public:
using ScheduleFn = std::function<void(omnetpp::cMessage*, omnetpp::simtime_t)>;
using CancelFn = std::function<void(omnetpp::cMessage*)>;
SimScheduler(ScheduleFn schedule, CancelFn cancel)
: schedule_(std::move(schedule)), cancel_(std::move(cancel)) {}
~SimScheduler() override {
for (auto& [id, timer] : timers_) {
cancel_(timer.msg);
delete timer.msg;
}
}
ports::TimerId after(domain::Duration delay, std::function<void()> callback) override {
const auto id = ++last_id_;
auto* msg = new omnetpp::cMessage("loti-timer");
by_msg_[msg] = id;
timers_[id] = Timer{msg, std::move(callback)};
schedule_(msg, omnetpp::simTime() + omnetpp::SimTime::fromRaw(delay));
return id;
}
void cancel(ports::TimerId id) override {
auto it = timers_.find(id);
if (it == timers_.end()) return;
cancel_(it->second.msg);
by_msg_.erase(it->second.msg);
delete it->second.msg;
timers_.erase(it);
}
// Is this message one of ours? (asked by the host's handleMessage)
[[nodiscard]] bool owns(omnetpp::cMessage* msg) const { return by_msg_.count(msg) != 0; }
// Run the callback for a fired timer message and retire it (one-shot).
void fire(omnetpp::cMessage* msg) {
auto bit = by_msg_.find(msg);
if (bit == by_msg_.end()) return;
const auto id = bit->second;
auto callback = std::move(timers_[id].cb);
by_msg_.erase(bit);
timers_.erase(id);
delete msg; // delivered self-message is consumed
callback(); // may schedule a fresh timer
}
private:
struct Timer {
omnetpp::cMessage* msg = nullptr;
std::function<void()> cb;
};
ScheduleFn schedule_;
CancelFn cancel_;
std::map<ports::TimerId, Timer> timers_;
std::map<omnetpp::cMessage*, ports::TimerId> by_msg_;
ports::TimerId last_id_ = 0;
};
} // namespace loti::sim