class port {
    constructor(value) {
        this.#value = value
        this.#free = true
    }
    get_value() {
        return this.#value
    }
    set_free() {
        this.#free = true
    }
    set_busy() {
        this.#free = false
    }
    is_free() {
        return this.#free
    }
    #value
    #free
}

module.exports = class port_alloc {
    constructor(port_begin, count) {
        this.#ports = new Array()
        for (var i = port_begin; i < port_begin + count; ++i) {
            this.#ports.push(new port(i))
        }
    }
    get() {
        for (var i in this.#ports) {
            if (this.#ports[i].is_free()) {
                this.#ports[i].set_busy()
                return this.#ports[i].get_value()
            }
        }
        throw new Error('no free ports')
    }
    free(busy_port) {
        for (var i in this.#ports) {
            if (this.#ports[i].get_value() == busy_port) {
                this.#ports[i].set_free()
                return
            }
        }
    }
    #ports
}