move ProtocolManager into its own unit

This commit is contained in:
modeco80
2024-08-22 04:29:17 -04:00
parent 74d7b17d8b
commit a8d32f0555
6 changed files with 42 additions and 39 deletions

View File

@@ -17,7 +17,8 @@ import { ReaderModel } from '@maxmind/geoip2-node';
import { Size, Rect } from './Utilities.js'; import { Size, Rect } from './Utilities.js';
import pino from 'pino'; import pino from 'pino';
import { BanManager } from './BanManager.js'; import { BanManager } from './BanManager.js';
import { IProtocolHandlers, ListEntry, ProtocolAddUser, ProtocolFlag, ProtocolRenameStatus, ProtocolUpgradeCapability, TheProtocolManager } from './protocol/Protocol.js'; import { IProtocolMessageHandler, ListEntry, ProtocolAddUser, ProtocolFlag, ProtocolRenameStatus, ProtocolUpgradeCapability } from './protocol/Protocol.js';
import { TheProtocolManager } from './protocol/Manager.js';
// Instead of strange hacks we can just use nodejs provided // Instead of strange hacks we can just use nodejs provided
// import.meta properties, which have existed since LTS if not before // import.meta properties, which have existed since LTS if not before
@@ -37,7 +38,7 @@ type VoteTally = {
no: number; no: number;
}; };
export default class CollabVMServer implements IProtocolHandlers { export default class CollabVMServer implements IProtocolMessageHandler {
private Config: IConfig; private Config: IConfig;
private clients: User[]; private clients: User[];
@@ -184,7 +185,7 @@ export default class CollabVMServer implements IProtocolHandlers {
user.socket.on('disconnect', () => this.connectionClosed(user)); user.socket.on('disconnect', () => this.connectionClosed(user));
// Set ourselves as the handler // Set ourselves as the handler
user.protocol.setHandler(this as IProtocolHandlers); user.protocol.setHandler(this as IProtocolMessageHandler);
if (this.Config.auth.enabled) { if (this.Config.auth.enabled) {
user.protocol.sendAuth(this.Config.auth.apiEndpoint); user.protocol.sendAuth(this.Config.auth.apiEndpoint);
@@ -223,7 +224,7 @@ export default class CollabVMServer implements IProtocolHandlers {
this.clients.forEach((c) => c.protocol.sendRemUser([user.username!])); this.clients.forEach((c) => c.protocol.sendRemUser([user.username!]));
} }
// IProtocolHandlers // Protocol message handlers
// does auth check // does auth check
private authCheck(user: User, guestPermission: boolean) { private authCheck(user: User, guestPermission: boolean) {
@@ -318,7 +319,7 @@ export default class CollabVMServer implements IProtocolHandlers {
user.Capabilities.bin = true; user.Capabilities.bin = true;
user.protocol.dispose(); user.protocol.dispose();
user.protocol = TheProtocolManager.createProtocol('binary1', user); user.protocol = TheProtocolManager.createProtocol('binary1', user);
user.protocol.setHandler(this as IProtocolHandlers); user.protocol.setHandler(this as IProtocolMessageHandler);
break; break;
default: default:
break; break;
@@ -706,11 +707,10 @@ export default class CollabVMServer implements IProtocolHandlers {
this.clients.forEach((c) => c.protocol.sendChatMessage('', message)); this.clients.forEach((c) => c.protocol.sendChatMessage('', message));
} }
// end IProtocolHandlers // end protocol message handlers
getUsernameList(): string[] { getUsernameList(): string[] {
var arr: string[] = []; var arr: string[] = [];
this.clients.filter((c) => c.username).forEach((c) => arr.push(c.username!)); this.clients.filter((c) => c.username).forEach((c) => arr.push(c.username!));
return arr; return arr;
} }

View File

@@ -8,7 +8,8 @@ import { NetworkClient } from './net/NetworkClient.js';
import { CollabVMCapabilities } from '@cvmts/collab-vm-1.2-binary-protocol'; import { CollabVMCapabilities } from '@cvmts/collab-vm-1.2-binary-protocol';
import pino from 'pino'; import pino from 'pino';
import { BanManager } from './BanManager.js'; import { BanManager } from './BanManager.js';
import { IProtocol, TheProtocolManager } from './protocol/Protocol.js'; import { IProtocol } from './protocol/Protocol.js';
import { TheProtocolManager } from './protocol/Manager.js';
export class User { export class User {
socket: NetworkClient; socket: NetworkClient;

View File

@@ -16,7 +16,7 @@ import pino from 'pino';
import { Database } from './Database.js'; import { Database } from './Database.js';
import { BanManager } from './BanManager.js'; import { BanManager } from './BanManager.js';
import { QemuVMShim } from './vm/qemu.js'; import { QemuVMShim } from './vm/qemu.js';
import { TheProtocolManager } from './protocol/Protocol.js'; import { TheProtocolManager } from './protocol/Manager.js';
import { GuacamoleProtocol } from './protocol/GuacamoleProtocol.js'; import { GuacamoleProtocol } from './protocol/GuacamoleProtocol.js';
import { BinRectsProtocol } from './protocol/BinRectsProtocol.js'; import { BinRectsProtocol } from './protocol/BinRectsProtocol.js';

View File

@@ -1,5 +1,5 @@
import pino from 'pino'; import pino from 'pino';
import { IProtocol, IProtocolHandlers, ListEntry, ProtocolAddUser, ProtocolBase, ProtocolChatHistory, ProtocolFlag, ProtocolRenameStatus, ProtocolUpgradeCapability, ScreenRect } from './Protocol.js'; import { IProtocol, IProtocolMessageHandler, ListEntry, ProtocolAddUser, ProtocolBase, ProtocolChatHistory, ProtocolFlag, ProtocolRenameStatus, ProtocolUpgradeCapability, ScreenRect } from './Protocol.js';
import { Rank, User } from '../User.js'; import { Rank, User } from '../User.js';
import * as cvm from '@cvmts/cvm-rs'; import * as cvm from '@cvmts/cvm-rs';

View File

@@ -0,0 +1,27 @@
import { IProtocol } from "./Protocol";
import { User } from "../User";
// The protocol manager. Holds protocol factories, and provides the ability
// to create a protocol by name. Avoids direct dependency on a given list of protocols,
// and allows (relatively simple) expansion.
export class ProtocolManager {
private protocols = new Map<String, () => IProtocol>();
// Registers a protocol with the given name.
registerProtocol(name: string, protocolFactory: () => IProtocol) {
if (!this.protocols.has(name)) this.protocols.set(name, protocolFactory);
}
// Creates an instance of a given protocol for a user.
createProtocol(name: string, user: User): IProtocol {
if (!this.protocols.has(name)) throw new Error(`ProtocolManager does not have protocol \"${name}\"`);
let factory = this.protocols.get(name)!;
let proto = factory();
proto.init(user);
return proto;
}
}
/// Global protocol manager
export let TheProtocolManager = new ProtocolManager();

View File

@@ -40,7 +40,7 @@ export interface ProtocolFlag {
} }
// Protocol handlers. This is implemented by a layer that wants to listen to CollabVM protocol messages. // Protocol handlers. This is implemented by a layer that wants to listen to CollabVM protocol messages.
export interface IProtocolHandlers { export interface IProtocolMessageHandler {
onNop(user: User): void; onNop(user: User): void;
onNoFlag(user: User): void; onNoFlag(user: User): void;
@@ -95,7 +95,7 @@ export interface IProtocol {
dispose(): void; dispose(): void;
// Sets handler object. // Sets handler object.
setHandler(handlers: IProtocolHandlers): void; setHandler(handlers: IProtocolMessageHandler): void;
// Protocol implementation stuff // Protocol implementation stuff
@@ -152,7 +152,7 @@ export interface IProtocol {
// Base mixin for all concrete protocols to use. Inherit from this! // Base mixin for all concrete protocols to use. Inherit from this!
export class ProtocolBase { export class ProtocolBase {
protected handlers: IProtocolHandlers | null = null; protected handlers: IProtocolMessageHandler | null = null;
protected user: User | null = null; protected user: User | null = null;
init(u: User): void { init(u: User): void {
@@ -164,32 +164,7 @@ export class ProtocolBase {
this.handlers = null; this.handlers = null;
} }
setHandler(handlers: IProtocolHandlers): void { setHandler(handlers: IProtocolMessageHandler): void {
this.handlers = handlers; this.handlers = handlers;
} }
} }
// The protocol manager. Holds protocol factories, and provides the ability
// to create a protocol by name. Avoids direct dependency on a given list of protocols,
// and allows (relatively simple) expansion.
export class ProtocolManager {
private protocols = new Map<String, () => IProtocol>();
// Registers a protocol with the given name.
registerProtocol(name: string, protocolFactory: () => IProtocol) {
if (!this.protocols.has(name)) this.protocols.set(name, protocolFactory);
}
// Creates an instance of a given protocol for a user.
createProtocol(name: string, user: User): IProtocol {
if (!this.protocols.has(name)) throw new Error(`ProtocolManager does not have protocol \"${name}\"`);
let factory = this.protocols.get(name)!;
let proto = factory();
proto.init(user);
return proto;
}
}
/// Global protocol manager
export let TheProtocolManager = new ProtocolManager();