diff --git a/src/bridge/SubProcess.ts b/src/bridge/SubProcess.ts index 5c13615..7469cbb 100644 --- a/src/bridge/SubProcess.ts +++ b/src/bridge/SubProcess.ts @@ -4,9 +4,9 @@ import {Connection} from './Connection'; export class SubProcess implements Connection { public channel: Duplex; - public child: ChildProcess; + public child?: ChildProcess; - constructor(channel: Duplex, process: ChildProcess) { + constructor(channel: Duplex, process?: ChildProcess) { this.channel = channel; this.child = process; } diff --git a/src/manage/Uploader.ts b/src/manage/Uploader.ts index 4c179c2..810435d 100644 --- a/src/manage/Uploader.ts +++ b/src/manage/Uploader.ts @@ -39,6 +39,8 @@ export class UploaderFactory { return new ArduinoUploader(this.arduino, args, specification.options as SerialOptions); case PlatformType.emulator: return new EmulatorUploader(this.emulator, args, specification.options as SubprocessOptions); + case PlatformType.debug: + return new EmulatorConnector(specification.options as SubprocessOptions) } throw new Error('Unsupported file type'); } @@ -65,6 +67,34 @@ function isReadable(x: Readable | null): x is Readable { return x !== null; } +export class EmulatorConnector extends Uploader { + private readonly port: number; + + constructor(options: SubprocessOptions) { + super(); + this.port = options.port; + } + + upload(compiled: CompileOutput, listener?: (chunk: any) => void): Promise { + return this.connectSocket(compiled.file, listener); + } + + private connectSocket(program: string, listener?: (chunk: any) => void): Promise { + const that = this; + + return new Promise(function (resolve, reject) { + const client = new net.Socket(); + client.connect(that.port, () => { + that.emit(UploaderEvents.connected); + if (listener !== undefined) { + client.on('data', listener); + } + resolve(new SubProcess(client)); + }); + }); + } +} + export class EmulatorUploader extends Uploader { private readonly interpreter: string; private readonly args: string[]; diff --git a/src/testbeds/Emulator.ts b/src/testbeds/Emulator.ts index 3beb514..c91f4dc 100644 --- a/src/testbeds/Emulator.ts +++ b/src/testbeds/Emulator.ts @@ -14,7 +14,7 @@ export class Emulator extends Platform { } kill(): Promise { - this.connection.child.kill(); + this.connection.child?.kill(); return super.kill(); } } diff --git a/src/testbeds/TestbedFactory.ts b/src/testbeds/TestbedFactory.ts index e392967..af7f47b 100644 --- a/src/testbeds/TestbedFactory.ts +++ b/src/testbeds/TestbedFactory.ts @@ -28,6 +28,7 @@ export class TestbedFactory { case PlatformType.arduino: return new Arduino(connection as Serial); case PlatformType.emulator: + case PlatformType.debug: return new Emulator(connection as SubProcess); default: return Promise.reject('Platform not implemented.'); diff --git a/src/testbeds/TestbedSpecification.ts b/src/testbeds/TestbedSpecification.ts index 67671d0..eaf0ef7 100644 --- a/src/testbeds/TestbedSpecification.ts +++ b/src/testbeds/TestbedSpecification.ts @@ -1,6 +1,7 @@ export enum PlatformType { arduino, emulator, + debug } export interface ConnectionOptions { @@ -27,8 +28,8 @@ export class EmulatorSpecification implements TestbedSpecification { public readonly type: PlatformType; public readonly options: SubprocessOptions; - constructor(port: number) { - this.type = PlatformType.emulator; + constructor(port: number, type: PlatformType = PlatformType.emulator) { + this.type = type; this.options = {port: port}; } }