-
Notifications
You must be signed in to change notification settings - Fork 1
/
bottlingplant.cc
73 lines (61 loc) · 2.07 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "bottlingplant.h"
#include "printer.h"
#include <iostream>
using namespace std;
extern MPRNG rand_gen;
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),totalcount(0),shutdown(false)
{
//init stock space
for(int i=0;i<4;i++){
stock.push_back(0);
}
//cout<<"begin:"<<shutdown<<endl;
}
void BottlingPlant::getShipment(unsigned int cargo[]){
//the truck calls getShipment to obtain a shipment from the plant
//and the shipment is copied into the cargo array passed by the truck
totalcount = 0;
if(shutdown){
uRendezvousAcceptor();
throw BottlingPlant::Shutdown();
}
//production run
//??? consequence to move it here
for(unsigned int i=0;i<maxShippedPerFlavour;i++){
unsigned int mynumber=(rand_gen(maxShippedPerFlavour));
stock[i]=mynumber;
totalcount+=mynumber;
}
prt.print(Printer::BottlingPlant,'G',totalcount);
//ready to ship
for(unsigned int i=0;i<4;i++){
cargo[i]=stock[i];
}
prt.print(Printer::BottlingPlant,'P');
}
BottlingPlant::~BottlingPlant(){
}
void BottlingPlant::main(){
//it begines by creating a truck, performing a production run
//MaxShippedPerFlavour is the maximum number of bottles of each flavour generated during production
prt.print(Printer::BottlingPlant,'S');
mytruck=new Truck(prt,nameServer,*this,numVendingMachines,maxStockPerFlavour);
while(true){
//and waiting for the truck to pickup the production run
_Accept(BottlingPlant::~BottlingPlant){
shutdown=true;
_Accept(BottlingPlant::getShipment);
delete mytruck;
prt.print(Printer::BottlingPlant,'F');
return;
}
or _Accept(BottlingPlant::getShipment){
yield(timeBetweenShipments);
}
}
}