import { IDataOperator } from "src/interfaces/IDataOperator" ; import { arrayEquals } from "src/shared/ExtensionMethods" ; import { Endpoint , EndpointRef } from "./Endpoint" ; import { gRPCMode } from "./enums/gRPCMode" ; import { Protocol } from "./enums/Protocol" ; import { LogicComponent } from "./LogicComponent" ; import { Options } from "./Options" ; import { Port } from "./Port" ; import { RequestData } from "./RequestData" ; export class Client extends LogicComponent implements IDataOperator { outputPort : Port ; options : ClientOptions ; isConnectedToEndpoint : boolean = false ; connectedId : string ; color = "#0EC35B" ; constructor ( ) { super ( ) ; this . outputPort = new Port ( this , true , false ) ; this . options = new ClientOptions ( ) ; this . options . title = "Client" ; } receiveData ( data ) { this . fireReceiveData ( data ) ; if ( data . header . endpoint . endpoint . protocol == Protocol . WebSockets || data . header . endpoint . endpoint . grpcMode == gRPCMode [ "Bidirectional Streaming" ] || data . header . endpoint . endpoint . grpcMode == gRPCMode [ "Server Streaming" ] ) { if ( data . header . stream == true ) { if ( this . connectedId == null ) return ; this . isConnectedToEndpoint = true ; } else { this . isConnectedToEndpoint = false ; this . connectedId = null ; } } } async sendData ( data ) { this . updateEndpoint ( ) ; return await this . outputPort . sendData ( data ) ; } getAvailableEndpoints ( ) { let availableEndpoints = [ ] ; for ( let connection of this . outputPort . connections ) { connection . getOtherPort ( this . outputPort ) . parent . getAvailableEndpoints ( ) . forEach ( endpoint => { let duplicate = ( availableEndpoints . find ( ep => ep . url === endpoint . url && arrayEquals ( endpoint . supportedMethods , ep . supportedMethods ) ) != null ) ; if ( ! duplicate ) availableEndpoints . push ( endpoint ) ; } ) ; } return availableEndpoints ; } updateEndpoint ( ) { let availableEndpoints = this . getAvailableEndpoints ( ) ; if ( this . options . endpointRef . endpoint == null ) { if ( availableEndpoints . length == 0 ) return ; this . options . endpointRef . endpoint = availableEndpoints [ 0 ] ; this . options . endpointRef . method = availableEndpoints [ 0 ] . supportedMethods [ 0 ] ; } else { let isAvailable = false ; for ( let endpoint of availableEndpoints ) { if ( this . options . endpointRef . endpoint === endpoint ) { isAvailable = true ; break ; } } if ( ! isAvailable ) { if ( availableEndpoints . length == 0 ) return ; this . options . endpointRef . endpoint = availableEndpoints [ 0 ] ; this . options . endpointRef . method = availableEndpoints [ 0 ] . supportedMethods [ 0 ] ; } } } } export class ClientOptions extends Options { protocol : Protocol = Protocol . HTTP ; endpointRef : EndpointRef = new EndpointRef ( ) ; outputRate : number = 4 ; }