From 1673f0abd74484566d6cb864bb498af927db13ec Mon Sep 17 00:00:00 2001 From: modeco80 Date: Wed, 21 Aug 2024 22:26:36 -0400 Subject: [PATCH] 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 --- cvmts/src/GuacamoleProtocol.ts | 20 ++------------------ cvmts/src/Protocol.ts | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/cvmts/src/GuacamoleProtocol.ts b/cvmts/src/GuacamoleProtocol.ts index f928455..069ec05 100644 --- a/cvmts/src/GuacamoleProtocol.ts +++ b/cvmts/src/GuacamoleProtocol.ts @@ -1,31 +1,15 @@ 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 * as cvm from '@cvmts/cvm-rs'; // CollabVM protocol implementation for Guacamole. -export class GuacamoleProtocol implements IProtocol { - private handlers: IProtocolHandlers | null = null; +export class GuacamoleProtocol extends ProtocolBase implements IProtocol { private logger = pino({ 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 { switch (decodedElements[1]) { case '2': diff --git a/cvmts/src/Protocol.ts b/cvmts/src/Protocol.ts index ab5bc1f..8a04aa6 100644 --- a/cvmts/src/Protocol.ts +++ b/cvmts/src/Protocol.ts @@ -76,12 +76,15 @@ export interface IProtocolHandlers { // Abstracts away all of the CollabVM protocol details export interface IProtocol { + // don't implement this yourself, extend from ProtocolBase init(u: User): void; dispose(): void; // Sets handler object. setHandler(handlers: IProtocolHandlers): void; + // Protocol implementation stuff + // 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 // to handle errors. It should, however, catch invalid parameters without failing. @@ -117,6 +120,25 @@ export interface IProtocol { 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. export class ProtocolManager { private protocols = new Map IProtocol>();