@@ -21,10 +21,9 @@ import { LogManager } from '@aliceo2/web-ui';
2121import type { Command , CommandHandler } from 'models/commands.model' ;
2222import type { DuplexMessageEvent } from '../../models/message.model' ;
2323import { ConnectionDirection } from '../../models/message.model' ;
24+ import { ConnectionStatus } from '../../models/connection.model' ;
25+ import { peerListener } from '../../utils/connection/peerListener' ;
2426
25- /**
26- * @description Manages all the connection between clients and central system.
27- */
2827/**
2928 * Manages the lifecycle and connection logic for a gRPC client communicating with the central system.
3029 *
@@ -45,6 +44,10 @@ export class ConnectionManager {
4544 private _centralConnection : CentralConnection ;
4645 private _sendingConnections = new Map < string , Connection > ( ) ;
4746 private _receivingConnections = new Map < string , Connection > ( ) ;
47+ private _wrapper : any ;
48+ private _peerCtor : any ;
49+ private _peerServer : grpc . Server | undefined ;
50+ private _baseAPIPath : string = '' ;
4851
4952 /**
5053 * Initializes a new instance of the ConnectionManager class.
@@ -64,9 +67,10 @@ export class ConnectionManager {
6467 } ) ;
6568
6669 const proto = grpc . loadPackageDefinition ( packageDef ) as any ;
67- const wrapper = proto . webui . tokenization ;
70+ this . _wrapper = proto . webui . tokenization ;
71+ this . _peerCtor = this . _wrapper . Peer2Peer ;
6872
69- const client = new wrapper . CentralSystem ( centralAddress , grpc . credentials . createInsecure ( ) ) ;
73+ const client = new this . _wrapper . CentralSystem ( centralAddress , grpc . credentials . createInsecure ( ) ) ;
7074
7175 // Event dispatcher for central system events
7276 this . _centralDispatcher = new CentralCommandDispatcher ( ) ;
@@ -109,13 +113,15 @@ export class ConnectionManager {
109113 * @param token Optional token for connection
110114 */
111115 createNewConnection ( address : string , direction : ConnectionDirection , token ?: string ) {
112- const conn = new Connection ( token ?? '' , address , direction ) ;
116+ const conn = new Connection ( token ?? '' , address , direction , this . _peerCtor ) ;
113117
114118 if ( direction === ConnectionDirection . RECEIVING ) {
115119 this . _receivingConnections . set ( address , conn ) ;
116120 } else {
117121 this . _sendingConnections . set ( address , conn ) ;
118122 }
123+ conn . status = ConnectionStatus . CONNECTED ;
124+ this . _logger . infoMessage ( `Connection with ${ address } has been estabilished. Status: ${ conn . status } ` ) ;
119125
120126 return conn ;
121127 }
@@ -150,4 +156,26 @@ export class ConnectionManager {
150156 receiving : [ ...this . _receivingConnections . values ( ) ] ,
151157 } ;
152158 }
159+
160+ /** Starts a listener server for p2p connections */
161+ public async listenForPeers ( port : number , baseAPIPath ?: string ) : Promise < void > {
162+ if ( baseAPIPath ) this . _baseAPIPath = baseAPIPath ;
163+
164+ if ( this . _peerServer ) {
165+ this . _peerServer . forceShutdown ( ) ;
166+ this . _peerServer = undefined ;
167+ }
168+
169+ this . _peerServer = new grpc . Server ( ) ;
170+ this . _peerServer . addService ( this . _wrapper . Peer2Peer . service , {
171+ Fetch : async ( call : grpc . ServerUnaryCall < any , any > , callback : grpc . sendUnaryData < any > ) =>
172+ peerListener ( call , callback , this . _logger , this . _receivingConnections , this . _peerCtor , this . _baseAPIPath ) ,
173+ } ) ;
174+
175+ await new Promise < void > ( ( resolve , reject ) => {
176+ this . _peerServer ?. bindAsync ( `localhost:${ port } ` , grpc . ServerCredentials . createInsecure ( ) , ( err ) => ( err ? reject ( err ) : resolve ( ) ) ) ;
177+ } ) ;
178+
179+ this . _logger . infoMessage ( `Peer server listening on localhost:${ port } ` ) ;
180+ }
153181}
0 commit comments