BACKEND DONE FOR ALL APPS
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { SocketCommunicatorBase } from "./socket_communicator/socket_communicator_base";
|
||||
|
||||
interface Connection {
|
||||
communicator: SocketCommunicatorBase;
|
||||
}
|
||||
|
||||
export class ConnectionManager {
|
||||
private readonly connections: { [key: string]: Connection };
|
||||
|
||||
constructor() {
|
||||
this.connections = {};
|
||||
}
|
||||
|
||||
// Adds a new communicator, keyed by both IP and port
|
||||
addConnection(ip: string, port: number, communicator: SocketCommunicatorBase): void {
|
||||
const key = `${ip}:${port}`;
|
||||
|
||||
// Store the communicator along with the client's public and private keys
|
||||
this.connections[key] = {
|
||||
communicator
|
||||
};
|
||||
|
||||
console.log(`Added communicator for ${ip}:${port}, generated public and private keys.`);
|
||||
}
|
||||
|
||||
// Removes a communicator based on IP and port
|
||||
removeCommunicator(ip: string, port: number): void {
|
||||
const key = `${ip}:${port}`;
|
||||
if (this.connections[key]) {
|
||||
delete this.connections[key];
|
||||
console.log(`Removed communicator for ${ip}:${port}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Retrieves a communicator based on IP and port
|
||||
getCommunicator(ip: string, port: number): SocketCommunicatorBase | null {
|
||||
const key = `${ip}:${port}`;
|
||||
return this.connections[key] ? this.connections[key].communicator : null;
|
||||
}
|
||||
|
||||
// Checks if a communicator exists for a given IP and port
|
||||
communicatorExists(ip: string, port: number): boolean {
|
||||
const key = `${ip}:${port}`;
|
||||
return this.connections[key] !== undefined;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user