|
@@ -1,98 +1,64 @@
|
|
-/* This is to emulate another remote process also using socket io to connect.
|
|
|
|
-TEST: to see if it performs the necessary self check to identify itself, as well as
|
|
|
|
-receiving all the notification and response messages */
|
|
|
|
-
|
|
|
|
-// Import the necessary modules
|
|
|
|
-import { io, Socket } from "socket.io-client";
|
|
|
|
-import { handleClientSocketConnection } from "../utils/socket.utils";
|
|
|
|
-import { ConnectedServerSocket } from "../transport/websocket";
|
|
|
|
|
|
+import { filter, map, Observable, Observer, Subject } from "rxjs";
|
|
|
|
+import { Bus, FisMessage, MessageTransmission } from "../interface/transport.interface";
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
import { v4 as uuidv4 } from 'uuid'
|
|
-import { filter, interval, map, Observable, Observer, Subject, Subscription, takeWhile } from "rxjs";
|
|
|
|
-import { TransportEvent } from "../interface/connector.interface";
|
|
|
|
-import { EventMessage, FisMessage } from "../interface/transport.interface";
|
|
|
|
-import { checkMessage, WrappedMessage } from "../utils/message.ordering";
|
|
|
|
-import { RetransmissionService } from "../utils/retransmission.service";
|
|
|
|
|
|
+import { MessageTransmissionManager } from "../transmission/msg.transmission.manager";
|
|
|
|
+import { TransportEvent, TransportMessage } from "../interface/connector.interface";
|
|
|
|
+import { WrappedMessage } from "../utils/message.ordering";
|
|
|
|
|
|
-class SocketClient {
|
|
|
|
- private currentSocketId!: string
|
|
|
|
- private socket!: Socket;
|
|
|
|
- private connectedServerSocket: ConnectedServerSocket[] = []
|
|
|
|
- private requestMessages: Subject<any> = new Subject()
|
|
|
|
|
|
+class Supervisor {
|
|
|
|
+ private isClient: boolean = true
|
|
|
|
+ private transmissionManager!: MessageTransmissionManager
|
|
private event: Subject<TransportEvent> = new Subject()
|
|
private event: Subject<TransportEvent> = new Subject()
|
|
- private retransmission: RetransmissionService = new RetransmissionService()
|
|
|
|
|
|
+ private transmissionSets: MessageTransmission[] = []
|
|
|
|
|
|
- constructor(url: string) {
|
|
|
|
- this.setUpClientServerConnection(url)
|
|
|
|
- }
|
|
|
|
|
|
+ constructor() {
|
|
|
|
+ this.transmissionManager = new MessageTransmissionManager(this.event, this.isClient)
|
|
|
|
|
|
- private setUpClientServerConnection(url: string) {
|
|
|
|
- // Connect to the serve
|
|
|
|
- this.socket = io(url);
|
|
|
|
- // use the existing socket handler
|
|
|
|
- handleClientSocketConnection(this.socket, this.connectedServerSocket).subscribe(this.event)
|
|
|
|
- this.startListening(this.event)
|
|
|
|
- }
|
|
|
|
|
|
+ this.transmissionManager.subscribe().subscribe((transmissionSet: MessageTransmission) => {
|
|
|
|
+ this.transmissionSets.push(transmissionSet)
|
|
|
|
|
|
- private startListening(event: Subject<TransportEvent>): void {
|
|
|
|
- event.subscribe((event: TransportEvent) => {
|
|
|
|
- console.log('Event', (((event.data as EventMessage)?.payload as WrappedMessage)?.payload as FisMessage)?.header.messageID ?? 'Not Fis Message')
|
|
|
|
- if (event.event == `New Server`) {
|
|
|
|
- this.currentSocketId = (event.data as EventMessage).clientId
|
|
|
|
-
|
|
|
|
- let currentClientSocket: ConnectedServerSocket | undefined = this.connectedServerSocket.find(obj => obj.id === this.currentSocketId)
|
|
|
|
- if (currentClientSocket) {
|
|
|
|
- // so retransmission is working as usual
|
|
|
|
- this.retransmission.implementRetransmission(this.requestMessages, currentClientSocket.connectionState, true)
|
|
|
|
- // this.startGeneratingRequest(10000, this.requestMessages)
|
|
|
|
- this.retransmission.returnSubjectForBufferedItems().subscribe((message: WrappedMessage) => {
|
|
|
|
- this.sendMessage(message).subscribe({
|
|
|
|
- next: response => console.log(`Receiving response for ${message.thisMessageID}`),
|
|
|
|
- complete: () => console.log(`Request Completed for ${message.thisMessageID}`)
|
|
|
|
- })
|
|
|
|
- })
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+ this.handleActivity(transmissionSet)
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
- private startGeneratingRequest(intervalDuration: number, requestsPipe: Subject<FisMessage>) {
|
|
|
|
- interval(intervalDuration).subscribe(time => {
|
|
|
|
- let message: FisMessage = {
|
|
|
|
- header: {
|
|
|
|
- messageID: uuidv4(),
|
|
|
|
- messageName: 'RequestMessage'
|
|
|
|
- },
|
|
|
|
- data: 'Data'
|
|
|
|
- }
|
|
|
|
- requestsPipe.next(message)
|
|
|
|
|
|
+ // only called once for each connected clients.
|
|
|
|
+ private handleActivity(messageTransmission: MessageTransmission): void {
|
|
|
|
+ // start listening to incoming messages from this client
|
|
|
|
+ messageTransmission.receiver.getMessageBus(Bus.GeneralBus).subscribe((event: TransportEvent) => {
|
|
|
|
+ console.log(`General Bus`, event)
|
|
})
|
|
})
|
|
- }
|
|
|
|
|
|
|
|
- private sendMessage(message: WrappedMessage): Observable<WrappedMessage> {
|
|
|
|
- return new Observable((response: Observer<WrappedMessage>) => {
|
|
|
|
- console.log(`Emitting: ${(message.payload as FisMessage).header.messageID}`)
|
|
|
|
- this.socket.emit('message', message)
|
|
|
|
|
|
+ let request: FisMessage = {
|
|
|
|
+ header: {
|
|
|
|
+ messageID: uuidv4(),
|
|
|
|
+ messageName: 'RequestMessage'
|
|
|
|
+ },
|
|
|
|
+ data: 'Data'
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // this.request(request, messageTransmission).subscribe({
|
|
|
|
+ // next: res => console.log(res),
|
|
|
|
+ // complete: () => console.log(`Responses Completed for request: ${request.header.messageID}`)
|
|
|
|
+ // })
|
|
|
|
+ }
|
|
|
|
|
|
- let eventSubscription: Subscription = this.event.pipe(
|
|
|
|
|
|
+ private request(request: FisMessage, messageTransmission: MessageTransmission): Observable<any> {
|
|
|
|
+ return new Observable((response: Observer<any>) => {
|
|
|
|
+ messageTransmission.transmitter.emit(request)
|
|
|
|
+ messageTransmission.receiver.getMessageBus(Bus.GeneralBus).pipe(
|
|
filter(event => event.event == 'New Message'),
|
|
filter(event => event.event == 'New Message'),
|
|
- filter(event => (((event.data as EventMessage).payload as WrappedMessage).payload as FisMessage).header.messageID === (message.payload as FisMessage).header.messageID),
|
|
|
|
- // takeWhile(event => (((event.data as EventMessage).payload as WrappedMessage).payload as FisMessage).data != 'Complete'),
|
|
|
|
- map(event => ((event.data as EventMessage).payload as WrappedMessage)),
|
|
|
|
- ).subscribe((message: WrappedMessage) => {
|
|
|
|
- response.next(message)
|
|
|
|
- if ((message.payload as FisMessage).data == 'Complete') {
|
|
|
|
- eventSubscription.unsubscribe()
|
|
|
|
|
|
+ filter(event => (((event.data as TransportMessage)?.payload as WrappedMessage)?.payload as FisMessage)?.header.messageID === request.header.messageID),
|
|
|
|
+ map(event => (((event.data as TransportMessage)?.payload as WrappedMessage)?.payload as FisMessage))
|
|
|
|
+ ).subscribe(message => {
|
|
|
|
+ if (message.data == 'Complete') {
|
|
response.complete()
|
|
response.complete()
|
|
|
|
+ } else {
|
|
|
|
+ response.next(message)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
})
|
|
})
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
|
|
-// Usage example:
|
|
|
|
-const client = new SocketClient("http://localhost:3001");
|
|
|
|
-// const client = new SocketClient("http://localhost:3002");
|
|
|
|
-// const client = new SocketClient("http://127.0.0.1:3000");
|
|
|
|
-// const client = new SocketClient("http://192.168.100.96:3000");
|
|
|
|
|
|
+}
|
|
|
|
|
|
-// Send a message
|
|
|
|
|
|
+let supervisor = new Supervisor()
|