"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const network_1 = require("../network");
const proto_1 = require("./proto");
/**
 * RPC to talk with the status service of the conodes
 */
class StatusRPC {
    constructor(roster) {
        this.timeout = 10 * 1000;
        this.conn = roster.list
            .map((srvid) => new network_1.WebSocketConnection(srvid.getWebSocketAddress(), StatusRPC.serviceName));
    }
    /**
     * Set a new timeout value for the next requests
     * @param value Timeout in ms
     */
    setTimeout(value) {
        this.timeout = value;
    }
    /**
     * Fetch the status of the server at the given index
     * @param index Index of the server identity
     * @returns a promise that resolves with the status response
     */
    getStatus(index = 0) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (index >= this.conn.length || index < 0) {
                throw new Error("Index out of bound for the roster");
            }
            this.conn[index].setTimeout(this.timeout);
            return this.conn[index].send(new proto_1.StatusRequest(), proto_1.StatusResponse);
        });
    }
}
exports.default = StatusRPC;
StatusRPC.serviceName = "Status";
