-
Notifications
You must be signed in to change notification settings - Fork 3
/
AddressPool.js
49 lines (40 loc) · 1.1 KB
/
AddressPool.js
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
const DeviceInstanceManager = require('./DeviceInstanceManager');
const ClientManager = require('./ClientManager');
class AddressPool {
constructor(start, end) {
const sp = start.split('.');
const ep = end.split('.');
this.base = sp.slice(0, 3).join('.');
if (this.base != ep.slice(0, 3).join('.')) {
throw new Error(`Bad base: ${this.base}`);
}
this.start = parseInt(sp[3]);
this.end = parseInt(ep[3]);
}
allocateAddress() {
for (let a = this.start; a < this.end; a++) {
const address = `${this.base}.${a}`;
// Check if we have a device with this IP
if (DeviceInstanceManager.getDeviceByIP(address)) {
continue;
}
// Check if we have a client with this IP
if (ClientManager.getClientByIP(address)) {
continue;
}
return address;
}
return null;
}
}
module.exports = {
pools: {},
getInstance: function(start, end) {
let pool = this.pools[`${start}:${end}`];
if (!pool) {
pool = new AddressPool(start, end);
this.pools[`${start}:${end}`] = pool;
}
return pool;
}
};