move some shared stuff into a new baseclass for protocols to implement

will make greenfield (i.e: brand new non-Guac or whatever) protocol implementation a bit less boilerplatey
This commit is contained in:
modeco80
2024-08-21 22:26:36 -04:00
parent 4583531fce
commit 1673f0abd7
2 changed files with 24 additions and 18 deletions

View File

@@ -1,31 +1,15 @@
import pino from 'pino'; import pino from 'pino';
import { IProtocol, IProtocolHandlers, ListEntry, ProtocolAddUser, ProtocolChatHistory, ScreenRect } from './Protocol'; import { IProtocol, IProtocolHandlers, ListEntry, ProtocolAddUser, ProtocolBase, ProtocolChatHistory, ScreenRect } from './Protocol';
import { User } from './User'; import { User } from './User';
import * as cvm from '@cvmts/cvm-rs'; import * as cvm from '@cvmts/cvm-rs';
// CollabVM protocol implementation for Guacamole. // CollabVM protocol implementation for Guacamole.
export class GuacamoleProtocol implements IProtocol { export class GuacamoleProtocol extends ProtocolBase implements IProtocol {
private handlers: IProtocolHandlers | null = null;
private logger = pino({ private logger = pino({
name: 'CVMTS.GuacamoleProtocol' name: 'CVMTS.GuacamoleProtocol'
}); });
protected user: User | null = null;
init(u: User): void {
this.user = u;
}
dispose(): void {
this.user = null;
this.handlers = null;
}
setHandler(handlers: IProtocolHandlers): void {
this.handlers = handlers;
}
private __processMessage_admin(decodedElements: string[]): boolean { private __processMessage_admin(decodedElements: string[]): boolean {
switch (decodedElements[1]) { switch (decodedElements[1]) {
case '2': case '2':

View File

@@ -76,12 +76,15 @@ export interface IProtocolHandlers {
// Abstracts away all of the CollabVM protocol details // Abstracts away all of the CollabVM protocol details
export interface IProtocol { export interface IProtocol {
// don't implement this yourself, extend from ProtocolBase
init(u: User): void; init(u: User): void;
dispose(): void; dispose(): void;
// Sets handler object. // Sets handler object.
setHandler(handlers: IProtocolHandlers): void; setHandler(handlers: IProtocolHandlers): void;
// Protocol implementation stuff
// Parses a single CollabVM protocol message and fires the given handler. // Parses a single CollabVM protocol message and fires the given handler.
// This function does not catch any thrown errors; it is the caller's responsibility // This function does not catch any thrown errors; it is the caller's responsibility
// to handle errors. It should, however, catch invalid parameters without failing. // to handle errors. It should, however, catch invalid parameters without failing.
@@ -117,6 +120,25 @@ export interface IProtocol {
sendScreenUpdate(rect: ScreenRect): void; sendScreenUpdate(rect: ScreenRect): void;
} }
// base mixin for all protocols to use
export class ProtocolBase {
protected handlers: IProtocolHandlers | null = null;
protected user: User | null = null;
init(u: User): void {
this.user = u;
}
dispose(): void {
this.user = null;
this.handlers = null;
}
setHandler(handlers: IProtocolHandlers): void {
this.handlers = handlers;
}
}
// Holds protocol factories. // Holds protocol factories.
export class ProtocolManager { export class ProtocolManager {
private protocols = new Map<String, () => IProtocol>(); private protocols = new Map<String, () => IProtocol>();