From 892d1555b8c191cf8c064a255bf1e08e3e8db9d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Fri, 25 Sep 2020 13:48:02 +0200 Subject: [PATCH] Adding "dump" controller and fixing issue with groups in PositionNotifier by delegating the PositionNotifier.updatePosition call to groups themselves --- .env.template | 4 +- back/package.json | 2 + back/src/App.ts | 2 + back/src/Controller/DebugController.ts | 58 +++++++++++++++++++++++ back/src/Controller/IoSocketController.ts | 4 ++ back/src/Model/Group.ts | 21 ++++++-- back/src/Model/PositionNotifier.ts | 2 +- back/src/Model/World.ts | 14 ++++-- back/src/Model/Zone.ts | 27 ++++++++--- back/yarn.lock | 10 ++++ docker-compose.yaml | 1 + 11 files changed, 127 insertions(+), 18 deletions(-) create mode 100644 back/src/Controller/DebugController.ts diff --git a/.env.template b/.env.template index 81044e99..d355ab67 100644 --- a/.env.template +++ b/.env.template @@ -1 +1,3 @@ -DEBUG_MODE=false \ No newline at end of file +DEBUG_MODE=false +JITSI_URL=meet.jit.si +ADMIN_API_TOKEN=123 diff --git a/back/package.json b/back/package.json index 6ad2842f..b1159144 100644 --- a/back/package.json +++ b/back/package.json @@ -38,6 +38,7 @@ "dependencies": { "axios": "^0.20.0", "body-parser": "^1.19.0", + "circular-json": "^0.5.9", "express": "^4.17.1", "generic-type-guard": "^3.2.0", "google-protobuf": "^3.13.0", @@ -51,6 +52,7 @@ "uuidv4": "^6.0.7" }, "devDependencies": { + "@types/circular-json": "^0.4.0", "@types/express": "^4.17.4", "@types/google-protobuf": "^3.7.3", "@types/http-status-codes": "^1.2.0", diff --git a/back/src/App.ts b/back/src/App.ts index d1f7392f..ac681ba3 100644 --- a/back/src/App.ts +++ b/back/src/App.ts @@ -8,6 +8,7 @@ import * as http from "http"; import {MapController} from "./Controller/MapController"; import {PrometheusController} from "./Controller/PrometheusController"; import {AdminController} from "./Controller/AdminController"; +import {DebugController} from "./Controller/DebugController"; class App { public app: Application; @@ -35,6 +36,7 @@ class App { this.mapController = new MapController(this.app); this.prometheusController = new PrometheusController(this.app, this.ioSocketController); this.adminController = new AdminController(this.app); + this.debugController = new DebugController(this.app, this.ioSocketController); } // TODO add session user diff --git a/back/src/Controller/DebugController.ts b/back/src/Controller/DebugController.ts new file mode 100644 index 00000000..ebc4894b --- /dev/null +++ b/back/src/Controller/DebugController.ts @@ -0,0 +1,58 @@ +import {Application, Request, Response} from "express"; +import {OK} from "http-status-codes"; +import {ADMIN_API_TOKEN, ADMIN_API_URL} from "../Enum/EnvironmentVariable"; +import Axios from "axios"; +import {DEBUG_MODE} from "../../../front/src/Enum/EnvironmentVariable"; +import {IoSocketController} from "_Controller/IoSocketController"; +import Flatted from "flatted"; +import {stringify} from "circular-json"; + +export class DebugController { + constructor(private App : Application, private ioSocketController: IoSocketController) { + this.getDump(); + } + + + getDump(){ + this.App.get("/dump", async (req: Request, res: Response) => { + if (req.query.token !== ADMIN_API_TOKEN) { + return res.status(401).send('Invalid token sent!'); + } + +/* const obj: any = {}; + + for (const [worldName, world] of this.ioSocketController.getWorlds().entries()) { + let users = new Array(); + for (const [worldName, world] of this.ioSocketController.getWorlds().entries()) { + + } + + + obj[worldName] = { + users: world.getUsers() + }; + }*/ + + return res.status(OK).contentType('application/json').send(stringify( + this.ioSocketController.getWorlds(), + (key: any, value: any) => { + if(value instanceof Map) { + const obj: any = {}; + for (const [mapKey, mapValue] of value.entries()) { + obj[mapKey] = mapValue; + } + return obj; + } else if(value instanceof Set) { + const obj: Array = []; + for (const [setKey, setValue] of value.entries()) { + obj.push(setValue); + } + return obj; + } else { + return value; + } + } + )); + }); + } +} diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index ca83a79e..7111e7d2 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -757,4 +757,8 @@ export class IoSocketController { Client.leave(Client.webRtcRoomId); delete Client.webRtcRoomId; } + + public getWorlds(): Map { + return this.Worlds; + } } diff --git a/back/src/Model/Group.ts b/back/src/Model/Group.ts index 4364455d..d08e6d90 100644 --- a/back/src/Model/Group.ts +++ b/back/src/Model/Group.ts @@ -3,6 +3,7 @@ import { User } from "./User"; import {PositionInterface} from "_Model/PositionInterface"; import {uuid} from "uuidv4"; import {Movable} from "_Model/Movable"; +import {PositionNotifier} from "_Model/PositionNotifier"; export class Group implements Movable { static readonly MAX_PER_GROUP = 4; @@ -11,16 +12,12 @@ export class Group implements Movable { private id: number; private users: Set; - private connectCallback: ConnectCallback; - private disconnectCallback: DisconnectCallback; private x!: number; private y!: number; - constructor(users: User[], connectCallback: ConnectCallback, disconnectCallback: DisconnectCallback) { + constructor(users: User[], private connectCallback: ConnectCallback, private disconnectCallback: DisconnectCallback, private positionNotifier: PositionNotifier) { this.users = new Set(); - this.connectCallback = connectCallback; - this.disconnectCallback = disconnectCallback; this.id = Group.nextId; Group.nextId++; @@ -53,6 +50,9 @@ export class Group implements Movable { * Computes the barycenter of all users (i.e. the center of the group) */ updatePosition(): void { + const oldX = this.x; + const oldY = this.y; + let x = 0; let y = 0; // Let's compute the barycenter of all users. @@ -67,6 +67,13 @@ export class Group implements Movable { } this.x = x; this.y = y; + + if (oldX === undefined) { + // TODO: do we need a "create" + this.positionNotifier.updatePosition(this, {x, y}, {x, y}); + } else { + this.positionNotifier.updatePosition(this, {x, y}, {x: oldX, y: oldY}); + } } isFull(): boolean { @@ -93,6 +100,10 @@ export class Group implements Movable { } user.group = undefined; + if (this.users.size !== 0) { + this.updatePosition(); + } + // Broadcast on the right event this.disconnectCallback(user.id, this); } diff --git a/back/src/Model/PositionNotifier.ts b/back/src/Model/PositionNotifier.ts index 0e5b4b2f..0c30fe30 100644 --- a/back/src/Model/PositionNotifier.ts +++ b/back/src/Model/PositionNotifier.ts @@ -117,7 +117,7 @@ export class PositionNotifier { let zone = this.zones[j][i]; if (zone === undefined) { - zone = new Zone(this.onUserEnters, this.onUserMoves, this.onUserLeaves); + zone = new Zone(this.onUserEnters, this.onUserMoves, this.onUserLeaves, i, j); this.zones[j][i] = zone; } return zone; diff --git a/back/src/Model/World.ts b/back/src/Model/World.ts index 321b3e1b..bbc472a0 100644 --- a/back/src/Model/World.ts +++ b/back/src/Model/World.ts @@ -93,6 +93,10 @@ export class World { user.position = userPosition; user.group?.updatePosition(); + /*if (user.group !== undefined) { + // TODO: positionNotifier should be notified by the group itself when it moves!!! + this.positionNotifier.updatePosition(user.group, user.group.getPosition(), oldGroupPosition ? oldGroupPosition : user.group.getPosition()); + }*/ if (user.silent) { return; @@ -112,7 +116,7 @@ export class World { const group: Group = new Group([ user, closestUser - ], this.connectCallback, this.disconnectCallback); + ], this.connectCallback, this.disconnectCallback, this.positionNotifier); this.groups.add(group); } } @@ -127,9 +131,9 @@ export class World { } // At the very end, if the user is part of a group, let's call the callback to update group position - if (user.group !== undefined) { + /*if (user.group !== undefined) { this.positionNotifier.updatePosition(user.group, user.group.getPosition(), oldGroupPosition ? oldGroupPosition : user.group.getPosition()); - } + }*/ } setSilent(socket: Identificable, silent: boolean) { @@ -162,6 +166,7 @@ export class World { if (group === undefined) { throw new Error("The user is part of no group"); } + const oldPosition = group.getPosition(); group.leave(user); if (group.isEmpty()) { this.positionNotifier.leave(group); @@ -171,7 +176,8 @@ export class World { } this.groups.delete(group); } else { - this.positionNotifier.updatePosition(group, group.getPosition(), group.getPosition()); + group.updatePosition(); + //this.positionNotifier.updatePosition(group, group.getPosition(), oldPosition); } } diff --git a/back/src/Model/Zone.ts b/back/src/Model/Zone.ts index 9933637c..36551b39 100644 --- a/back/src/Model/Zone.ts +++ b/back/src/Model/Zone.ts @@ -1,6 +1,7 @@ import {User} from "./User"; import {PositionInterface} from "_Model/PositionInterface"; -import {Movable} from "_Model/Movable"; +import {Movable} from "./Movable"; +import {Group} from "./Group"; export type EntersCallback = (thing: Movable, listener: User) => void; export type MovesCallback = (thing: Movable, position: PositionInterface, listener: User) => void; @@ -10,14 +11,27 @@ export class Zone { private things: Set = new Set(); private listeners: Set = new Set(); - constructor(private onEnters: EntersCallback, private onMoves: MovesCallback, private onLeaves: LeavesCallback) { + /** + * @param x For debugging purpose only + * @param y For debugging purpose only + */ + constructor(private onEnters: EntersCallback, private onMoves: MovesCallback, private onLeaves: LeavesCallback, private x: number, private y: number) { } /** * A user/thing leaves the zone */ public leave(thing: Movable, newZone: Zone|null) { - this.things.delete(thing); + const result = this.things.delete(thing); + if (!result) { + if (thing instanceof User) { + console.error('Could not find user in zone '+thing.id); + } + if (thing instanceof Group) { + console.error('Could not find group '+thing.getId()+' in zone ('+this.x+','+this.y+'). Position of group: ('+thing.getPosition().x+','+thing.getPosition().y+')'); + } + + } this.notifyLeft(thing, newZone); } @@ -34,13 +48,13 @@ export class Zone { public enter(thing: Movable, oldZone: Zone|null, position: PositionInterface) { this.things.add(thing); - this.notifyUserEnter(thing, oldZone, position); + this.notifyEnter(thing, oldZone, position); } /** * Notify listeners of this zone that this user entered */ - private notifyUserEnter(thing: Movable, oldZone: Zone|null, position: PositionInterface) { + private notifyEnter(thing: Movable, oldZone: Zone|null, position: PositionInterface) { for (const listener of this.listeners) { if (listener === thing) { continue; @@ -56,8 +70,7 @@ export class Zone { public move(thing: Movable, position: PositionInterface) { if (!this.things.has(thing)) { this.things.add(thing); - const foo = this.things; - this.notifyUserEnter(thing, null, position); + this.notifyEnter(thing, null, position); return; } diff --git a/back/yarn.lock b/back/yarn.lock index cb25dc86..3731547d 100644 --- a/back/yarn.lock +++ b/back/yarn.lock @@ -27,6 +27,11 @@ "@types/connect" "*" "@types/node" "*" +"@types/circular-json@^0.4.0": + version "0.4.0" + resolved "https://registry.yarnpkg.com/@types/circular-json/-/circular-json-0.4.0.tgz#7401f7e218cfe87ad4c43690da5658b9acaf51be" + integrity sha512-7+kYB7x5a7nFWW1YPBh3KxhwKfiaI4PbZ1RvzBU91LZy7lWJO822CI+pqzSre/DZ7KsCuMKdHnLHHFu8AyXbQg== + "@types/color-name@^1.1.1": version "1.1.1" resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0" @@ -359,6 +364,11 @@ chardet@^0.7.0: version "0.7.0" resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e" +circular-json@^0.5.9: + version "0.5.9" + resolved "https://registry.yarnpkg.com/circular-json/-/circular-json-0.5.9.tgz#932763ae88f4f7dead7a0d09c8a51a4743a53b1d" + integrity sha512-4ivwqHpIFJZBuhN3g/pEcdbnGUywkBblloGbkglyloVjjR3uT6tieI89MVOfbP2tHX5sgb01FuLgAOzebNlJNQ== + cli-cursor@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/cli-cursor/-/cli-cursor-3.1.0.tgz#264305a7ae490d1d03bf0c9ba7c925d1753af307" diff --git a/docker-compose.yaml b/docker-compose.yaml index a37fe28f..ffc846e4 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -73,6 +73,7 @@ services: STARTUP_COMMAND_1: yarn install SECRET_KEY: yourSecretKey ALLOW_ARTILLERY: "true" + ADMIN_API_TOKEN: "$ADMIN_API_TOKEN" volumes: - ./back:/usr/src/app labels: