state = &atm::done_processing;
}
);
}
void waiting_for_card() {
interface_hardware.send(display_enter_card());
incoming.wait().handle
[&](card_inserted const& msg) {
account = msg.account;
pin = "";
interface_hardware.send(display_enter_pin());
state = &atm::getting_pin;
}
);
}
void done_processing() {
interface_hardware.send(eject_card());
state = &atm::waiting_for_card;
}
atm(atm const&) = delete;
atm& operator=(atm const&) = delete;
public:
atm(messaging::sender bank_,
messaging::sender interface_hardware_):
bank(bank_), interface_hardware(interface_hardware_) {}
void done() {
get_sender().send(messaging::close_queue());
}
void run() {
state = &atm::waiting_for_card;
try {
for (;;) {
(this->*state)();
}
} catch(messaging::close_queue const&) {
}
}
messaging::sender get_sender() {
return incoming;
}
};
Листинг С.8. Конечный автомат банка
class bank_machine {
messaging::receiver incoming;
unsigned balance;
public:
bank_machine():
balance(199) {}
void done() {
get_sender().send(messaging::close_queue());
}
void run() {
try {
for (;;) {
incoming.wait().handle
[&](verify_pin const& msg) {
if (msg.pin == "1937") {
msg.atm_queue.send(pin_verified());
} else {
msg.atm_queue.send(pin_incorrect());
}
}
).handle
[&](withdraw const& msg) {
if (balance >= msg.amount) {
msg.atm_queue.send(withdraw_ok());
balance -= msg.amount;
} else {
msg.atm_queue.send(withdraw_denied());
}
}
).handle
[&](get_balance const& msg) {
msg.atm_queue.send(::balance(balance));
}
).handle
[&](withdrawal_processed const& msg) {
}
).handle
[&](cancel_withdrawal const& msg) {
}
);
}
} catch(messaging::close_queue const&) {
}
}
messaging::sender get_sender() {
return incoming;
}
};
Листинг С.9. Конечный автомат пользовательского интерфейса
class interface_machine {
messaging::receiver incoming;
public:
void done() {
get_sender().send(messaging::close_queue());
}
void run() {
try {
for (;;) {
incoming.wait().handle
[&](issue_money const& msg) {
{
std::lock_guard
std::cout << "Issuing "
<< msg.amount << std::endl;
}
}
).handle
[&](display_insufficient_funds const& msg) {
{
std::lock_guard
std::cout << "Insufficient funds" << std::endl;
}
}
).handle
[&](display_enter_pin const& msg) {
{
std::lock_guard
std::cout
<< "Please enter your PIN (0-9)" << std::endl;
}
}
).handle
[&](display_enter_card const& msg) {
{
std::lock_guard
std::cout << "Please enter your card (I)"
<< std::endl;
}
}
).handle
[&](display_balance const& msg) {
{
std::lock_guard
std::cout
<< "The balance of your account is "
<< msg.amount << std::endl;
}
}
).handle
[&](display_withdrawal_options const& msg) {
{
std::lock_guard