-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuf_friend.cpp
More file actions
70 lines (61 loc) · 1.8 KB
/
Copy pathuf_friend.cpp
File metadata and controls
70 lines (61 loc) · 1.8 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
#include "uf_friend.h"
#include "app.h"
#include "mailbox.h"
#include "uf_student.h"
using namespace uniflow;
Flow_Friend::Flow_Friend(uniflow::Runtime& rt)
: uniflow::Uniflow<Flow_Friend>(rt, "Flow_Friend"),
rng_(2)
{
AddTask(ctx_emit_);
plays_ = {
{Message::Kind::Play, "soccer", 0, 0, 6},
{Message::Kind::Play, "board game", 0, 0, 3},
{Message::Kind::Play, "movie", 0, 0, 4},
};
}
StepResult Flow_Friend::Task_Emit::Step1_Arm()
{
flow().ScheduleNext();
Describe("armed, will invite to play");
return Next(UF_FN(Step2_Tick));
}
StepResult Flow_Friend::Task_Emit::Step2_Tick()
{
if (flow().emitted_ >= flow().Total())
{
Describe("all invites sent");
return Done();
}
if (uniflow::Clock::now() < flow().next_at_)
{
Describe("waiting before next invite");
return Stay();
}
flow().EmitOne();
flow().ScheduleNext();
Flow_Student& student = App::inst().student;
if (student.IsIdle())
{
student.ctx_drain_.StartFlow();
}
return Stay();
}
void Flow_Friend::ScheduleNext()
{
auto min_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
GlobalConfig::kFriendMinGap).count();
auto max_ms = std::chrono::duration_cast<std::chrono::milliseconds>(
GlobalConfig::kFriendMaxGap).count();
std::uniform_int_distribution<long long> d(min_ms, max_ms);
next_at_ = uniflow::Clock::now() + std::chrono::milliseconds(d(rng_));
}
void Flow_Friend::EmitOne()
{
const Message& m = plays_[emitted_];
Mailbox::Push(m);
++emitted_;
GlobalLog::Add("friend posted play \"" + m.name + "\" inbox="
+ std::to_string(Mailbox::Size()));
Describe("posted \"", m.name, "\" inbox=", Mailbox::Size());
}