All files / src/util discovery.ts

92.31% Statements 24/26
100% Branches 10/10
83.33% Functions 10/12
92.31% Lines 24/26

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 731x 1x       1x           39x 39x 39x       8x 8x     8x 8x         1x         1x         1x 1x     1x         2x 1x   2x           2x 1x   1x   2x       2x         1x      
import { Protocol, Messages } from 'raas-core';
import { Common, ErrorMessages } from './common';
import { MessageConnection } from 'vscode-jsonrpc';
import { EventEmitter } from 'events';
 
export class Discovery {
 
    private connection: MessageConnection;
    private emitter: EventEmitter;
 
    constructor(connection: MessageConnection, emitter: EventEmitter) {
        this.connection = connection;
        this.emitter = emitter;
        this.listenToDiscoveryChanges();
    }
 
    private listenToDiscoveryChanges() {
        this.connection.onNotification(Messages.Client.DiscoveryPathAddedNotification.type, (discoveryPath: any) => {
            this.emitter.emit('discoveryPathAdded', discoveryPath);
        });
 
        this.connection.onNotification(Messages.Client.DiscoveryPathRemovedNotification.type, discoveryPath => {
            this.emitter.emit('discoveryPathRemoved', discoveryPath);
        });
    }
 
    findServerInstallations(path: string, timeout: number = 2000): Promise<Protocol.ServerInstallation[]> {
        return Common.sendSimpleRequest(this.connection, Messages.Server.FindServerInstallationsRequest.type,
             {location: path}, timeout, ErrorMessages.FINDINSTALLATIONS_TIMEOUT);
    }
 
    addDiscoveryPathAsync(path: string, timeout: number = 2000): Promise<Protocol.Status> {
        return Common.sendSimpleRequest(this.connection, Messages.Server.AddDiscoveryPathRequest.type,
             {location: path}, timeout, ErrorMessages.ADDPATH_TIMEOUT);
    }
 
    addDiscoveryPathSync(path: string, timeout: number = 2000): Promise<Protocol.DiscoveryPath> {
        const discoveryPath = { location: path };
        const listener = (param: Protocol.DiscoveryPath) => {
            return param.location === discoveryPath.location;
        };
        return Common.sendRequestSync(this.connection, Messages.Server.AddDiscoveryPathRequest.type, discoveryPath,
            this.emitter, 'discoveryPathAdded', listener, timeout, ErrorMessages.ADDPATH_TIMEOUT);
    }
 
    removeDiscoveryPathAsync(path: string | Protocol.DiscoveryPath, timeout: number = 2000): Promise<Protocol.Status> {
        if (typeof(path) === 'string') {
            path = { location: path };
        }
        return Common.sendSimpleRequest(this.connection, Messages.Server.RemoveDiscoveryPathRequest.type,
             path, timeout, ErrorMessages.REMOVEPATH_TIMEOUT);
    }
 
    removeDiscoveryPathSync(path: string | Protocol.DiscoveryPath, timeout: number = 2000): Promise<Protocol.DiscoveryPath> {
        let discoveryPath: Protocol.DiscoveryPath;
        if (typeof(path) === 'string') {
            discoveryPath = { location: path };
        } else {
            discoveryPath = path;
        }
        const listener = (param: Protocol.DiscoveryPath) => {
            return param.location === discoveryPath.location;
        };
 
        return Common.sendRequestSync(this.connection, Messages.Server.RemoveDiscoveryPathRequest.type, discoveryPath, this.emitter,
            'discoveryPathRemoved', listener, timeout, ErrorMessages.REMOVEPATH_TIMEOUT);
    }
 
    getDiscoveryPaths(timeout: number = 2000): Promise<Protocol.DiscoveryPath[]> {
        return Common.sendSimpleRequest(this.connection, Messages.Server.GetDiscoveryPathsRequest.type,
             null, timeout, ErrorMessages.GETPATHS_TIMEOUT);
    }
}