127 lines
5.1 KiB
JavaScript
127 lines
5.1 KiB
JavaScript
// Virtual address / IP pressure system.
|
|
// Lets a connection claim a human-friendly alias (short code, number, IP-style address).
|
|
|
|
export class IPPressure {
|
|
constructor(mwse) {
|
|
this.mwse = mwse;
|
|
this.APNumber = undefined;
|
|
this.APShortCode = undefined;
|
|
this.APIPAddress = undefined;
|
|
}
|
|
|
|
async allocAPIPAddress() {
|
|
const { status, ip } = await this.mwse.EventPooling.request({ type: 'alloc/APIPAddress' });
|
|
if (status === 'success') { this.APIPAddress = ip; return ip; }
|
|
throw new Error('Error allocating virtual IP address');
|
|
}
|
|
|
|
async allocAPNumber() {
|
|
const { status, number } = await this.mwse.EventPooling.request({ type: 'alloc/APNumber' });
|
|
if (status === 'success') { this.APNumber = number; return number; }
|
|
throw new Error('Error allocating virtual number');
|
|
}
|
|
|
|
async allocAPShortCode() {
|
|
const { status, code } = await this.mwse.EventPooling.request({ type: 'alloc/APShortCode' });
|
|
if (status === 'success') { this.APShortCode = code; return code; }
|
|
throw new Error('Error allocating virtual short code');
|
|
}
|
|
|
|
async reallocAPIPAddress() {
|
|
const { status, ip } = await this.mwse.EventPooling.request({ type: 'realloc/APIPAddress' });
|
|
if (status === 'success') { this.APIPAddress = ip; return ip; }
|
|
throw new Error('Error reallocating virtual IP address');
|
|
}
|
|
|
|
async reallocAPNumber() {
|
|
const { status, number } = await this.mwse.EventPooling.request({ type: 'realloc/APNumber' });
|
|
if (status === 'success') { this.APNumber = number; return number; }
|
|
throw new Error('Error reallocating virtual number');
|
|
}
|
|
|
|
async reallocAPShortCode() {
|
|
const { status, code } = await this.mwse.EventPooling.request({ type: 'realloc/APShortCode' });
|
|
if (status === 'success') { this.APShortCode = code; return code; }
|
|
throw new Error('Error reallocating virtual short code');
|
|
}
|
|
|
|
async releaseAPIPAddress() {
|
|
const { status } = await this.mwse.EventPooling.request({ type: 'release/APIPAddress' });
|
|
if (status === 'success') { this.APIPAddress = undefined; return; }
|
|
throw new Error('Error releasing virtual IP address');
|
|
}
|
|
|
|
async releaseAPNumber() {
|
|
const { status } = await this.mwse.EventPooling.request({ type: 'release/APNumber' });
|
|
if (status === 'success') { this.APNumber = undefined; return; }
|
|
throw new Error('Error releasing virtual number');
|
|
}
|
|
|
|
async releaseAPShortCode() {
|
|
const { status } = await this.mwse.EventPooling.request({ type: 'release/APShortCode' });
|
|
if (status === 'success') { this.APShortCode = undefined; return; }
|
|
throw new Error('Error releasing virtual short code');
|
|
}
|
|
|
|
async queryAPIPAddress(ip) {
|
|
const { status, socket } = await this.mwse.EventPooling.request({
|
|
type: 'whois/APIPAddress', whois: ip
|
|
});
|
|
return status === 'success' ? socket : null;
|
|
}
|
|
|
|
async queryAPNumber(number) {
|
|
const { status, socket } = await this.mwse.EventPooling.request({
|
|
type: 'whois/APNumber', whois: number
|
|
});
|
|
return status === 'success' ? socket : null;
|
|
}
|
|
|
|
async queryAPShortCode(code) {
|
|
const { status, socket } = await this.mwse.EventPooling.request({
|
|
type: 'whois/APShortCode', whois: code
|
|
});
|
|
return status === 'success' ? socket : null;
|
|
}
|
|
|
|
// ---- Sub-network (#40) -----------------------------------------------
|
|
|
|
// Claim a /24 virtual sub-network (10.A.B.0/24).
|
|
// The caller becomes the subnet owner. Members can then call allocSubNetIP().
|
|
async allocSubNet() {
|
|
const { status, prefix } = await this.mwse.EventPooling.request({ type: 'alloc/APSubNet' });
|
|
if (status === 'success') { this.APSubNet = prefix; return prefix; }
|
|
throw new Error('Error allocating sub-network');
|
|
}
|
|
|
|
async releaseSubNet() {
|
|
const { status } = await this.mwse.EventPooling.request({ type: 'release/APSubNet' });
|
|
if (status === 'success') { this.APSubNet = undefined; this.APSubNetIP = undefined; return; }
|
|
throw new Error('Error releasing sub-network');
|
|
}
|
|
|
|
// Allocate a host IP within an existing subnet.
|
|
// prefix: the "10.A.B" string returned by allocSubNet().
|
|
async allocSubNetIP(prefix) {
|
|
const { status, ip } = await this.mwse.EventPooling.request({
|
|
type: 'alloc/APSubNetIP', prefix
|
|
});
|
|
if (status === 'success') { this.APSubNetIP = ip; return ip; }
|
|
throw new Error('Error allocating sub-network IP');
|
|
}
|
|
|
|
async releaseSubNetIP() {
|
|
const { status } = await this.mwse.EventPooling.request({ type: 'release/APSubNetIP' });
|
|
if (status === 'success') { this.APSubNetIP = undefined; return; }
|
|
throw new Error('Error releasing sub-network IP');
|
|
}
|
|
|
|
// Look up which socket holds a given IP within a subnet.
|
|
async querySubNetIP(prefix, ip) {
|
|
const { status, socket } = await this.mwse.EventPooling.request({
|
|
type: 'whois/APSubNetIP', prefix, whois: ip
|
|
});
|
|
return status === 'success' ? socket : null;
|
|
}
|
|
}
|