-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnameserver.cc
executable file
·53 lines (42 loc) · 1.45 KB
/
nameserver.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
#include "nameserver.h"
#include <iostream>
#include <fstream>
#include "printer.h"
#include "vendingmachine.h"
void NameServer::main() {
printer.print(Printer::Kind::NameServer, 'S');
while (vendingMachineCount < numVendingMachines) {
_Accept(VMregister);
}
for (;;) {
_Accept(~NameServer) {
break;
} or _Accept( getMachine || getMachineList );
}
std::ofstream test_out{"t.out", std::ios::app};
printer.print(Printer::Kind::NameServer, 'F');
}
NameServer::NameServer( Printer & prt, unsigned int numVendingMachines, unsigned int numStudents ):
printer{prt}, numVendingMachines{numVendingMachines}, numStudents{numStudents} {
vendingMachines = new VendingMachine* [numVendingMachines];
offsets = new int[numStudents] {0};
vendingMachineCount = 0;
}
NameServer::~NameServer() {
delete [] vendingMachines;
delete [] offsets;
}
void NameServer::VMregister( VendingMachine * vendingmachine ) {
printer.print(Printer::Kind::NameServer, 'R', vendingmachine->getId());
vendingMachines[vendingMachineCount++] = vendingmachine;
}
VendingMachine * NameServer::getMachine( unsigned int id ) {
int index = (id + offsets[id]) % numVendingMachines;
offsets[id]++;
VendingMachine * m = vendingMachines[index];
printer.print(Printer::Kind::NameServer, 'N', id, m->getId());
return m;
}
VendingMachine ** NameServer::getMachineList() {
return vendingMachines;
}