11import { Platform } from './Platform' ;
22import { SubProcess } from '../bridge/SubProcess' ;
3+ import { Connection } from '../bridge/Connection' ;
4+ import { UploaderFactory } from '../manage/Uploader' ;
5+ import { ARDUINO , EMULATOR } from '../util/env' ;
6+ import { ProxySpecification } from './TestbedSpecification' ;
7+ import { CompileOutput } from '../manage/Compiler' ;
8+ import * as net from 'node:net' ;
9+ import { SourceMap } from '../sourcemap/SourceMap' ;
10+ import { Request } from '../messaging/Message' ;
11+ import { AddressInfo , Socket } from 'node:net' ;
12+ import { MessageQueue } from '../messaging/MessageQueue' ;
313
414export class Emulator extends Platform {
515 readonly name : string = 'Emulator' ;
@@ -18,3 +28,54 @@ export class Emulator extends Platform {
1828 return super . kill ( ) ;
1929 }
2030}
31+
32+ /**
33+ * Dummy proxy object, forwards all requests on a dummy port to the real proxy instance
34+ *
35+ * todo this allows for testing the communication between supervisor and proxy
36+ */
37+ export class DummyProxy extends Emulator {
38+ dummy : net . Server ;
39+
40+ protected forwarding : MessageQueue ;
41+
42+ private supervisor ?: Socket ;
43+
44+ constructor ( connection : SubProcess ) {
45+ super ( connection ) ;
46+
47+ this . forwarding = new MessageQueue ( '\n' ) ;
48+
49+ this . dummy = new net . Server ( ) ;
50+ }
51+
52+ public async init ( specification : ProxySpecification ) {
53+ this . dummy . on ( 'connection' , ( connection ) => {
54+ this . supervisor = connection ;
55+ connection . on ( 'data' , ( data ) => {
56+ this . connection . channel . write ( data . toString ( ) ) ;
57+ } )
58+ } ) ;
59+ this . dummy . listen ( specification . dummy . port ) ;
60+ }
61+
62+ protected listen ( ) : void {
63+ this . connection . channel . on ( 'data' , ( data : Buffer ) => {
64+ if ( this . waitingForMessages ( ) ) {
65+ this . messages . push ( data . toString ( ) ) ;
66+ this . process ( ) ;
67+ } else {
68+ this . forwarding . push ( data . toString ( ) ) ;
69+ while ( this . forwarding . hasCompleteMessage ( ) ) {
70+ const message = this . forwarding . pop ( )
71+ if ( ! message ?. includes ( 'Interrupt' ) ) this . supervisor ! . write ( message ! . toString ( ) ) ;
72+ }
73+
74+ }
75+ } ) ;
76+ }
77+
78+ private waitingForMessages ( ) : boolean {
79+ return this . requests . length > 0 ;
80+ }
81+ }
0 commit comments