Files
FACULTATE-LICENTA/User/src/network/connection_manager.ts
T

44 lines
1.2 KiB
TypeScript

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,
}
}
// 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]
}
}
// 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
}
}