Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/bridge/SubProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
30 changes: 30 additions & 0 deletions src/manage/Uploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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<SubProcess> {
return this.connectSocket(compiled.file, listener);
}

private connectSocket(program: string, listener?: (chunk: any) => void): Promise<SubProcess> {
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[];
Expand Down
2 changes: 1 addition & 1 deletion src/testbeds/Emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class Emulator extends Platform {
}

kill(): Promise<void> {
this.connection.child.kill();
this.connection.child?.kill();
return super.kill();
}
}
1 change: 1 addition & 0 deletions src/testbeds/TestbedFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
5 changes: 3 additions & 2 deletions src/testbeds/TestbedSpecification.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export enum PlatformType {
arduino,
emulator,
debug
}

export interface ConnectionOptions {
Expand All @@ -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};
}
}
Expand Down