@@ -8,7 +8,7 @@ import { WebRTCConnection } from 'net/transport/WebRTCConnection';
88import { RNGImpl } from 'crypto/random' ;
99import { SignallingServerConnection } from 'net/linkup/SignallingServerConnection' ;
1010import { WebSocketConnection } from 'net/transport/WebSocketConnection' ;
11- import { LinkupManagerHost , LinkupManagerEvent } from 'net/linkup' ;
11+ import { LinkupManagerHost , LinkupManagerEvent , NewCallMessageCallback } from 'net/linkup' ;
1212import { WebRTCConnectionCommand , WebRTCConnectionEvent , WebRTCConnectionProxy } from 'net/transport' ;
1313import { Identity } from 'data/identity' ;
1414
@@ -78,6 +78,7 @@ type ConnectionInfo = {
7878 localEndpoint : Endpoint ,
7979 remoteEndpoint : Endpoint ,
8080 connId : ConnectionId ,
81+ remoteInstanceId ?: string , // see the note in instanceIds in class SignallingServerConnection
8182 status : ConnectionStatus ,
8283 timestamp : number ,
8384 requestedBy : Set < AgentId >
@@ -121,13 +122,13 @@ class NetworkAgent implements Agent {
121122 connections : Map < ConnectionId , Connection > ;
122123
123124 connectionInfo : Map < ConnectionId , ConnectionInfo > ;
124- deferredInitialMessages : Map < ConnectionId , Array < any > > ;
125+ deferredInitialMessages : Map < ConnectionId , Array < { instanceId : string , message : any } > > ;
125126
126127 messageCallback : ( data : any , conn : Connection ) => void ;
127128
128129 connectionReadyCallback : ( conn : Connection ) => void ;
129130
130- newConnectionRequestCallback : ( sender : LinkupAddress , receiver : LinkupAddress , callId : string , message : any ) => void ;
131+ newConnectionRequestCallback : NewCallMessageCallback ;
131132
132133 linkupMessageCallback : ( sender : LinkupAddress , receiver : LinkupAddress , message : any ) => void ;
133134
@@ -209,6 +210,9 @@ class NetworkAgent implements Agent {
209210 if ( connInfo . status !== ConnectionStatus . Ready ) {
210211 this . connections . set ( connectionId , conn ) ;
211212 connInfo . status = ConnectionStatus . Ready ;
213+ if ( connInfo . remoteInstanceId === undefined ) {
214+ connInfo . remoteInstanceId = conn . remoteInstanceId ;
215+ }
212216 const ev : ConnectionStatusChangeEvent = {
213217 type : NetworkEventType . ConnectionStatusChange ,
214218 content : {
@@ -227,7 +231,7 @@ class NetworkAgent implements Agent {
227231 }
228232 }
229233
230- this . newConnectionRequestCallback = ( sender : LinkupAddress , receiver : LinkupAddress , connectionId : string , message : any ) => {
234+ this . newConnectionRequestCallback = ( sender : LinkupAddress , receiver : LinkupAddress , connectionId : string , instanceId : string , message : any ) => {
231235
232236 let connInfo = this . connectionInfo . get ( connectionId ) ;
233237
@@ -237,7 +241,8 @@ class NetworkAgent implements Agent {
237241 connInfo = {
238242 localEndpoint : receiver . url ( ) ,
239243 remoteEndpoint : sender . url ( ) ,
240- connId : connectionId ,
244+ connId : connectionId ,
245+ remoteInstanceId : instanceId ,
241246 status : ConnectionStatus . Received ,
242247 timestamp : Date . now ( ) ,
243248 requestedBy : new Set ( )
@@ -246,13 +251,22 @@ class NetworkAgent implements Agent {
246251 this . connectionInfo . set ( connectionId , connInfo ) ;
247252 }
248253
254+ /*if (connInfo.localEndpoint === receiver.url() &&
255+ connInfo.remoteEndpoint === sender.url() &&
256+ connInfo.remoteEndpoint !== instanceId) {
257+
258+ console.log('MISMATCH')
259+ CONSOL
260+ }*/
261+
249262 if ( connInfo . localEndpoint === receiver . url ( ) &&
250- connInfo . remoteEndpoint === sender . url ( ) ) {
263+ connInfo . remoteEndpoint === sender . url ( ) &&
264+ connInfo . remoteInstanceId === instanceId ) {
251265
252266 if ( connInfo . status === ConnectionStatus . Establishing ) {
253- this . acceptReceivedConnectionMessages ( connectionId , message ) ;
267+ this . acceptReceivedConnectionMessages ( connectionId , instanceId , message ) ;
254268 } else if ( connInfo . status === ConnectionStatus . Received ) {
255- this . deferReceivedConnectionMessage ( connectionId , message ) ;
269+ this . deferReceivedConnectionMessage ( connectionId , instanceId , message ) ;
256270
257271 if ( isNew ) {
258272 let ev : ConnectionStatusChangeEvent = {
@@ -389,26 +403,27 @@ class NetworkAgent implements Agent {
389403 }
390404 */
391405
392- private acceptReceivedConnectionMessages ( connId : ConnectionId , message ?: any ) {
406+ private acceptReceivedConnectionMessages ( connId : ConnectionId , instanceId ?: string , message ?: any ) {
393407
394408 let messages = this . deferredInitialMessages . get ( connId ) ;
395409
396410 if ( messages === undefined ) {
397411 messages = [ ] ;
398412 }
399413
400- if ( message !== undefined ) {
401- messages . push ( message ) ;
414+ if ( message !== undefined && instanceId !== undefined ) {
415+ messages . push ( { instanceId : instanceId , message : message } ) ;
402416 }
403417
404418
405- for ( const message of messages ) {
419+ for ( const { message, instanceId } of messages ) {
406420 let conn = this . connections . get ( connId ) ;
407421
408422 if ( conn === undefined ) {
409423 let connInfo = this . connectionInfo . get ( connId ) as ConnectionInfo ;
410424
411425 if ( connInfo !== undefined ) {
426+
412427 const receiver = LinkupAddress . fromURL ( connInfo . localEndpoint ) ;
413428 const sender = LinkupAddress . fromURL ( connInfo . remoteEndpoint ) ;
414429
@@ -434,13 +449,13 @@ class NetworkAgent implements Agent {
434449
435450 if ( conn instanceof WebRTCConnection || conn instanceof WebRTCConnectionProxy || conn instanceof WebSocketConnection ) {
436451 conn . setMessageCallback ( this . messageCallback ) ;
437- conn . answer ( message ) ;
452+ conn . answer ( instanceId , message ) ;
438453 }
439454 } else {
440455 if ( conn instanceof WebRTCConnection || conn instanceof WebRTCConnectionProxy ) {
441- conn . receiveSignallingMessage ( message ) ;
456+ conn . receiveSignallingMessage ( instanceId , message ) ;
442457 } else if ( conn instanceof WebSocketConnection ) {
443- conn . answer ( message ) ;
458+ conn . answer ( instanceId , message ) ;
444459 }
445460 }
446461
@@ -450,16 +465,16 @@ class NetworkAgent implements Agent {
450465 }
451466 }
452467
453- private deferReceivedConnectionMessage ( connId : ConnectionId , message : any ) {
468+ private deferReceivedConnectionMessage ( connId : ConnectionId , instanceId : string , message : any ) {
454469
455470 let messages = this . deferredInitialMessages . get ( connId ) ;
456471
457472 if ( messages === undefined ) {
458- messages = new Array < any > ( ) ;
473+ messages = new Array < { instanceId : string , message : any } > ( ) ;
459474 this . deferredInitialMessages . set ( connId , messages ) ;
460475 }
461476
462- messages . push ( message ) ;
477+ messages . push ( { message : message , instanceId : instanceId } ) ;
463478 }
464479
465480 // Network listen, shutdown
@@ -534,7 +549,8 @@ class NetworkAgent implements Agent {
534549 {
535550 localEndpoint : local ,
536551 remoteEndpoint : remote ,
537- connId : callId , status : ConnectionStatus . Establishing ,
552+ connId : callId ,
553+ status : ConnectionStatus . Establishing ,
538554 timestamp : Date . now ( ) ,
539555 requestedBy : new Set ( [ requestedBy ] )
540556 } ) ;
0 commit comments