All files / src/util serverLauncher.ts

83.33% Statements 35/42
85.71% Branches 12/14
66.67% Functions 14/21
83.33% Lines 35/42

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           41x 41x 41x       10x       10x       10x       10x       10x           1x         4x 4x 2x       4x 3x 2x 2x 2x 2x       4x 4x         1x         4x 4x 2x       4x 3x 2x 2x 2x 2x       4x 4x                          
import { MessageConnection } from 'vscode-jsonrpc';
import { Protocol, Messages, RhamtConfiguration } from 'raas-core';
import { EventEmitter } from 'events';
import { Common, ErrorMessages } from './common';
 
export class ServerLauncher {
 
    private connection: MessageConnection;
    private emitter: EventEmitter;
 
    constructor(connection: MessageConnection, emitter: EventEmitter) {
        this.connection = connection;
        this.emitter = emitter;
        this.listenToServerChanges();
    }
 
    private listenToServerChanges() {
        this.connection.onNotification(Messages.Client.ServerProcessCreatedNotification.type, process => {
            this.emitter.emit('serverProcessCreated', process);
        });
 
        this.connection.onNotification(Messages.Client.ServerProcessOutputAppendedNotification.type, output => {
            this.emitter.emit('serverOutputAppended', output);
        });
 
        this.connection.onNotification(Messages.Client.ServerProcessTerminatedNotification.type, process => {
            this.emitter.emit('serverProcessTerminated', process);
        });
 
        this.connection.onNotification(Messages.Client.ServerStateChangedNotification.type, state => {
            this.emitter.emit('serverStateChanged', state);
        });
 
        this.connection.onNotification(Messages.Client.AnalysisMessageNotification.type, out => {
            this.emitter.emit('analysisMessage', out);
        });
    }
 
    startServerAsync(config: Protocol.ServerConfig, timeout: number = 2000): Promise<Protocol.Status> {
        return Common.sendSimpleRequest(this.connection, Messages.Server.StartServerAsyncRequest.type, config,
            timeout, ErrorMessages.STARTSERVER_TIMEOUT);
    }
 
    startServerSync(config: Protocol.ServerConfig, timeout: number = 60000): Promise<Protocol.ServerConfig> {
        return new Promise<Protocol.ServerConfig>((resolve, reject) => {
            const timer = setTimeout(() => {
                return reject(new Error(ErrorMessages.STARTSERVER_TIMEOUT));
            }, timeout);
 
            let result: Thenable<Protocol.Status>;
            const listener = (server: Protocol.ServerConfig) => {
                if (server.id === config.id && server.status.state === Protocol.ServerState.STARTED) {
                    result.then(() => {
                        clearTimeout(timer);
                        this.emitter.removeListener('serverStateChanged', listener);
                        resolve(server);
                    });
                }
            };
            this.emitter.prependListener('serverStateChanged', listener);
            result = this.connection.sendRequest(Messages.Server.StartServerAsyncRequest.type, config);
        });
    }
 
    stopServerAsync(config: Protocol.ServerConfig, timeout: number = 2000): Promise<Protocol.Status> {
        return Common.sendSimpleRequest(this.connection, Messages.Server.StopServerAsyncRequest.type, config,
            timeout, ErrorMessages.STOPSERVER_TIMEOUT);
    }
 
    stopServerSync(config: Protocol.ServerConfig, timeout: number = 60000): Promise<Protocol.ServerConfig> {
        return new Promise<Protocol.ServerConfig>((resolve, reject) => {
            const timer = setTimeout(() => {
                return reject(new Error(ErrorMessages.STOPSERVER_TIMEOUT));
            }, timeout);
 
            let result: Thenable<Protocol.Status>;
            const listener = (server: Protocol.ServerConfig) => {
                if (server.id === config.id && server.status.state === Protocol.ServerState.STOPPED) {
                    result.then(() => {
                        clearTimeout(timer);
                        this.emitter.removeListener('serverStateChanged', listener);
                        resolve(server);
                    });
                }
            };
            this.emitter.prependListener('serverStateChanged', listener);
            result = this.connection.sendRequest(Messages.Server.StopServerAsyncRequest.type, config);
        });
    }
 
    startAnalysis(runConfig: RhamtConfiguration, serverConfig: Protocol.ServerConfig, timeout: number = 60000): Promise<Protocol.Status> {
        return Common.sendSimpleRequest(this.connection, Messages.Server.StartAnalaysisRequest.type,
            {runConfig, serverConfig}, timeout, ErrorMessages.STARTANALYSIS_TIMEOUT);
    }
 
    cancelAnalysis(runConfig: RhamtConfiguration, serverConfig: Protocol.ServerConfig, timeout: number = 60000): Promise<Protocol.Status> {
        return Common.sendSimpleRequest(this.connection, Messages.Server.CancelAnalaysisRequest.type,
            {runConfig, serverConfig}, timeout, ErrorMessages.CANCELANALYSIS_TIMEOUT);
    }
}