-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbottlingPlant.cc
57 lines (49 loc) · 1.52 KB
/
bottlingPlant.cc
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
//bottlingPlant.cc
#include "bottlingPlant.h"
#include "MPRNG.h"
#include "truck.h"
extern MPRNG mprng; //in main.cc
BottlingPlant::BottlingPlant( Printer & prt, NameServer & nameServer, unsigned int numVendingMachines,
unsigned int maxShippedPerFlavour, unsigned int maxStockPerFlavour,
unsigned int timeBetweenShipments ) :
prt(prt), nameServer(nameServer), numVendingMachines(numVendingMachines),
maxShippedPerFlavour(maxShippedPerFlavour), maxStockPerFlavour(maxStockPerFlavour),
timeBetweenShipments(timeBetweenShipments) {
flag = false;
product[0] = product[1] = product[2] = product[3] = 0;
}
void BottlingPlant::main() {
prt.print(Printer::Kind::BottlingPlant, 'S');
Truck trk(prt, nameServer, (*this), numVendingMachines, maxStockPerFlavour);
while (true) {
produce();
yield(timeBetweenShipments);
try {
_Enable {
_Accept ( ~BottlingPlant ) {
flag = true;
_Accept ( getShipment ); // make sure truck dies
break;
} or _Accept ( getShipment );
}//_Enable
} catch (...) {break;}
}//while
prt.print(Printer::Kind::BottlingPlant, 'F');
}
void BottlingPlant::produce() {
unsigned int b = 0;
for (int i = 0; i < 4; ++i) {
product[i] = mprng(0, maxShippedPerFlavour);
b += product[i];
}
prt.print(Printer::Kind::BottlingPlant, 'G', b);
}
void BottlingPlant::getShipment ( unsigned int cargo [] ) {
if (flag) {
_Throw Shutdown();
} else {
prt.print(Printer::Kind::BottlingPlant, 'P');
for (int i = 0; i < 4; ++i)
cargo[i] = product[i];
}
}