-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatm.cpp
More file actions
66 lines (65 loc) · 2.06 KB
/
Copy pathatm.cpp
File metadata and controls
66 lines (65 loc) · 2.06 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
#include <string>
struct card_inserted
{
std::string acount;
};
/*消息传递同步机制 分析完代码发现其实就是一个有限状态机,涉及状态转换*/
class atm
{
messaging::receiver incoming;
messaging::sender bank;
messaging::sender interface_hardware;
void (atm::*state)();
std::string account;
std::string pin;
void waiting_for_card()
{
/*硬件发出信号 请插入卡*/
interface_hardware.send(display_enter_card());
/*静候消息处理回复 wait后面绑定一个一种消息类型(这里
是插入卡,若接受到的消息类型不匹配,便舍弃继续等待)*/
incoming.wait().handle<card_inserted>([&](card_inserted const &msg)
{
account = msg.acount;
pin="";
/*硬件发出信号 请输入密码 更换状态了 下次run
函数里的循环就执行getting_pin了*/
interface_hardware.send(display_enter_pin());
state = &atm::getting_pin;
});
}
void getting_pin()
{
/*这里等待三种消息,数字的按压 清除最后一个输入 取消按压
这里只有pin长度符合要求时才会发生状态通知*/
incoming.wait().handle<digit_pressed>([&](digit_pressed const &msg)
{
unsigned const pin_length = 4;
ping += msg.digit;
if(pin.length() == pin_length)
{
bank.send(verify_pin(account,pin,incoming));
state = &atm::verifying_pin;
}
}).handle<clear_last_pressed>([&](clear_last_pressed const &msg)
{
if(!pin.empty())
pin.resize(pin.length()-1);
}).handle<cancle_pressed>([&](concle_pressed const &msg)
{
state = &atm::done_processing;
});
}
void run()
{
state = &atm::waiting_for_card;
try
{
for(;;)
(this->*state)();
}
catch(messaging::close_queue const &)
{
}
}
}