diff --git a/src/framework/Testee.ts b/src/framework/Testee.ts index d7a2c5a..e53a575 100644 --- a/src/framework/Testee.ts +++ b/src/framework/Testee.ts @@ -81,9 +81,9 @@ export class Testee { // TODO unified with testbed interface this.name = name; this.specification = specification; this.timeout = timeout; - this.connector = new TestbedFactory(connectionTimeout); this.mapper = new SourceMapFactory(); this.framework = Framework.getImplementation(); + this.connector = new TestbedFactory(connectionTimeout, (chunk: Buffer) => this.framework.reporter.debug(chunk.toString())); } public bed(target: Target = Target.supervisor): Testbed | void { diff --git a/src/manage/Uploader.ts b/src/manage/Uploader.ts index 4547af9..6889416 100644 --- a/src/manage/Uploader.ts +++ b/src/manage/Uploader.ts @@ -50,7 +50,7 @@ export class UploaderFactory { export abstract class Uploader extends EventEmitter { - abstract upload(compiled: CompileOutput): Promise; + abstract upload(compiled: CompileOutput, listener?: (chunk: Buffer) => void): Promise; protected removeTmpDir(tmpdir: string): Promise { return new Promise((resolve, reject) => { @@ -109,7 +109,7 @@ export class EmulatorUploader extends Uploader { this.args = args; } - upload(compiled: CompileOutput, listener?: (chunk: any) => void): Promise { + upload(compiled: CompileOutput, listener?: (chunk: Buffer) => void): Promise { return this.connectSocket(compiled.file, listener); } @@ -118,7 +118,7 @@ export class EmulatorUploader extends Uploader { return spawn(this.interpreter, _args); } - private connectSocket(program: string, listener?: (chunk: any) => void): Promise { + private connectSocket(program: string, listener?: (chunk: Buffer) => void): Promise { const that = this; const process = this.startWARDuino(program); @@ -139,7 +139,7 @@ export class EmulatorUploader extends Uploader { const reader = new ReadlineParser(); process.stdout.pipe(reader); - reader.on('data', (data) => { + reader.on('data', (data: Buffer) => { if (listener !== undefined) { listener(data); } diff --git a/src/reporter/Reporter.ts b/src/reporter/Reporter.ts index 996c444..2a235c2 100644 --- a/src/reporter/Reporter.ts +++ b/src/reporter/Reporter.ts @@ -138,4 +138,10 @@ export class Reporter { error(text: string) { this.output += `error: ${text}\n`; } + + debug(text: string) { + if (this.verboseness === Verbosity.debug) { + console.debug(text); + } + } } \ No newline at end of file diff --git a/src/testbeds/Platform.ts b/src/testbeds/Platform.ts index 7142f3b..114389b 100644 --- a/src/testbeds/Platform.ts +++ b/src/testbeds/Platform.ts @@ -81,10 +81,12 @@ export abstract class Platform extends EventEmitter implements Testbed { // send request over duplex channel public sendRequest(map: SourceMap.Mapping, request: Request): Promise { + const message = `${request.type}${request.payload?.(map) ?? ''}\n`; + this.emit(TestbedEvents.Send, message); return new Promise((resolve, reject) => { this.requests.push([request, resolve]); - this.connection.channel.write(`${request.type}${request.payload?.(map) ?? ''}\n`, (err: any) => { - if (err !== null) { + this.connection.channel.write(message, (err: Error | null | undefined) => { + if (err !== null && err !== undefined) { reject(err); } }); diff --git a/src/testbeds/Testbed.ts b/src/testbeds/Testbed.ts index 96a535e..25cc7d3 100644 --- a/src/testbeds/Testbed.ts +++ b/src/testbeds/Testbed.ts @@ -8,7 +8,8 @@ export enum TestbedEvents { OnMessage = 'message', OnBreakpointHit = 'breakpoint', OnPushEvent = 'push', - Ready = 'ready' + Ready = 'ready', + Send = 'send' } export declare interface Testbed extends EventEmitter { @@ -25,4 +26,8 @@ export declare interface Testbed extends EventEmitter { on(event: TestbedEvents.OnBreakpointHit, listener: (message: Breakpoint) => void): this; on(event: TestbedEvents.OnPushEvent, listener: (data: string) => void): this; + + on(event: TestbedEvents.Ready, listener: (data: string) => void): this; + + on(event: TestbedEvents.Send, listener: (data: string) => void): this; } diff --git a/src/testbeds/TestbedFactory.ts b/src/testbeds/TestbedFactory.ts index 39861a8..fa0c31b 100644 --- a/src/testbeds/TestbedFactory.ts +++ b/src/testbeds/TestbedFactory.ts @@ -1,4 +1,4 @@ -import {Testbed} from './Testbed'; +import {Testbed, TestbedEvents} from './Testbed'; import {ARDUINO, EMULATOR, WABT} from '../util/env'; import {CompileOutput, CompilerFactory} from '../manage/Compiler'; import {DummyProxy, Emulator} from './Emulator'; @@ -13,28 +13,36 @@ export class TestbedFactory { public readonly timeout: number; private readonly compilerFactory: CompilerFactory; private readonly uploaderFactory: UploaderFactory; + private readonly listener?: (chunk: Buffer) => void; - constructor(timeout: number) { + constructor(timeout: number, listener?: (chunk: Buffer) => void) { this.timeout = timeout; + this.listener = listener; this.compilerFactory = new CompilerFactory(WABT); this.uploaderFactory = new UploaderFactory(EMULATOR, ARDUINO); } public async initialize(specification: TestbedSpecification, program: string, args: string[]): Promise { const compiled: CompileOutput = await this.compilerFactory.pickCompiler(program).compile(program).catch((e) => Promise.reject(e)); - const connection: Connection = await this.uploaderFactory.pickUploader(specification, args).upload(compiled).catch((e) => Promise.reject(e)); + const connection: Connection = await this.uploaderFactory.pickUploader(specification, args).upload(compiled, this.listener).catch((e) => Promise.reject(e)); + let testbed: Testbed; switch (specification.type) { case PlatformType.arduino: - return new Arduino(connection as Serial); + testbed = new Arduino(connection as Serial); + break; case PlatformType.emulator: case PlatformType.emu2emu: case PlatformType.debug: - return new Emulator(connection as SubProcess); + testbed = new Emulator(connection as SubProcess); + break; case PlatformType.emuproxy: - return new DummyProxy(connection as SubProcess, specification as ProxySpecification); + testbed = new DummyProxy(connection as SubProcess, specification as ProxySpecification); + break; default: return Promise.reject('Platform not implemented.'); } + testbed.on(TestbedEvents.Send, (message: string) => this.listener?.(Buffer.from(message))); + return testbed; } }