-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnameServer.cc
More file actions
72 lines (58 loc) · 2.45 KB
/
Copy pathnameServer.cc
File metadata and controls
72 lines (58 loc) · 2.45 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
71
72
#include "nameServer.h"
#include "printer.h"
#include "vendingMachine.h"
NameServer::NameServer(Printer &prt, unsigned int numVendingMachines, unsigned int numStudents) :
printer(prt),
numVendingMachines(numVendingMachines),
numStudents(numStudents),
numRegistered(0),
machineList(new VendingMachine*[numVendingMachines]),
machineAssignment(new unsigned int[numStudents]) {
}
NameServer::~NameServer() {
delete[] machineList;
delete[] machineAssignment;
}
void NameServer::VMregister(VendingMachine *vendingmachine) {
// add `vendingMachine` to the end of the list of registered machines
machineList[numRegistered] = vendingmachine;
printer.print(Printer::NameServer, (char)Register, vendingmachine->getId());
}
VendingMachine* NameServer::getMachine(unsigned int id) {
// cache a pointer to the vending machine associated with student `id`
VendingMachine *ret = machineList[machineAssignment[id]];
printer.print(Printer::NameServer, (char)New, id, ret->getId());
// after caching the return value, perform a circular increment to the next vending machine
machineAssignment[id] = (machineAssignment[id] < numVendingMachines - 1) ? machineAssignment[id] + 1 : 0;
return ret;
}
VendingMachine** NameServer::getMachineList() {
return machineList;
}
void NameServer::main() {
printer.print(Printer::NameServer, Starting);
outer: while (true) {
// block tasks from using vending machines until `numVendingMachines` machines have been registered
while (numRegistered < numVendingMachines) {
_Accept(~NameServer) {
// allow the destructor to terminate the loop
break outer;
} or _Accept(VMregister) {
// each time we register a vending machine, increment `numRegistered`
numRegistered++;
}
}
// now that machines have been registered, assign each student to a vending machine
for (unsigned int i = 0; i < numStudents; i++) {
unsigned int machine = i % numVendingMachines;
machineAssignment[i] = machine;
}
while (true) {
_Accept(~NameServer) {
// allow the destructor to terminate the loop
break outer;
} or _Accept(getMachine, getMachineList); // otherwise, finally allow normal calls to our public methods
}
}
printer.print(Printer::NameServer, (char)Finished);
}