Browse Source

simple test connection established. Milestone

Enzo 1 week ago
parent
commit
a695713fe3

+ 1 - 1
.env

@@ -1,2 +1,2 @@
-Transport = "WEBSOCKET"
+Transport = "Websocket"
 PORT = 3000

+ 3 - 0
468076d5-1a00-4861-b6c9-8e369103dc8e.json

@@ -0,0 +1,3 @@
+{
+  "id": "468076d5-1a00-4861-b6c9-8e369103dc8e"
+}

+ 3 - 0
5157e5f6-291e-4464-8cf8-9cd19f624b30.json

@@ -0,0 +1,3 @@
+{
+  "id": "5157e5f6-291e-4464-8cf8-9cd19f624b30"
+}

+ 3 - 0
ecd7a4ef-420e-48d4-be15-8bae649cd9e6.json

@@ -0,0 +1,3 @@
+{
+  "id": "ecd7a4ef-420e-48d4-be15-8bae649cd9e6"
+}

+ 4 - 7
src/connector/connector.base.ts

@@ -10,16 +10,16 @@ export class ConnectionAdapter implements ConnectionAdaptorBase {
     event!: Subject<TransportEvent>
     connector: any;
     connectorProfile!: AdapterProfile;
-    connectionStateBus!: BehaviorSubject<ConnectionState>;
+    connectionStateBus!: Subject<ConnectionState>;
     adaptorTransmissionRole!: AdaptorTransmissionRole;
 
-    constructor(port?: number, url?: string) {
-        if (!port && !url) console.error(`No role has assigned for transport service due to lack of information provided!`)
+    constructor() {
+        //logic here
     }
     setAdapterProfile(adapterId: string): void {
         throw new Error("Method not implemented.")
     }
-    getInfo(): any {
+    getInfo(): AdapterProfile {
         throw new Error("Method not implemented.");
     }
     subscribeConnectionState(): Observable<ConnectionState> {
@@ -37,9 +37,6 @@ export class ConnectionAdapter implements ConnectionAdaptorBase {
     getMessageBus(bus: Bus): Observable<any> {
         throw new Error("Method not implemented.");
     }
-    subscribeMessages(messageFilter: any): Observable<any> {
-        throw new Error("Method not implemented.");
-    }
 
 
 }

+ 29 - 33
src/connector/connector.manager.ts

@@ -1,6 +1,6 @@
 import { v4 as uuidv4 } from 'uuid'
 import dotenv from 'dotenv';
-import { TransportEvent, ConnectionManager as ConnectionManagerInterface, AdapterSet, Transport, TransportService } from "../interface/connector.interface"
+import { TransportEvent, ConnectionManager as ConnectionManagerInterface, AdapterSet, Transport, TransportService, ClientObject } from "../interface/connector.interface"
 import { TransmitterConnectionAdapter } from './connector.transmitter'
 import { ReceiverConnectionAdapter } from './connector.receiver'
 import { EventMessage } from '../interface/transport.interface'
@@ -18,13 +18,14 @@ export class ConnectionManager implements ConnectionManagerInterface {
     adapterSet: AdapterSet[] = []
 
     constructor() {
+        console.log(`Connection Manager: Contructing ConnectionManager....`)
         // logic here
-        this.setUpServer(process.env.PORT as unknown as number, this.getTransportService(process.env.Transport as unknown as Transport))
+        this.getTransportService(process.env.Transport as unknown as Transport)
+        this.setUpServer(process.env.PORT as unknown as number)
 
         // Everything that happens will go through here. Can also plug in logging service here at a later time
         this.transportEvent.subscribe((event: TransportEvent) => {
             console.log(`TranpsortEvent: `, event)
-
             if (event.event == 'New Client') {
                 this.handleNewClient((event.data as EventMessage).payload) // payload is connectedclientInstance
             }
@@ -40,47 +41,41 @@ export class ConnectionManager implements ConnectionManagerInterface {
         }
     }
 
-    getTransportService(transportType: Transport): any {
-        console.log(`Instanting ${transportType} tranport service to be used.`)
-        if (transportType == Transport.Websocket) {
+    getTransportService(transportType: Transport): void {
+        console.log(`Instantiating ${transportType} tranport service to be used.`)
+        if (transportType === Transport.Websocket) {
             this.transportService = new WebsocketTransportService()
-            this.transportEvent.next({
-                id: uuidv4(),
-                event: `New Transport`,
-                data: {
-                    message: `Setting up Websocket Transport Service`
-                }
-            })
+            this.triggerTransportEvent(`Setting up Websocket Transport Service`)
         }
-        if (transportType == Transport.Http) {
+        if (transportType === Transport.Http) {
             this.transportService = new HttpTransportService()
-            this.transportEvent.next({
-                id: uuidv4(),
-                event: `New Transport`,
-                data: {
-                    message: `Setting up Http Transport Service`
-                }
-            })
+            this.triggerTransportEvent(`Setting up Http Transport Service`)
         }
     }
 
-    // Server to be set up as well as acquiring client information if needed. Like in the case for grpc and socket. Http not required.
-    private setUpServer(port: number, transportService: TransportService): void {
-        if ((transportService as TransportService).getInfo().transport == Transport.Websocket) {
-            (transportService as WebsocketTransportService).startServer(port);
-            // this doesn't mean all the responses come through here, but rather to record and assign new instances based on the new clinets connected
-            (transportService as WebsocketTransportService).getTransportEvent().subscribe(this.transportEvent)
-        }
+    private triggerTransportEvent(message: string): void {
+        this.transportEvent.next({
+            id: uuidv4(),
+            event: `New Transport`,
+            data: { message }
+        });
+    }
 
-        if ((transportService as TransportService).getInfo().transport == Transport.Http) {
-            (transportService as HttpTransportService).startServer(port)
-            //  To be Enhanced at a later time
+    // Server to be set up as well as acquiring client information if needed. Like in the case for grpc and socket. Http not requ`ired.
+    private setUpServer(port: number): void {
+        if (this.transportService instanceof WebsocketTransportService) {
+            this.transportService.startServer(port);
+            this.transportService.getTransportEvent().subscribe(this.transportEvent);
+        } else if (this.transportService instanceof HttpTransportService) {
+            this.transportService.startServer(port);
+            this.transportService.getTransportEvent().subscribe(this.transportEvent);
+            // Additional Http-specific setup if needed.
         }
     }
 
-    private handleNewClient(clientInstance: any): void {
+    private handleNewClient(clientInstance: ClientObject): void {
         // need to setup the adapters to be called by the transmission manager
-        let transmitter: TransmitterConnectionAdapter = new TransmitterConnectionAdapter(this.transportService, clientInstance.id, this.transportEvent)
+        let transmitter: TransmitterConnectionAdapter = new TransmitterConnectionAdapter(this.transportService, clientInstance.id, this.transportEvent, clientInstance.connectionState.asObservable())
         let receiver: ReceiverConnectionAdapter = new ReceiverConnectionAdapter(this.transportService, clientInstance.id, this.transportEvent)
         let requestResponse: RequestResponseConnectionAdapter = new RequestResponseConnectionAdapter(transmitter, receiver, clientInstance.id)
         let adapterSet: AdapterSet = {
@@ -98,6 +93,7 @@ export class ConnectionManager implements ConnectionManagerInterface {
             data: {
                 adapterId: adapterSet.id,
                 message: `New Adapter Set${adapterSet.id} has been added`,
+                payload: adapterSet
             } as EventMessage
         })
     }

+ 12 - 3
src/connector/connector.receiver.ts

@@ -1,7 +1,7 @@
 import dotenv from 'dotenv';
 import { Bus, FisAppActor, FisMessage } from "../interface/transport.interface";
 import { ConnectionAdapter } from "./connector.base";
-import { AdaptorTransmissionRole, ConnectionState, ReceiverConnectionAdapter as ReceiverConnectionAdapterInterface, TransportEvent, TransportMessage, TransportService } from "../interface/connector.interface";
+import { AdapterProfile, AdaptorTransmissionRole, ConnectionState, ReceiverConnectionAdapter as ReceiverConnectionAdapterInterface, TransportEvent, TransportMessage, TransportService } from "../interface/connector.interface";
 import { BehaviorSubject, filter, map, Observable, Observer, Subject, Subscription, takeWhile } from 'rxjs';
 import { v4 as uuidv4 } from 'uuid'
 
@@ -21,6 +21,7 @@ export class ReceiverConnectionAdapter extends ConnectionAdapter implements Rece
         this.setAdapterProfile(adapterId)
     }
 
+
     setAdapterProfile(adapterId: string): void {
         this.connectorProfile = {
             id: adapterId,
@@ -35,7 +36,7 @@ export class ReceiverConnectionAdapter extends ConnectionAdapter implements Rece
                 const subscription: Subscription = this.event.pipe(
                     filter((message: TransportEvent) => message.event === 'New Message'),
                     // take message only specific for this adapter. Although that itself wouldn't be necessary, considerng everything goes through transportEvent. I guess it's for better management
-                    filter((message:TransportEvent) => (message.data as TransportMessage).target == this.connectorProfile.id), 
+                    filter((message: TransportEvent) => (message.data as TransportMessage).target == this.connectorProfile.id),
                     takeWhile((message: TransportEvent) => {
                         const shouldTake = ((message.data as TransportMessage).payload as FisMessage).data !== 'Complete';
                         if (!shouldTake) {
@@ -47,7 +48,7 @@ export class ReceiverConnectionAdapter extends ConnectionAdapter implements Rece
                 ).subscribe((message: TransportMessage) => {
                     observable.next(message);
                 });
-    
+
                 // Clean up on unsubscription
                 return () => {
                     subscription.unsubscribe();
@@ -64,6 +65,14 @@ export class ReceiverConnectionAdapter extends ConnectionAdapter implements Rece
             }
         });
     }
+
+    getInfo(): AdapterProfile {
+        return this.connectorProfile
+    }
+
+    subscribeMessages(messageFilter: any): Observable<any> {
+        throw new Error('Method not implemented.');
+    }
 }
 
 

+ 14 - 9
src/connector/connector.request.response.ts

@@ -20,7 +20,6 @@ export class RequestResponseConnectionAdapter extends ConnectionAdapter implemen
         this.receiverAdapter = receiverAdapter
         this.setAdapterProfile(adapterId)
     }
-    
     setAdapterProfile(adapterId: string): void {
         this.connectorProfile = {
             id: adapterId,
@@ -28,14 +27,8 @@ export class RequestResponseConnectionAdapter extends ConnectionAdapter implemen
             adapter: this.connector
         }    
     }
-
-    emit(message: TransmissionMessage): void {
-        throw new Error('Method not implemented.');
-    }
-    emitStream(message: TransmissionMessage): void {
-        throw new Error('Method not implemented.');
-    }
-
+    
+    
     send(message: TransmissionMessage): Observable<TransportMessage> {
         return new Observable((response: Observer<TransportMessage>) => {
             // logic here
@@ -63,6 +56,18 @@ export class RequestResponseConnectionAdapter extends ConnectionAdapter implemen
         })
     }
   
+
+    /* Extended from both transmitter and receiver */
+    subscribeMessages(messageFilter: any): Observable<any> {
+        throw new Error('Method not implemented.');
+    }
+    
+    emit(message: TransmissionMessage): void {
+        throw new Error('Method not implemented.');
+    }
+    emitStream(message: TransmissionMessage): void {
+        throw new Error('Method not implemented.');
+    }
 }
 
 

+ 8 - 3
src/connector/connector.transmitter.ts

@@ -1,7 +1,7 @@
 import dotenv from 'dotenv';
 import { FisAppActor, FisMessage, TransmissionMessage } from "../interface/transport.interface";
 import { ConnectionAdapter } from "./connector.base";
-import { AdaptorTransmissionRole, ConnectionState, TransmitterConnectionAdapter as TransmitterConnectionAdapterInterface, Transport, TransportEvent, TransportMessage, TransportService } from '../interface/connector.interface';
+import { AdapterProfile, AdaptorTransmissionRole, ConnectionState, TransmitterConnectionAdapter as TransmitterConnectionAdapterInterface, Transport, TransportEvent, TransportMessage, TransportService } from '../interface/connector.interface';
 import { BehaviorSubject, Observable, Observer, Subject } from 'rxjs';
 import { v4 as uuidv4 } from 'uuid'
 
@@ -9,14 +9,15 @@ dotenv.config();
 /* This transport manager will be instantiating the necessary transport to deal with tranmission and receiving from different receivers
 So how?: */
 export class TransmitterConnectionAdapter extends ConnectionAdapter implements TransmitterConnectionAdapterInterface {
-    connectionStateBus: BehaviorSubject<ConnectionState> = new BehaviorSubject('OFFLINE' as ConnectionState) // this cannot work, because there will be alot of clients connected presumably
+    connectionStateBus: Subject<ConnectionState> = new Subject() 
 
-    constructor(transportService: TransportService, adapterId: string, event: Subject<TransportEvent>) {
+    constructor(transportService: TransportService, adapterId: string, event: Subject<TransportEvent>, connectionState: Observable<ConnectionState>) {
         super()
         // logic here
         this.event = event
         this.connector = transportService
         this.setAdapterProfile(adapterId)
+        connectionState.subscribe(this.connectionStateBus)
     }
 
     setAdapterProfile(adapterId: string): void {
@@ -40,6 +41,10 @@ export class TransmitterConnectionAdapter extends ConnectionAdapter implements T
             payload: message.payload
         } as TransportMessage)
     }
+
+    getInfo(): AdapterProfile {
+        return this.connectorProfile
+    }
     
 }
 

+ 14 - 8
src/interface/connector.interface.ts

@@ -13,7 +13,7 @@ export type TYPE = {
 export interface AdapterProfile {
     id: string,
     adapter: ConnectionAdapter,
-    connectionState: BehaviorSubject<ConnectionState>
+    connectionState: Subject<ConnectionState>
 }
 
 
@@ -30,7 +30,7 @@ export interface ConnectionManager {
 export interface ConnectionAdaptorBase {
     connector: TransportService // this one will refer to the actual tranpsort service like websocket and so on
     connectorProfile: AdapterProfile
-    connectionStateBus: BehaviorSubject<ConnectionState>
+    connectionStateBus: Subject<ConnectionState>
     adaptorTransmissionRole: AdaptorTransmissionRole
     event: Subject<TransportEvent>
 
@@ -39,7 +39,7 @@ export interface ConnectionAdaptorBase {
     connect(): void
     disconnect(): void
     getMessageBus(bus: Bus): Observable<any>
-    getInfo(): any
+    getInfo(): AdapterProfile
     setAdapterProfile(id: string): void
 }
 
@@ -65,9 +65,9 @@ export enum AdaptorTransmissionRole {
 }
 
 export enum Transport {
-    Websocket,
-    Grpc,
-    Http
+    Websocket = `Websocket`,
+    Grpc = `Grpc`,
+    Http = `Http`
 }
 
 // TO be used for transmission at the trasport level
@@ -86,7 +86,7 @@ export interface TransportEvent {
 }
 
 export interface TransportService {
-    getInfo(): any
+    getInfo(): Transport
     emit(message: TransportMessage): void
     subscribe(): Observable<TransportEvent> //all messages and whatever event will go through this, easier to implemnet across different transport protocol
 }
@@ -103,4 +103,10 @@ export interface AdapterSet {
     transmitterAdapter: TransmitterConnectionAdapter,
     receiverAdapter: ReceiverConnectionAdapter,
     requestResponsAdapter: RequestResponseConnectionAdapter
-}
+}
+
+export interface ClientObject {
+    id: string,
+    dateCreated: Date,
+    connectionState: BehaviorSubject<ConnectionState>
+}

+ 12 - 32
src/test/receiver.ts

@@ -4,46 +4,27 @@ receiving all the notification and response messages */
 
 // Import the necessary modules
 import { io, Socket } from "socket.io-client";
-import { checkOwnClientInfo } from "../utils/socket.utils";
+import { handleClientSocketConnection } from "../utils/socket.utils";
+import { ConnectedServerSocket } from "../transport/websocket";
+import { v4 as uuidv4 } from 'uuid'
+import { Subject } from "rxjs";
+import { TransportEvent } from "../interface/connector.interface";
 
 class SocketClient {
     private socket: Socket;
-    private receiverProfile!: any
+    private connectedServerSocket: ConnectedServerSocket[] = []
+    private event: Subject<TransportEvent> = new Subject()
 
     constructor(url: string) {
         // Connect to the serve
         this.socket = io(url);
-
-        // Setup event listeners
-        this.socket.on("connect", () => {
-            console.log("Connected to server with ID:", this.socket.id);
-        });
-
-        if (this.receiverProfile) {
-            checkOwnClientInfo(this.receiverProfile.id).then((data) => {
-                this.socket.emit('profile', { name: 'Existing Profile',  data: data })
-            })
-        } else {
-            this.socket.emit('profile', { name: 'New Profile', data: null })
-        }
-
-        this.socket.on(`profile`, (info) => {
-            // logic here
-        })
-
-
-        this.socket.on("message", (data: any) => {
-            console.log("Received message:", data);
-        });
-
-        this.socket.on("disconnect", () => {
-            console.log("Disconnected from server");
-        });
+        // use the existing socket handler
+        handleClientSocketConnection(this.socket, this.connectedServerSocket).subscribe(this.event)
+        this.startListening(this.event)
     }
 
-    // Emit a message to the server
-    public sendMessage(event: string, message: any) {
-        this.socket.emit(event, message);
+    private startListening(event: Subject<TransportEvent>): void {
+        event.subscribe((event: TransportEvent) => console.log(event))
     }
 }
 
@@ -51,4 +32,3 @@ class SocketClient {
 const client = new SocketClient("http://localhost:3000");
 
 // Send a message
-client.sendMessage("message", "Hello, server!");

+ 1 - 2
src/test/transmitter.ts

@@ -2,8 +2,6 @@ import { filter, interval, map, Observable, Observer, Subject, take } from "rxjs
 import { Bus, EventMessage, FisMessage, MessageTransmission, TransmissionMessage } from "../interface/transport.interface";
 import { v4 as uuidv4 } from 'uuid'
 import { MessageTransmissionManager } from "../transmission/msg.transmission.manager";
-import { error } from "console";
-import e from "express";
 import { TransportEvent } from "../interface/connector.interface";
 
 /*  These are the purple fonts. Gonna interact with blue fonts to set up the credentials to establish the necessary roles.
@@ -14,6 +12,7 @@ class Application {
     generalNotification: Subject<FisMessage> = new Subject()
 
     constructor() {
+        console.log(`Contructing Application....`)
         this.messageTransmissionManager = new MessageTransmissionManager()
         this.transmissionInstance = this.messageTransmissionManager.getTransmissionInstance()
 

+ 1 - 1
src/transmission/msg.transmission.base.ts

@@ -14,7 +14,7 @@ export class MessageTransmissionBase implements MessageTransmissionBaseInterface
     incomingMessageBus!: Subject<any>;
     outgoingMessageBus!: Subject<any>;
 
-    constructor() {
+    constructor() { 
         // logic here
     }
 

+ 1 - 3
src/transmission/msg.transmission.manager.ts

@@ -5,9 +5,6 @@ import { MessageTransmission, MessageTransmissionManager as MessageTransmissionM
 import { v4 as uuidv4 } from 'uuid'
 import { AdaptorTransmissionRole, RequestResponseConnectionAdapter, TransportEvent } from "../interface/connector.interface";
 import { MessageTransmissionRequestResponse } from "./msg.transmission.request-response";
-import { TransmitterConnectionAdapter } from "../connector/connector.transmitter";
-import { ReceiverConnectionAdapter } from "../connector/connector.receiver";
-import { filter, Subject } from "rxjs";
 /*OKay, the application don't give a shit whose request it comes from. That will be the responsibility of this manager in particular.
 So, from the transmtiter perspective, it would be as if it's only coming from one source, but the demultiplexing will occur here 
 so to speak. The plan, for one type of transport, this manager will make sure all of the connector instances travel through here.
@@ -22,6 +19,7 @@ export class MessageTransmissionManager implements MessageTransmissionManagerInt
 
     constructor() {
        // logic here
+       console.log(`TransmissionManager: Contructing Transmission Manager...`)
        this.instantiateConnectionManager()
     }
 

+ 5 - 4
src/transmission/msg.transmission.receiver.ts

@@ -1,7 +1,7 @@
 import { filter, map, Observable, Observer, PartialObserver, Subject, Subscriber, Subscription, takeWhile, Unsubscribable } from 'rxjs';
-import { AdapterProfile, AdaptorTransmissionRole, TransportEvent, TransportMessage } from '../interface/connector.interface';
+import { AdapterProfile, AdapterSet, AdaptorTransmissionRole, TransportEvent, TransportMessage } from '../interface/connector.interface';
 import { MessageTransmissionBase } from './msg.transmission.base';
-import { Bus, FisMessage, MessageReceiver as MessageReceiverInterface, ReceiverProfile, TransmissionMessage, TransmitterProfile } from '../interface/transport.interface'
+import { Bus, EventMessage, FisMessage, MessageReceiver as MessageReceiverInterface, ReceiverProfile, TransmissionMessage, TransmitterProfile } from '../interface/transport.interface'
 import { ConnectionAdapter } from '../connector/connector.base';
 import { v4 as uuidv4 } from 'uuid'
 import { ReceiverConnectionAdapter } from '../connector/connector.receiver';
@@ -47,8 +47,9 @@ export class MessageTransmissionReceiver extends MessageTransmissionBase impleme
     private handleEvent(event: Observable<TransportEvent>): void {
         event.pipe(
             filter((obj: TransportEvent) => obj.event == `New Adapter`),
-            map((obj: TransportEvent) => (obj.data as ReceiverConnectionAdapter))
-        ).subscribe((adapter: ReceiverConnectionAdapter) => {
+            map((obj: TransportEvent) => (obj.data as EventMessage))
+        ).subscribe((event: EventMessage) => {
+            let adapter = (event.payload as AdapterSet).receiverAdapter
             this.adaptorsArray.push({ id: adapter.getInfo().id, adapter: adapter } as unknown as AdapterProfile)
             adapter.getMessageBus(Bus.GeneralBus).subscribe(this.incomingTransportMessage)
         })

+ 7 - 5
src/transmission/msg.transmission.transmitter.ts

@@ -1,5 +1,5 @@
 import { MessageTransmissionBase } from "./msg.transmission.base";
-import { FisMessage, MessageTransmitter as MessageTransmitterInterface, TransmissionMessage, TransmitterProfile } from '../interface/transport.interface'
+import { EventMessage, FisMessage, MessageTransmitter as MessageTransmitterInterface, TransmissionMessage, TransmitterProfile } from '../interface/transport.interface'
 import { AdapterProfile, AdapterSet, AdaptorTransmissionRole, ConnectionState, TransportEvent, TransportMessage, TransportService } from "../interface/connector.interface";
 import { v4 as uuidv4 } from 'uuid'
 import { TransmitterConnectionAdapter } from "../connector/connector.transmitter";
@@ -25,7 +25,7 @@ export class MessageTransmissionTransmitter extends MessageTransmissionBase impl
     emit(message: TransmissionMessage): void {
         // logic here, emit based on message
         let adapter: AdapterProfile | undefined = this.adaptorsArray.find(obj => obj.id == message.adapterId)
-        if(adapter) {
+        if (adapter) {
             (adapter.adapter as TransmitterConnectionAdapter).emit(message)
         }
     }
@@ -33,9 +33,11 @@ export class MessageTransmissionTransmitter extends MessageTransmissionBase impl
     private handleEvent(event: Observable<TransportEvent>): void {
         event.pipe(
             filter((obj: TransportEvent) => obj.event == `New Adapter`),
-            map(obj => (obj as TransportEvent).data as AdapterSet)
-        ).subscribe((adapter: AdapterSet) => {
-            this.adaptorsArray.push({ id: adapter.transmitterAdapter.getInfo().id, adapter: adapter.transmitterAdapter as TransmitterConnectionAdapter, connectionState: adapter.transmitterAdapter.connectionStateBus }) // refers to channel ID, which will be embedded in these messages that pass through
+            map(obj => (obj as TransportEvent).data as EventMessage)
+        ).subscribe((event: EventMessage) => {
+            let adapter: TransmitterConnectionAdapter = (event.payload as AdapterSet).transmitterAdapter
+            // so event is only id. because I don't want to have too much instance ?
+            this.adaptorsArray.push({ id: adapter.getInfo().id, adapter: adapter, connectionState: adapter.connectionStateBus })
         })
     }
 }

+ 10 - 4
src/transport/http.ts

@@ -1,8 +1,9 @@
-import { Observable } from "rxjs";
-import { TransportEvent, TransportMessage, TransportService } from "../interface/connector.interface";
+import { Observable, Subject } from "rxjs";
+import { Transport, TransportEvent, TransportMessage, TransportService } from "../interface/connector.interface";
 
 export class HttpTransportService implements TransportService {
-    getInfo() {
+    transportEvent: Subject<TransportEvent> = new Subject()
+    getInfo(): Transport {
         throw new Error("Method not implemented.");
     }
     emit(message: TransportMessage): void {
@@ -15,7 +16,12 @@ export class HttpTransportService implements TransportService {
         throw new Error("Method not implemented.");
     }
     startServer(port: number): void {
-
+        throw new Error("Method not implemented.");
+    }
+    getTransportEvent(): Observable<TransportEvent> {
+        return this.transportEvent.asObservable()
     }
 
+
+
 }

+ 7 - 12
src/transport/websocket.ts

@@ -2,17 +2,18 @@ import { BehaviorSubject, filter, Observable, Subject } from "rxjs";
 import { Socket as ClientSocket } from 'socket.io-client'
 import { Socket as SocketForConnectedClient } from "socket.io"
 import { handleClientSocketConnection, handleNewSocketClient, startClientSocketConnection, startSocketServer } from "../utils/socket.utils";
-import { ConnectionState, Info, Transport, TransportEvent, TransportMessage, TransportService } from "../interface/connector.interface";
+import { ClientObject, ConnectionState, Info, Transport, TransportEvent, TransportMessage, TransportService } from "../interface/connector.interface";
 
 /* Just code in the context that this websocket service will be handling multiple UI clients. Can think about the server communication at a later time. */
 export class WebsocketTransportService implements TransportService {
-    private info: Info = { transport: Transport.Websocket }
+    private info: Transport = Transport.Websocket 
     private connectedServer: ConnectedServerSocket[] = [] // to allow the possibility of having to communicate with multiple servers as a client
     private connectedClientSocket: ConnectedClientSocket[] = [] // to keep track of the all the clients that are connected
     // private incomingMessage: Subject<TransportMessage> = new Subject() // this is only for client roles only atm
     private transportEvent: Subject<TransportEvent> = new Subject()
 
     constructor() {
+        console.log(`WebsocketTransportService: Constructing socket transport service....`)
         // logic here
     }
 
@@ -20,6 +21,7 @@ export class WebsocketTransportService implements TransportService {
         // logic here
         startSocketServer(port).subscribe({
             next: (connectedClient: SocketForConnectedClient) => {
+                console.log(`WebsocketTransport Server Started...`)
                 handleNewSocketClient(connectedClient, this.connectedClientSocket).subscribe(this.transportEvent)
             },
             error: error => console.error(error),
@@ -54,22 +56,15 @@ export class WebsocketTransportService implements TransportService {
         return this.transportEvent.asObservable()
     }
 
-    public getInfo(): Info {
+    public getInfo(): Transport {
         return this.info
     }
 }
 
-
-
-interface ConnectedSocket {
-    id: string,
-    dateCreated: Date,
-    connectionState: BehaviorSubject<ConnectionState>
-}
-export interface ConnectedClientSocket extends ConnectedSocket {
+export interface ConnectedClientSocket extends ClientObject {
     socketInstance: SocketForConnectedClient
 }
 
-export interface ConnectedServerSocket extends ConnectedSocket {
+export interface ConnectedServerSocket extends ClientObject {
     socketInstance: ClientSocket
 }

+ 16 - 9
src/utils/socket.utils.ts

@@ -11,6 +11,7 @@ import { EventMessage } from '../interface/transport.interface';
 export function startSocketServer(port: number): Observable<SocketForConnectedClient> {
     return new Observable((observer) => {
         try {
+            console.log(`Socket Server ${port} Started....`)
             let httpServer = createServer();
             let socketServer = new Server(httpServer)
 
@@ -89,6 +90,11 @@ export function handleClientSocketConnection(socket: ClientSocket, serversConnec
             console.log(`Websocket Client Transport Receieve Msg`, msg.id)
             if (receiverProfileInfo) {
                 // publish to event
+                eventNotification.next({
+                    id: uuidv4(),
+                    event: 'New Message',
+                    data: msg
+                })
             } else {
                 // Do nothing. just store in local array first. Cannot process without information. but then again, don['t need information if acting as client
                 // but for consistency sake, will impose the standard 
@@ -97,7 +103,7 @@ export function handleClientSocketConnection(socket: ClientSocket, serversConnec
         })
 
         socket.on('profile', (data: { name: string, message: any }) => {
-            // console.log(data)
+            console.log(data)
             if (data.name == 'New Profile') {
                 console.log(`Assigned client Name: ${(data.message as ConnectedServerSocket).id}`)
                 receiverProfileInfo = data.message as ConnectedServerSocket
@@ -115,12 +121,13 @@ export function handleClientSocketConnection(socket: ClientSocket, serversConnec
 
                 }).catch((error) => { }) // do nothing at the moment. 
                 // Update websocket instance record
-                serversConnected.push({
+                receiverProfileInfo = {
                     id: (data.message as ConnectedServerSocket).id,
                     dateCreated: new Date(),
                     socketInstance: socket,
                     connectionState: new BehaviorSubject<ConnectionState>(`ONLINE`)
-                })
+                }
+                serversConnected.push(receiverProfileInfo)
             }
             if (data.name == 'Adjusted Profile') {
                 console.log(`Assigned client Name: ${(data.message as ConnectedServerSocket).id}`)
@@ -137,7 +144,7 @@ export function handleClientSocketConnection(socket: ClientSocket, serversConnec
                     })
                 }).catch((error) => { }) // do nothing at the moment. 
                 // Update websocket instance record
-                let clientObj = serversConnected.find(obj => obj.id === (data.message as ConnectedServerSocket).id)
+                let clientObj: ConnectedServerSocket | undefined = serversConnected.find(obj => obj.id === (data.message as ConnectedServerSocket).id)
                 if (clientObj) {
                     clientObj.socketInstance = receiverProfileInfo.socketInstance
                     clientObj.connectionState.next('ONLINE')
@@ -187,6 +194,10 @@ export function handleNewSocketClient(socket: SocketForConnectedClient, connecte
                     socketInstance: socket,
                     connectionState: new BehaviorSubject<ConnectionState>(`ONLINE`)
                 }
+                // send to receiver for reference
+                socket.emit('profile', {
+                    name: `New Profile`, message: { id: clientInstance.id }
+                })
                 // publish first event notification
                 event.next({
                     id: uuidv4(),
@@ -197,10 +208,6 @@ export function handleNewSocketClient(socket: SocketForConnectedClient, connecte
                         payload: clientInstance
                     } as EventMessage
                 })
-                // send to receiver for reference
-                socket.emit('profile', {
-                    name: `New Profile`, message: clientInstance
-                })
                 // Update connected clientInstance info to adapter
                 connectedClientSocket.push(clientInstance)
                 startListening(socket, clientInstance, event)
@@ -209,7 +216,7 @@ export function handleNewSocketClient(socket: SocketForConnectedClient, connecte
                 let clientInstance: ConnectedClientSocket | undefined = connectedClientSocket.find(obj => obj.id === message.data.id)
                 if (clientInstance) {
                     console.log(`Socket Client ${clientInstance.id} Found`)
-                    socket.emit('profile', { name: 'Adjusted Profile', message: clientInstance })
+                    socket.emit('profile', { name: 'Adjusted Profile', message: { id: clientInstance.id } })
                     // replace socket instance since the previous has been terminated
                     clientInstance.socketInstance = socket
                     // need to start listening again, because it's assigned a different socket instance this time round