|
@@ -1,35 +1,38 @@
|
|
import { Express } from 'express';
|
|
import { Express } from 'express';
|
|
import { filter, Observable, Subject, Subscription, take } from "rxjs";
|
|
import { filter, Observable, Subject, Subscription, take } from "rxjs";
|
|
-import { ClientObject, Transport, TransportEvent, TransportMessage, TransportService } from "../interface/connector.interface";
|
|
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
import config from '../config/config.json';
|
|
import config from '../config/config.json';
|
|
import { handleClientHttpConnection, handleHttpClient, initiateClientToServer, startHttpServer } from "../utils/http.utils";
|
|
import { handleClientHttpConnection, handleHttpClient, initiateClientToServer, startHttpServer } from "../utils/http.utils";
|
|
import { WrappedMessage } from '../utils/message.ordering';
|
|
import { WrappedMessage } from '../utils/message.ordering';
|
|
import { error } from 'console';
|
|
import { error } from 'console';
|
|
import axios, { AxiosError } from 'axios';
|
|
import axios, { AxiosError } from 'axios';
|
|
-import { EventMessage } from '../interface/transport.interface';
|
|
|
|
import ConsoleLogger from '../utils/log.utils';
|
|
import ConsoleLogger from '../utils/log.utils';
|
|
|
|
+import { ClientObject, GeneralEvent, Transport, TransportMessage, TransportServiceInterface } from '../interface/interface';
|
|
|
|
|
|
-export class HttpTransportService implements TransportService {
|
|
|
|
|
|
+export class HttpTransportService implements TransportServiceInterface {
|
|
private console: ConsoleLogger = new ConsoleLogger(`HttpTransportService`, ['transport'])
|
|
private console: ConsoleLogger = new ConsoleLogger(`HttpTransportService`, ['transport'])
|
|
private baseUrl!: string;
|
|
private baseUrl!: string;
|
|
- private info: Transport = Transport.Http
|
|
|
|
|
|
+ private info: Transport = `Http`
|
|
private connectedHttpServer: ConnectedHttpServer[] = [] // to allow the possibility of having to communicate with multiple servers as a client
|
|
private connectedHttpServer: ConnectedHttpServer[] = [] // to allow the possibility of having to communicate with multiple servers as a client
|
|
private connectedHttpClients: ConnectedHttpClient[] = [] // to keep track of the all the clients that are connected
|
|
private connectedHttpClients: ConnectedHttpClient[] = [] // to keep track of the all the clients that are connected
|
|
- transportEvent!: Subject<TransportEvent>
|
|
|
|
|
|
+ transportEvent!: Subject<GeneralEvent<any>>
|
|
|
|
|
|
- constructor(event: Subject<TransportEvent>) {
|
|
|
|
|
|
+ constructor(event: Subject<GeneralEvent<any>>) {
|
|
this.baseUrl = config.connection.transmitter
|
|
this.baseUrl = config.connection.transmitter
|
|
this.transportEvent = event
|
|
this.transportEvent = event
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ subscribeForTransportEvent(): Observable<GeneralEvent<any>> {
|
|
|
|
+ throw new Error('Method not implemented.');
|
|
|
|
+ }
|
|
|
|
+
|
|
public getInfo(): Transport {
|
|
public getInfo(): Transport {
|
|
return this.info
|
|
return this.info
|
|
}
|
|
}
|
|
|
|
|
|
public emit(message: TransportMessage): void {
|
|
public emit(message: TransportMessage): void {
|
|
- let clientObj: ConnectedHttpClient | undefined = this.connectedHttpClients.find(obj => obj.id == message.target)
|
|
|
|
- let serverObj: ConnectedHttpServer | undefined = this.connectedHttpServer.find(obj => obj.id === message.target)
|
|
|
|
|
|
+ let clientObj: ConnectedHttpClient | undefined = this.connectedHttpClients.find(obj => obj.clientId == message.target)
|
|
|
|
+ let serverObj: ConnectedHttpServer | undefined = this.connectedHttpServer.find(obj => obj.clientId === message.target)
|
|
|
|
|
|
// for server usage
|
|
// for server usage
|
|
if (clientObj && clientObj.connectionState.getValue() == 'ONLINE') {
|
|
if (clientObj && clientObj.connectionState.getValue() == 'ONLINE') {
|
|
@@ -46,39 +49,35 @@ export class HttpTransportService implements TransportService {
|
|
console.error('HTTP emit error:', error.code);
|
|
console.error('HTTP emit error:', error.code);
|
|
this.transportEvent.next({
|
|
this.transportEvent.next({
|
|
id: uuidv4(),
|
|
id: uuidv4(),
|
|
|
|
+ type: 'Transport Event',
|
|
event: 'Re-Flush',
|
|
event: 'Re-Flush',
|
|
- data: {
|
|
|
|
- clientId: serverObj.id,
|
|
|
|
- payload: message
|
|
|
|
- } as EventMessage
|
|
|
|
- } as TransportEvent)
|
|
|
|
|
|
+ date: new Date(),
|
|
|
|
+ data: message,
|
|
|
|
+ transport: 'Http'
|
|
|
|
+ } as GeneralEvent<TransportMessage>)
|
|
});
|
|
});
|
|
} else {
|
|
} else {
|
|
this.console.error({ message: `Target Server is offline: Reflusing message ${(message.payload as WrappedMessage).thisMessageID}` });
|
|
this.console.error({ message: `Target Server is offline: Reflusing message ${(message.payload as WrappedMessage).thisMessageID}` });
|
|
this.transportEvent.next({
|
|
this.transportEvent.next({
|
|
id: uuidv4(),
|
|
id: uuidv4(),
|
|
|
|
+ type: 'Transport Event',
|
|
event: 'Re-Flush',
|
|
event: 'Re-Flush',
|
|
- data: {
|
|
|
|
- clientId: serverObj.id,
|
|
|
|
- payload: message
|
|
|
|
- } as EventMessage
|
|
|
|
- } as TransportEvent)
|
|
|
|
|
|
+ date: new Date(),
|
|
|
|
+ data: message,
|
|
|
|
+ transport: 'Http'
|
|
|
|
+ } as GeneralEvent<TransportMessage>)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- public subscribe(): Observable<TransportEvent> {
|
|
|
|
- return this.transportEvent.asObservable();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
public startServer(port: number): void {
|
|
public startServer(port: number): void {
|
|
startHttpServer(port).subscribe({
|
|
startHttpServer(port).subscribe({
|
|
next: (client: ConnectedHttpClient) => {
|
|
next: (client: ConnectedHttpClient) => {
|
|
handleHttpClient(client, this.connectedHttpClients).subscribe({
|
|
handleHttpClient(client, this.connectedHttpClients).subscribe({
|
|
next: event => this.transportEvent.next(event),
|
|
next: event => this.transportEvent.next(event),
|
|
error: error => console.error(error),
|
|
error: error => console.error(error),
|
|
- complete: () => (`Client ${client.id} disconnected...`)
|
|
|
|
|
|
+ complete: () => (`Client ${client.clientId} disconnected...`)
|
|
})
|
|
})
|
|
},
|
|
},
|
|
error: error => this.console.error({ message: 'Observer Error', details: error }),
|
|
error: error => this.console.error({ message: 'Observer Error', details: error }),
|