summaryrefslogtreecommitdiff
path: root/WebInterface/src/js/modules/networking
diff options
context:
space:
mode:
Diffstat (limited to 'WebInterface/src/js/modules/networking')
-rw-r--r--WebInterface/src/js/modules/networking/commands/_command.js28
-rw-r--r--WebInterface/src/js/modules/networking/commands/login/createServer.js23
-rw-r--r--WebInterface/src/js/modules/networking/commands/login/listServers.js43
-rw-r--r--WebInterface/src/js/modules/networking/commands/login/login.js54
-rw-r--r--WebInterface/src/js/modules/networking/commands/loginCmds.js36
-rw-r--r--WebInterface/src/js/modules/networking/commands/playCmds.js31
-rw-r--r--WebInterface/src/js/modules/networking/hash.js20
-rw-r--r--WebInterface/src/js/modules/networking/networker.js89
8 files changed, 324 insertions, 0 deletions
diff --git a/WebInterface/src/js/modules/networking/commands/_command.js b/WebInterface/src/js/modules/networking/commands/_command.js
new file mode 100644
index 0000000..46a1a14
--- /dev/null
+++ b/WebInterface/src/js/modules/networking/commands/_command.js
@@ -0,0 +1,28 @@
+/**
+ * Parent Command class which all commands inherit from
+ */
+export default class Command {
+ /**
+ * Constructs basic command object
+ * @param {Interface} iface Interface to communicate over
+ */
+ constructor(iface) {
+ this.iface = iface;
+ }
+
+ /**
+ * Registers public command names to interface
+ * @param {String} name Name to register under
+ * @param {...String} commandNames Names of public commands
+ */
+ registerPublic(name, ...commandNames) {
+ this.iface.addObject(this, name, ['destroy'].concat(commandNames));
+ }
+
+ /**
+ * Removes from iface
+ */
+ destroy() {
+ this.iface.removeObject(this);
+ }
+}
diff --git a/WebInterface/src/js/modules/networking/commands/login/createServer.js b/WebInterface/src/js/modules/networking/commands/login/createServer.js
new file mode 100644
index 0000000..78b2a1b
--- /dev/null
+++ b/WebInterface/src/js/modules/networking/commands/login/createServer.js
@@ -0,0 +1,23 @@
+import Command from '../_command';
+
+/**
+ * Handles creation of Servers
+ */
+export default class CreateServer extends Command {
+ /**
+ * Registers interface for communication with other objects
+ * @param {Interface} iface
+ */
+ constructor(iface) {
+ super(iface);
+ this.registerPublic('createServer', 'createServer');
+ this.refreshing = false;
+ }
+
+ /**
+ * TODO:
+ */
+ createServer() {
+
+ }
+}
diff --git a/WebInterface/src/js/modules/networking/commands/login/listServers.js b/WebInterface/src/js/modules/networking/commands/login/listServers.js
new file mode 100644
index 0000000..2c2bc11
--- /dev/null
+++ b/WebInterface/src/js/modules/networking/commands/login/listServers.js
@@ -0,0 +1,43 @@
+import Command from '../_command';
+
+/**
+ * Handles serverList commands
+ */
+export default class ListServers extends Command {
+ /**
+ * Registers interface for communication with other objects
+ * @param {Interface} iface
+ */
+ constructor(iface) {
+ super(iface);
+ this.registerPublic('listServers', 'listServers');
+ this.refreshing = false;
+ }
+
+ /**
+ * Requests server list from the server
+ */
+ listServers() {
+ if (this.refreshing) return; // If already refreshing, no new request
+
+ let listFn = (groups) => {
+ // Populate server listing
+ this.iface.callMethod('serverListing', 'flushElements');
+ this.iface.callMethod('serverListing', 'addElements', groups, this.iface);
+ // Unbind network function
+ this.iface.callMethod('networker', 'removeHandler', 'ListGroups');
+ this.refreshing = false;
+ };
+ let errorHandler = (err) => {
+ this.refreshing = false;
+ console.error(err.toString());
+ };
+
+ this.iface.callMethod('networker', 'registerHandler',
+ 'ListGroups', listFn);
+ this.iface.callMethod('networker', 'sendRequest',
+ 'GetGroups', errorHandler);
+
+ this.refreshing = true;
+ }
+}
diff --git a/WebInterface/src/js/modules/networking/commands/login/login.js b/WebInterface/src/js/modules/networking/commands/login/login.js
new file mode 100644
index 0000000..44a6c94
--- /dev/null
+++ b/WebInterface/src/js/modules/networking/commands/login/login.js
@@ -0,0 +1,54 @@
+import Command from '../_command';
+import LoginModal from '../../../ui/components/modal/login-modal';
+
+/**
+ * Handles login to server
+ */
+export default class Login extends Command {
+ /**
+ * Registers interface for communication with other objects
+ * @param {Interface} iface
+ */
+ constructor(iface) {
+ super(iface);
+ this.registerPublic('login', 'sendLogin', 'showLogin');
+ this.refreshing = false;
+ }
+
+ /**
+ * Shows a login modal
+ * @param {String} name
+ */
+ showLogin(name) {
+ new LoginModal(this.iface, name);
+ }
+
+ /**
+ * Registers login response method
+ */
+ registerLoginResponse() {
+ this.iface.callMethod('networker', 'registerHandler', 'LoginResponse',
+ (result) => {
+ if (result == 0) {
+ this.iface.callMethod('modal', 'close');
+ this.iface.callMethod('router', 'routePlay');
+ this.iface.callMethod('networker', 'removeHandler',
+ 'LoginResponse');
+ } else {
+ this.iface.callMethod('modal', 'loginFailed', result);
+ }
+ });
+ }
+
+ /**
+ * Sends a login request
+ * @param {string} group Group name to join
+ * @param {string} password Password to send as SHA-256 Base64 String
+ * @param {string} username Display name to use
+ */
+ sendLogin(group, password, username) {
+ this.registerLoginResponse();
+ this.iface.callMethod('networker', 'sendRequest', 'Login',
+ (err) => console.error(err), group, username, password);
+ }
+}
diff --git a/WebInterface/src/js/modules/networking/commands/loginCmds.js b/WebInterface/src/js/modules/networking/commands/loginCmds.js
new file mode 100644
index 0000000..bc5d8a7
--- /dev/null
+++ b/WebInterface/src/js/modules/networking/commands/loginCmds.js
@@ -0,0 +1,36 @@
+import ListServers from './login/listServers';
+import CreateServer from './login/createServer';
+import Login from './login/login';
+
+/**
+ * Manages commands related to the login page
+ */
+export default class LoginCommands {
+ /**
+ * Initializes the login commands
+ * @param {Interface} iface Interface for inter-object communication
+ */
+ constructor(iface) {
+ this.iface = iface;
+ this.cmds = [];
+ this.registerCommands();
+ }
+
+ /**
+ * Registers all the available commands
+ */
+ registerCommands() {
+ this.cmds.push(new ListServers(this.iface));
+ this.cmds.push(new CreateServer(this.iface));
+ this.cmds.push(new Login(this.iface));
+ }
+
+ /**
+ * Destroys all attached commands
+ */
+ destroy() {
+ for (let cmd of this.cmds) {
+ cmd.destroy();
+ }
+ }
+}
diff --git a/WebInterface/src/js/modules/networking/commands/playCmds.js b/WebInterface/src/js/modules/networking/commands/playCmds.js
new file mode 100644
index 0000000..94cd6ba
--- /dev/null
+++ b/WebInterface/src/js/modules/networking/commands/playCmds.js
@@ -0,0 +1,31 @@
+// import ListServers from './login/listServers';
+
+/**
+ * Manages commands related to the login page
+ */
+export default class LoginCommands {
+ /**
+ * Initializes the login commands
+ * @param {Interface} iface Interface for inter-object communication
+ */
+ constructor(iface) {
+ this.iface = iface;
+ this.cmds = [];
+ }
+
+ /**
+ * Registers all the available commands
+ */
+ registerCommands() {
+ // this.cmds.push(new ListServers(iface));
+ }
+
+ /**
+ * Destroys all attached commands
+ */
+ destroy() {
+ for (let cmd of this.cmds) {
+ cmd.destroy();
+ }
+ }
+}
diff --git a/WebInterface/src/js/modules/networking/hash.js b/WebInterface/src/js/modules/networking/hash.js
new file mode 100644
index 0000000..3abcc21
--- /dev/null
+++ b/WebInterface/src/js/modules/networking/hash.js
@@ -0,0 +1,20 @@
+/**
+ * Creates Base64 String with SHA-256 Hash of given string
+ */
+String.prototype.getHash = async function() {
+ let data = new ArrayBuffer(this.length * 2);
+ let bufferView = new Uint16Array(data);
+ for (let i = 0; i < this.length; i++) {
+ bufferView[i] = this.charCodeAt(i);
+ }
+
+ let encrypted = await crypto.subtle.digest('SHA-256', bufferView);
+ let byteArray = new Uint8Array(encrypted);
+ let base64String = '';
+
+ for (let byte of byteArray) {
+ base64String += String.fromCharCode(byte);
+ }
+
+ return btoa(base64String);
+};
diff --git a/WebInterface/src/js/modules/networking/networker.js b/WebInterface/src/js/modules/networking/networker.js
new file mode 100644
index 0000000..199adc7
--- /dev/null
+++ b/WebInterface/src/js/modules/networking/networker.js
@@ -0,0 +1,89 @@
+import * as signalR from '@aspnet/signalr';
+import LoginCommands from './commands/loginCmds';
+import PlayCommands from './commands/playCmds';
+
+/**
+ * Class for communication to server
+ */
+export default class Networker {
+ /**
+ * Creates new Networker and connects it to the Interface
+ * @param {Interface} iface Interface for communication between objects
+ * @param {String} url URL of the server backend
+ * @param {Boolean} [debug=false] Should there be debug output
+ */
+ constructor(iface, url, debug = false) {
+ this.url = url;
+
+ // Register in Interface
+ iface.addObject(this, 'networker',
+ ['sendRequest', 'registerHandler', 'removeHandler']);
+ this.iface = iface;
+
+ const connectionBuilder = new signalR.HubConnectionBuilder()
+ .withUrl(url);
+
+ if (debug) {
+ connectionBuilder.configureLogging(signalR.LogLevel.Trace);
+ } else {
+ connectionBuilder.configureLogging(signalR.LogLevel.Error);
+ }
+
+ this.connection = connectionBuilder.build();
+ this.connection.start()
+ .then(() => this.iface.callMethod('listServers', 'listServers'))
+ .catch((err) => console.error(err.toString()));
+
+ // Initialize refreshing (blocks new refreshes if true)
+ this.refreshing = false;
+ }
+
+ /**
+ * Sending a network request to the server
+ * @param {String} methodName Method to call on server
+ * @param {function} errorHandler Function to call on error
+ * @param {...*} args Arguments to pass to server
+ */
+ sendRequest(methodName, errorHandler, ...args) {
+ this.connection.invoke(methodName, ...args).catch(errorHandler);
+ }
+
+ /**
+ * Register a new function to be called upon receival of message from server
+ * @param {String} name Name of invoked method
+ * @param {function} fn function to call with received data
+ */
+ registerHandler(name, fn) {
+ this.connection.on(name, fn);
+ }
+
+ /**
+ * Removes handler for receiving messages from the server
+ * @param {String} name Name of the invoked method
+ */
+ removeHandler(name) {
+ this.connection.off(name);
+ }
+
+ /**
+ * Initializes Login Commands
+ */
+ initLogin() {
+ this.loginCmd = new LoginCommands(this.iface);
+ }
+
+ /**
+ * Initializes play commands
+ */
+ initPlay() {
+ this.playCmd = new PlayCommands(this.iface);
+ }
+
+ /**
+ * Clears all currently registered commands
+ */
+ clearCommands() {
+ if (this.loginCmd) this.loginCmd.destroy();
+ if (this.playCmd) this.playCmd.destroy();
+ }
+}