All files / src/util runtime.ts

17.14% Statements 6/35
0% Branches 0/6
4.55% Functions 1/22
17.14% Lines 6/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100    1x 1x   1x           5x 5x 5x                                                                                                                                                                            
import { MessageConnection } from 'vscode-jsonrpc';
import { EventEmitter } from 'events';
import { Messages, Protocol } from 'raas-core';
import { Common, ErrorMessages } from './common';
 
export class Runtime {
 
    private connection: MessageConnection;
    private emitter: EventEmitter;
 
    constructor(connection: MessageConnection, emitter: EventEmitter) {
        this.connection = connection;
        this.emitter = emitter;
        this.listenToRuntimeChanges();
    }
 
    private listenToRuntimeChanges() {
        this.connection.onNotification(Messages.Client.StartingServerNotification.type,
            serverRunConfiguration => {
            this.emitter.emit('startingServer', serverRunConfiguration);
        });
        this.connection.onNotification(Messages.Client.ServerMessageNotification.type,
            data => {
            this.emitter.emit('serverMessage', data);
        });
        this.connection.onNotification(Messages.Client.ErrorStartingServerNotification.type,
            data => {
            this.emitter.emit('errorStartingServer', data);
        });
        this.connection.onNotification(Messages.Client.ServerStartedNotification.type,
            serverInstanceHandle => {
            this.emitter.emit('serverStarted', serverInstanceHandle);
        });
        this.connection.onNotification(Messages.Client.ServerStoppedNotification.type,
            serverInstanceHandle => {
            this.emitter.emit('serverStopped', serverInstanceHandle);
        });
        this.connection.onNotification(Messages.Client.AnalysisStartedNotification.type,
            analysis => {
            this.emitter.emit('analysisStarted', analysis);
        });
        this.connection.onNotification(Messages.Client.AnalysisCancelledNotification.type,
            analysis => {
            this.emitter.emit('analysisCancelled', analysis);
        });
        this.connection.onNotification(Messages.Client.AnalysisMessageNotification.type,
            data => {
            this.emitter.emit('analysisMessage', data);
        });
        this.connection.onNotification(Messages.Client.AnalysisCompletedNotification.type,
            data => {
            this.emitter.emit('analysisComplete', data);
        });
    }
 
    startServer(config: Protocol.ServerRunConfiguration, timeout: number = 60000): Promise<Protocol.Status> {
        return this.send(Messages.Server.StartServerRequest.type, config,
            'serverStarted',
            (param: Protocol.ServerInstanceHandle) => param.runConfiguration.id === config.id,
            timeout, ErrorMessages.STARTSERVER_TIMEOUT);
    }
 
    stopServer(instance: Protocol.ServerInstanceHandle, timeout: number = 60000): Promise<Protocol.Status> {
        return this.send(Messages.Server.StopServerRequest.type, instance,
            'serverStopped',
            (param: Protocol.ServerInstanceHandle) => param.id === instance.id,
            timeout, ErrorMessages.STOPSERVER_TIMEOUT);
    }
 
    startAnalysis(config: Protocol.AnalysisRunConfiguration, timeout: number = 60000): Promise<Protocol.Status> {
        return this.send(Messages.Server.StartAnalaysisRequest.type, config,
            'analysisStarted',
            (param: Protocol.Analysis) => param.analysisConfiguration.id === config.id,
            timeout, ErrorMessages.STARTANALYSIS_TIMEOUT);
    }
 
    cancelAnalysis(config: Protocol.Analysis, timeout: number = 60000): Promise<Protocol.Status> {
        return this.send(Messages.Server.CancelAnalaysisRequest.type, config,
            'analysisCancelled',
            (param: Protocol.Analysis) => param.id === config.id,
            timeout, ErrorMessages.CANCELANALYSIS_TIMEOUT);
    }
 
    getAllAnalyses(timeout: number = 2000): Promise<Protocol.Analysis[]> {
        return Common.sendSimpleRequest(this.connection, Messages.Server.GetAllAnalysesRequest.type, null,
            timeout, ErrorMessages.GETALLANALYSES_TIMEOUT);
    }
 
    getAllServerHandles(timeout: number = 2000): Promise<Protocol.ServerInstanceHandle[]> {
        return Common.sendSimpleRequest(this.connection, Messages.Server.GetServerHandlesRequest.type, null,
            timeout, ErrorMessages.GETALLSERVERHANDLES_TIMEOUT);
    }
 
    private send(messageType: any, payload: any, id: string,
        listener: (params: any) => boolean, timeout: number,
        timeoutMessage: string): Promise<Protocol.Status> {
        return Common.sendRequestSync(this.connection, messageType,
            payload, this.emitter, id, listener, timeout, timeoutMessage);
    }
}