utCommandChannel.ts 1.92 KB
Newer Older
1
2
3
4
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { encodeCommand } from "../../../core/ipcInterface";
liuzhe-lz's avatar
liuzhe-lz committed
5
6
import { Command, CommandChannel, RunnerConnection } from "../../../training_service/reusable/commandChannel";
import { Channel, EnvironmentInformation } from "../../../training_service/reusable/environment";
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

class UtRunnerConnection extends RunnerConnection {

}

export class UtCommandChannel extends CommandChannel {
    private readonly receivedCommands: Command[] = [];

    public get channelName(): Channel {
        return "ut";
    }

    public async testSendCommandToTrialDispatcher(environment: EnvironmentInformation, commandType: string, commandData: any) {
        const content = encodeCommand(commandType, JSON.stringify(commandData));
        this.log.debug(`UtCommandChannel: env ${environment.id} send test command ${content}`);
        this.handleCommand(environment, content.toString("utf8"));
    }

    public async testReceiveCommandFromTrialDispatcher(): Promise<Command | undefined> {
        return this.receivedCommands.shift();
    }

29
    public async config(_key: string, _value: any): Promise<void> {
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
        // do nothing
    }

    public async start(): Promise<void> {
        // do nothing
    }

    public async stop(): Promise<void> {
        // do nothing
    }

    public async run(): Promise<void> {
        // do nothing
    }

    protected async sendCommandInternal(environment: EnvironmentInformation, message: string): Promise<void> {
        const parsedCommands = this.parseCommands(message);
        for (const parsedCommand of parsedCommands) {
            const command = new Command(environment, parsedCommand[0], parsedCommand[1]);
            this.receivedCommands.push(command);
        }
    }

    protected createRunnerConnection(environment: EnvironmentInformation): RunnerConnection {
        // do nothing
        return new UtRunnerConnection(environment);
    }
}