raas-client
Version:
193 lines • 7.18 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const net = require("net");
const rpc = require("vscode-jsonrpc");
const raas_core_1 = require("raas-core");
const events_1 = require("events");
const installable_1 = require("./util/installable");
const installation_1 = require("./util/installation");
const runtime_1 = require("./util/runtime");
class RaasClient {
constructor() {
this.starting = false;
this.connected = false;
this.emitter = new events_1.EventEmitter();
}
connect(timeout = 2000, host, port) {
if (this.connected || this.starting) {
return Promise.resolve();
}
this.host = host;
this.port = port;
this.connected = false;
this.starting = true;
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
this.starting = false;
return reject(new Error(`Failed to establish connection to ${this.host}:${this.port} within time`));
}, timeout);
this.socket = net.connect(this.port, this.host);
this.socket.on('connect', () => {
this.connection = rpc.createMessageConnection(new rpc.StreamMessageReader(this.socket), new rpc.StreamMessageWriter(this.socket));
this.connection.listen();
this.installableUtil = new installable_1.Installable(this.connection, this.emitter);
this.installationUtil = new installation_1.Installation(this.connection, this.emitter);
this.runtimeUtil = new runtime_1.Runtime(this.connection, this.emitter);
clearTimeout(timer);
this.connected = true;
this.starting = false;
this.emitter.emit('connected');
resolve();
});
this.socket.on('error', () => {
// this.disconnect();
});
this.socket.on('close', () => {
// this.disconnect();
});
});
}
disconnect() {
this.emitter.removeAllListeners();
this.connected = false;
if (this.connection) {
this.connection.dispose();
}
if (this.socket) {
this.socket.destroy();
}
// this.emitter.emit('disconnected');
}
onConnected(listener) {
this.emitter.on('connected', listener);
}
onDisconnected(listener) {
this.emitter.on('disconnected', listener);
}
shutdownServer() {
this.connection.sendNotification(raas_core_1.Messages.Server.ShutdownNotification.type);
this.disconnect();
}
// Installable
addInstallable(installable, timeout = 2000) {
return this.installableUtil.addInstallable(installable, timeout);
}
updateInstallableServer(installable, timeout = 2000) {
return this.installableUtil.updateInstallableServer(installable, timeout);
}
deleteInstallableServer(installable, timeout = 2000) {
return this.installableUtil.deleteInstallableServer(installable, timeout);
}
getAllInstallableServers(timeout = 2000) {
return this.installableUtil.getAllInstallableServers(timeout);
}
// Installation
addInstalledServer(installation, timeout = 2000) {
return this.installationUtil.addInstalledServer(installation, timeout);
}
installServer(request, timeout = 60000) {
return this.installationUtil.installServer(request, timeout);
}
cancelInstallation(installing, timeout = 2000) {
return this.installationUtil.cancelInstallation(installing, timeout);
}
deleteInstallation(installation, timeout = 2000) {
return this.installationUtil.deleteInstallation(installation, timeout);
}
updateInstallation(installation, timeout = 2000) {
return this.installationUtil.updateInstallation(installation, timeout);
}
getAllInstalledServers(timeout = 2000) {
return this.installationUtil.getAllInstalledServers(timeout);
}
getAllInstallingServers(timeout = 2000) {
return this.installationUtil.getAllInstallingServers(timeout);
}
// Runtime
startServer(config, timeout = 60000) {
return this.runtimeUtil.startServer(config, timeout);
}
stopServer(id, timeout = 60000) {
return this.runtimeUtil.stopServer(id, timeout);
}
startAnalysis(config, timeout = 60000) {
return this.runtimeUtil.startAnalysis(config, timeout);
}
cancelAnalysis(config, timeout = 60000) {
return this.runtimeUtil.cancelAnalysis(config, timeout);
}
getAllAnalyses(timeout = 2000) {
return this.runtimeUtil.getAllAnalyses(timeout);
}
getAllServerHandles(timeout = 2000) {
return this.runtimeUtil.getAllServerHandles(timeout);
}
onInstallableServerAdded(listener) {
this.emitter.on('installableServerAdded', listener);
}
onInstallableServerUpdated(listener) {
this.emitter.on('installableServerUpdated', listener);
}
onInstallableServerDeleted(listener) {
this.emitter.on('installableServerDeleted', listener);
}
onInstallingServer(listener) {
this.emitter.on('installingServer', listener);
}
onInstallingServerMessage(listener) {
this.emitter.on('installingServerMessage', listener);
}
onInstallingServerFailed(listener) {
this.emitter.on('installingServerFailedMessage', listener);
}
onInstallingServerCancelled(listener) {
this.emitter.on('installingServerCancelled', listener);
}
onInstallingServerCompleted(listener) {
this.emitter.on('installingServerCompleted', listener);
}
onInstalledServerDeleted(listener) {
this.emitter.on('installedServerDeleted', listener);
}
onInstalledServerUpdated(listener) {
this.emitter.on('installedServerUpdated', listener);
}
onStartingServer(listener) {
this.emitter.on('startingServer', listener);
}
onServerMessage(listener) {
this.emitter.on('serverMessage', listener);
}
onErrorStartingServer(listener) {
this.emitter.on('errorStartingServer', listener);
}
onServerStarted(listener) {
this.emitter.on('serverStarted', listener);
}
onServerStopped(listener) {
this.emitter.on('serverStopped', listener);
}
onAnalysisStarted(listener) {
this.emitter.on('analysisStarted', listener);
}
onAnalysisCancelled(listener) {
this.emitter.on('analysisCancelled', listener);
}
onAnalysisMessage(listener) {
this.emitter.on('analysisMessage', listener);
}
onAnalysisCompleted(listener) {
this.emitter.on('analysisComplete', listener);
}
getListeners(eventName) {
return this.emitter.listeners(eventName);
}
removeListener(eventName, listener) {
this.emitter.removeListener(eventName, listener);
}
removeAllListeners(eventName) {
this.emitter.removeAllListeners(eventName);
}
}
exports.RaasClient = RaasClient;
//# sourceMappingURL=raasClient.js.map