2018-08-04 21:46:25 +00:00
|
|
|
"use strict";
|
|
|
|
(function lorgar_js() {
|
|
|
|
var moduleName = "core/lorgar";
|
|
|
|
|
|
|
|
var defineArray = [];
|
|
|
|
defineArray.push("lib/utils/class");
|
2018-08-29 19:54:18 +00:00
|
|
|
defineArray.push("lib/utils/enum");
|
2018-08-04 21:46:25 +00:00
|
|
|
defineArray.push("lib/wSocket/socket");
|
|
|
|
defineArray.push("lib/wDispatcher/dispatcher");
|
|
|
|
defineArray.push("lib/wDispatcher/handler");
|
|
|
|
defineArray.push("lib/wDispatcher/logger");
|
|
|
|
|
|
|
|
defineArray.push("lib/wType/event");
|
|
|
|
defineArray.push("lib/wType/address");
|
|
|
|
defineArray.push("lib/wType/vocabulary");
|
|
|
|
defineArray.push("lib/wType/string");
|
|
|
|
|
|
|
|
defineArray.push("lib/wController/globalControls");
|
|
|
|
defineArray.push("lib/wController/pageStorage");
|
|
|
|
defineArray.push("lib/wController/page");
|
|
|
|
defineArray.push("lib/wController/localModel");
|
2018-10-28 21:32:44 +00:00
|
|
|
defineArray.push("lib/wController/player");
|
2018-08-04 21:46:25 +00:00
|
|
|
|
|
|
|
defineArray.push("views/view");
|
|
|
|
defineArray.push("views/layout");
|
|
|
|
defineArray.push("views/gridLayout");
|
|
|
|
defineArray.push("views/page");
|
|
|
|
defineArray.push("views/mainLayout");
|
|
|
|
|
|
|
|
define(moduleName, defineArray, function lorgar_module() {
|
|
|
|
var Class = require("lib/utils/class");
|
2018-08-29 19:54:18 +00:00
|
|
|
var Enum = require("lib/utils/enum");
|
2018-08-04 21:46:25 +00:00
|
|
|
var Socket = require("lib/wSocket/socket");
|
|
|
|
var Dispatcher = require("lib/wDispatcher/dispatcher");
|
|
|
|
var Handler = require("lib/wDispatcher/handler");
|
|
|
|
var Logger = require("lib/wDispatcher/logger");
|
|
|
|
|
|
|
|
var Event = require("lib/wType/event");
|
|
|
|
var Address = require("lib/wType/address");
|
|
|
|
var Vocabulary = require("lib/wType/vocabulary");
|
|
|
|
var String = require("lib/wType/string");
|
|
|
|
|
|
|
|
var GlobalControls = require("lib/wController/globalControls");
|
|
|
|
var PageStorage = require("lib/wController/pageStorage");
|
|
|
|
var PageController = require("lib/wController/page");
|
|
|
|
var LocalModel = require("lib/wController/localModel");
|
2018-10-28 21:32:44 +00:00
|
|
|
var PlayerModel = require("lib/wController/player");
|
2018-08-04 21:46:25 +00:00
|
|
|
|
|
|
|
var View = require("views/view");
|
|
|
|
var Layout = require("views/layout");
|
|
|
|
var GridLayout = require("views/gridLayout");
|
|
|
|
var Page = require("views/page");
|
|
|
|
var MainLayout = require("views/mainLayout");
|
|
|
|
|
|
|
|
var Lorgar = Class.inherit({
|
|
|
|
"className": "Lorgar",
|
|
|
|
"constructor": function() {
|
|
|
|
Class.fn.constructor.call(this);
|
|
|
|
|
2018-08-29 19:54:18 +00:00
|
|
|
this._playerCtl = undefined;
|
2018-08-04 21:46:25 +00:00
|
|
|
this._currentPageCtl = undefined;
|
|
|
|
this._nodes = Object.create(null);
|
|
|
|
|
|
|
|
this._initDispatcher();
|
2018-08-29 19:54:18 +00:00
|
|
|
this._initModels();
|
|
|
|
this._initViews();
|
2018-08-04 21:46:25 +00:00
|
|
|
|
|
|
|
this._prepareNode("Magnus", "localhost", 8081);
|
|
|
|
this._prepareNode("Corax", "localhost", 8080);
|
|
|
|
|
2018-08-29 19:54:18 +00:00
|
|
|
this._registerModels();
|
2018-08-04 21:46:25 +00:00
|
|
|
|
|
|
|
this.connectNode("Magnus");
|
|
|
|
this.connectNode("Corax");
|
|
|
|
window.onpopstate = this._onHistoryPopState.bind(this)
|
|
|
|
},
|
|
|
|
"destructor": function() {
|
|
|
|
window.onpopstate = undefined;
|
2018-08-29 19:54:18 +00:00
|
|
|
this._unregisterModels();
|
|
|
|
|
2018-08-04 21:46:25 +00:00
|
|
|
if (this._currentPageCtl) {
|
|
|
|
this._currentPage.destructor();
|
|
|
|
this._currentPageCtl.destructor();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._gc.destructor();
|
|
|
|
this._ps.destructor();
|
|
|
|
this._emptyHelper.destructor();
|
|
|
|
|
|
|
|
this._body.destructor();
|
|
|
|
|
|
|
|
this.coraxSocket.close();
|
2018-08-29 19:54:18 +00:00
|
|
|
this.dispatcher.unregisterHandler(this._playerResponseHandler);
|
2018-08-04 21:46:25 +00:00
|
|
|
this.dispatcher.unregisterDefaultHandler(this._logger);
|
|
|
|
|
|
|
|
this._logger.destructor();
|
|
|
|
this.dispatcher.destructor();
|
2018-08-29 19:54:18 +00:00
|
|
|
this._playerResponseHandler.destructor();
|
2018-08-04 21:46:25 +00:00
|
|
|
//this.magnusSocket.destructor();
|
|
|
|
//this.coraxSocket.destructor();
|
|
|
|
|
|
|
|
Class.fn.destructor.call(this);
|
|
|
|
},
|
|
|
|
"changePage": function(addr) {
|
|
|
|
if (this._currentPageCtl && this._currentPageCtl.getPairAddress()["=="](addr)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._ps.getPageName(addr);
|
|
|
|
this._initPageController(addr.clone());
|
|
|
|
},
|
|
|
|
"connectNode": function(name) {
|
|
|
|
var node = this._nodes[name];
|
|
|
|
if (node === undefined) {
|
|
|
|
throw new Error("An attempt to connect not prepared node " + name);
|
|
|
|
}
|
|
|
|
|
|
|
|
node.socket.open(node.address, node.port);
|
2018-08-29 19:54:18 +00:00
|
|
|
node.state.setData(SocketState.straight.connecting);
|
|
|
|
},
|
|
|
|
_registerModels: function () {
|
|
|
|
this._gc.register(this.dispatcher, this._nodes.Magnus.socket);
|
|
|
|
this._ps.register(this.dispatcher, this._nodes.Magnus.socket);
|
2018-08-04 21:46:25 +00:00
|
|
|
},
|
2018-08-29 19:54:18 +00:00
|
|
|
_unregisterModels: function() {
|
|
|
|
if (this._currentPageCtl) {
|
|
|
|
this._currentPageCtl.unregister();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._gc.unregister();
|
|
|
|
this._ps.unregister();
|
2018-08-04 21:46:25 +00:00
|
|
|
},
|
|
|
|
"_initDispatcher": function() {
|
|
|
|
this.dispatcher = new Dispatcher();
|
|
|
|
this._logger = new Logger();
|
2018-08-29 19:54:18 +00:00
|
|
|
this._playerResponseHandler = new Handler(new Address(["getPlayer"]), this, this._responsePlayer);
|
2018-08-04 21:46:25 +00:00
|
|
|
this.dispatcher.registerDefaultHandler(this._logger);
|
2018-08-29 19:54:18 +00:00
|
|
|
this.dispatcher.registerHandler(this._playerResponseHandler);
|
2018-08-04 21:46:25 +00:00
|
|
|
},
|
|
|
|
"_initModels": function() {
|
|
|
|
this._gc = new GlobalControls(new Address(["magnus", "gc"]));
|
|
|
|
this._ps = new PageStorage(new Address(["magnus", "ps"]));
|
|
|
|
|
|
|
|
this._emptyHelper = new LocalModel();
|
|
|
|
|
2019-01-03 00:26:42 +00:00
|
|
|
this._gc.on("serviceMessage", this._onServiceMessage, this);
|
|
|
|
this._ps.on("serviceMessage", this._onServiceMessage, this);
|
|
|
|
|
2018-08-04 21:46:25 +00:00
|
|
|
this._gc.on("themeSelected", this.setTheme, this);
|
|
|
|
this._ps.on("pageName", this._onPageName, this);
|
|
|
|
},
|
|
|
|
"_initPageController": function(addr) {
|
|
|
|
if (this._currentPageCtl) {
|
|
|
|
this._currentPage.destructor();
|
|
|
|
this._currentPageCtl.destructor();
|
|
|
|
}
|
|
|
|
this._currentPageCtl = new PageController(addr);
|
|
|
|
this._currentPageCtl.register(this.dispatcher, this._nodes.Magnus.socket);
|
|
|
|
this._currentPage = new Page(this._currentPageCtl);
|
|
|
|
this._currentPageCtl.subscribe();
|
|
|
|
this._mainLayout.append(this._currentPage, 1, 1, 1, 1);
|
|
|
|
},
|
|
|
|
"_initViews": function() {
|
|
|
|
this._body = new Layout(this._emptyHelper);
|
|
|
|
this._mainLayout = new MainLayout(this._gc);
|
|
|
|
|
|
|
|
document.body.innerHTML = "";
|
|
|
|
document.body.appendChild(this._body._e);
|
2018-08-29 19:54:18 +00:00
|
|
|
window.addEventListener("resize", this._onWindowResize.bind(this), false);
|
2018-08-04 21:46:25 +00:00
|
|
|
|
|
|
|
this._body.setSize(document.body.offsetWidth, document.body.offsetHeight);
|
|
|
|
this._body.append(this._mainLayout);
|
|
|
|
},
|
|
|
|
"_onHistoryPopState": function(e) {
|
|
|
|
this._initPageController(new Address(e.state.address));
|
|
|
|
},
|
|
|
|
"_onPageName": function(name) {
|
|
|
|
window.history.pushState({
|
|
|
|
address: this._currentPageCtl.getPairAddress().toArray()
|
|
|
|
}, "", name);
|
|
|
|
},
|
2019-01-03 00:26:42 +00:00
|
|
|
"_onServiceMessage": function(text, severity) {
|
|
|
|
var fn;
|
|
|
|
|
|
|
|
switch (severity) {
|
|
|
|
case 2:
|
|
|
|
fn = console.error;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
fn = console.warn;
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
fn = console.info;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn(text);
|
|
|
|
},
|
2018-08-04 21:46:25 +00:00
|
|
|
"_onSocketConnected": function(name) {
|
|
|
|
console.log(name + " socket connected");
|
|
|
|
var node = this._nodes[name];
|
|
|
|
node.connected = true;
|
2018-08-29 19:54:18 +00:00
|
|
|
node.state.setData(SocketState.straight.connected);
|
2018-08-04 21:46:25 +00:00
|
|
|
|
|
|
|
for (var id in node.foreigns) {
|
|
|
|
if (node.foreigns[id].subscribed) {
|
|
|
|
node.foreigns[id].controller.subscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 19:54:18 +00:00
|
|
|
switch (name) {
|
|
|
|
case "Magnus":
|
|
|
|
this._gc.subscribe();
|
2018-08-04 21:46:25 +00:00
|
|
|
|
2018-08-29 19:54:18 +00:00
|
|
|
if (!this._currentPageCtl) {
|
|
|
|
this._ps.getPageAddress(location.pathname);
|
|
|
|
this._ps.one("pageAddress", this._initPageController, this);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "Corax":
|
|
|
|
this._requestPlayer();
|
|
|
|
break;
|
2018-08-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"_onSocketDisconnected": function(name) {
|
|
|
|
console.log(name + " socket disconnected");
|
|
|
|
var node = this._nodes[name];
|
|
|
|
node.connected = false;
|
|
|
|
|
2018-10-28 21:32:44 +00:00
|
|
|
switch (name) {
|
|
|
|
case "Corax":
|
|
|
|
if (this._playerCtl) {
|
|
|
|
this._mainLayout.removePlayer();
|
|
|
|
this._playerCtl._onSocketDisconnected();
|
2018-12-02 15:07:43 +00:00
|
|
|
this._playerCtl.unregister();
|
2018-10-28 21:32:44 +00:00
|
|
|
this._playerCtl.destructor();
|
|
|
|
this._playerCtl = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-04 21:46:25 +00:00
|
|
|
for (var id in node.foreigns) {
|
|
|
|
if (node.foreigns[id].subscribed) {
|
2018-10-28 21:32:44 +00:00
|
|
|
node.foreigns[id].controller._onSocketDisconnected();
|
2018-08-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-29 19:54:18 +00:00
|
|
|
node.state.setData(SocketState.straight.disconnected);
|
2018-08-04 21:46:25 +00:00
|
|
|
},
|
2018-08-29 19:54:18 +00:00
|
|
|
"_onSocketError": function(name, e) {
|
2018-08-04 21:46:25 +00:00
|
|
|
console.log(name + " socket error: ");
|
|
|
|
console.log(e);
|
|
|
|
},
|
|
|
|
"_onWindowResize": function() {
|
|
|
|
this._body.setSize(document.body.offsetWidth, document.body.offsetHeight);
|
|
|
|
},
|
2018-11-28 06:54:14 +00:00
|
|
|
"play": function(id) {
|
2018-11-29 19:41:53 +00:00
|
|
|
if (this._nodes.Corax && this._nodes.Corax.connected) {
|
|
|
|
var vc = new Vocabulary();
|
|
|
|
vc.insert("id", id.clone());
|
|
|
|
|
2019-02-01 22:24:09 +00:00
|
|
|
var ev = new Event(this._playerCtl.getPairAddress()["+="](play), vc);
|
2018-11-29 19:41:53 +00:00
|
|
|
|
|
|
|
var socket = this._nodes.Corax.socket;
|
|
|
|
ev.setSenderId(socket.getId().clone());
|
|
|
|
socket.send(ev);
|
|
|
|
ev.destructor();
|
|
|
|
}
|
2018-11-28 06:54:14 +00:00
|
|
|
},
|
2018-08-04 21:46:25 +00:00
|
|
|
"_prepareNode": function(name, address, port) {
|
|
|
|
if (this._nodes[name]) {
|
|
|
|
throw new Error("An attempt to prepeare node " + name + " for the second time");
|
|
|
|
}
|
|
|
|
var obj = Object.create(null);
|
|
|
|
obj.name = name;
|
|
|
|
obj.address = address;
|
|
|
|
obj.port = port;
|
|
|
|
obj.socket = new Socket("Lorgar");
|
|
|
|
obj.connected = false;
|
|
|
|
obj.foreigns = Object.create(null);
|
2018-08-29 19:54:18 +00:00
|
|
|
obj.state = new LocalModel({fontFamily: "casualFont"});
|
|
|
|
obj.state.enum = SocketState;
|
|
|
|
obj.state.setData(SocketState.straight.disconnected);
|
2018-08-04 21:46:25 +00:00
|
|
|
|
|
|
|
obj.socket.on("connected", this._onSocketConnected.bind(this, name));
|
|
|
|
obj.socket.on("disconnected", this._onSocketDisconnected.bind(this, name));
|
|
|
|
obj.socket.on("error", this._onSocketError.bind(this, name));
|
|
|
|
obj.socket.on("message", this.dispatcher.pass, this.dispatcher);
|
|
|
|
|
2018-08-29 19:54:18 +00:00
|
|
|
this._mainLayout.addState(name, obj.state);
|
|
|
|
|
2018-08-04 21:46:25 +00:00
|
|
|
this._nodes[name] = obj;
|
|
|
|
},
|
2018-10-28 21:32:44 +00:00
|
|
|
"registerForeignController": function(nodeName, controller) {
|
|
|
|
var node = this._nodes[nodeName];
|
2018-08-04 21:46:25 +00:00
|
|
|
if (node === undefined) {
|
2018-10-28 21:32:44 +00:00
|
|
|
throw new Error("An attempt to register controller to an unknown node " + nodeName);
|
2018-08-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (node.foreigns[controller.id] !== undefined) {
|
2018-10-28 21:32:44 +00:00
|
|
|
throw new Error("An attempt to register a controller under node " + nodeName + " for a second time");
|
2018-08-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
var obj = Object.create(null);
|
|
|
|
obj.controller = controller;
|
|
|
|
obj.subscribed = false;
|
|
|
|
node.foreigns[controller.id] = obj;
|
|
|
|
controller.register(this.dispatcher, node.socket);
|
|
|
|
},
|
2018-08-29 19:54:18 +00:00
|
|
|
"_requestPlayer": function() {
|
|
|
|
var vc = new Vocabulary();
|
|
|
|
vc.insert("source", new Address([]));
|
|
|
|
|
|
|
|
var ev = new Event(new Address(["management", "givePlayer"]), vc);
|
|
|
|
|
|
|
|
var socket = this._nodes.Corax.socket;
|
|
|
|
ev.setSenderId(socket.getId().clone());
|
|
|
|
socket.send(ev);
|
|
|
|
ev.destructor();
|
|
|
|
},
|
|
|
|
"_responsePlayer": function(ev) {
|
|
|
|
var data = ev.getData();
|
|
|
|
|
2018-10-28 21:32:44 +00:00
|
|
|
this._playerCtl = new PlayerModel(data.at("address").clone());
|
|
|
|
this._playerCtl.register(this.dispatcher, this._nodes["Corax"].socket);
|
|
|
|
this._playerCtl.subscribe();
|
|
|
|
this._mainLayout.appendPlayer(this._playerCtl);
|
2019-01-03 00:26:42 +00:00
|
|
|
|
|
|
|
this._playerCtl.on("serviceMessage", this._onServiceMessage, this);
|
2018-08-29 19:54:18 +00:00
|
|
|
},
|
2019-02-01 22:24:09 +00:00
|
|
|
"scheduleToPlay": function(id) {
|
|
|
|
if (this._nodes.Corax && this._nodes.Corax.connected) {
|
|
|
|
var vc = new Vocabulary();
|
|
|
|
vc.insert("id", id.clone());
|
|
|
|
|
|
|
|
var ev = new Event(this._playerCtl.getPairAddress()["+="](queue), vc);
|
|
|
|
|
|
|
|
var socket = this._nodes.Corax.socket;
|
|
|
|
ev.setSenderId(socket.getId().clone());
|
|
|
|
socket.send(ev);
|
|
|
|
ev.destructor();
|
|
|
|
}
|
|
|
|
},
|
2018-08-04 21:46:25 +00:00
|
|
|
"setTheme": function(theme) {
|
|
|
|
View.setTheme(theme);
|
|
|
|
},
|
2018-10-28 21:32:44 +00:00
|
|
|
"subscribeForeignController": function(nodeName, controller) {
|
|
|
|
var node = this._nodes[nodeName];
|
2018-08-04 21:46:25 +00:00
|
|
|
if (node === undefined) {
|
2018-10-28 21:32:44 +00:00
|
|
|
throw new Error("An attempt to subscribe a controller to an unknown node " + nodeName);
|
2018-08-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (node.foreigns[controller.id] === undefined) {
|
2018-10-28 21:32:44 +00:00
|
|
|
throw new Error("An attempt to subscribe not registered controller to node " + nodeName);
|
2018-08-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
node.foreigns[controller.id].subscribed = true;
|
|
|
|
controller.subscribe();
|
|
|
|
},
|
2018-10-28 21:32:44 +00:00
|
|
|
"unregisterForeignController": function(nodeName, controller) {
|
|
|
|
var node = this._nodes[nodeName];
|
2018-08-04 21:46:25 +00:00
|
|
|
if (node === undefined) {
|
2018-10-28 21:32:44 +00:00
|
|
|
throw new Error("An attempt to unregister a controller from an unknown node " + nodeName);
|
2018-08-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (node.foreigns[controller.id] === undefined) {
|
2018-10-28 21:32:44 +00:00
|
|
|
throw new Error("An attempt to unregister not registered controller from node " + nodeName);
|
2018-08-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
delete node.foreigns[controller.id];
|
|
|
|
controller.unregister();
|
|
|
|
},
|
2018-10-28 21:32:44 +00:00
|
|
|
"unsubscribeForeignController": function(nodeName, controller) {
|
|
|
|
var node = this._nodes[nodeName];
|
2018-08-04 21:46:25 +00:00
|
|
|
if (node === undefined) {
|
2018-10-28 21:32:44 +00:00
|
|
|
throw new Error("An attempt to unsubscribe a controller from an unknown node " + nodeName);
|
2018-08-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (node.foreigns[controller.id] === undefined) {
|
2018-10-28 21:32:44 +00:00
|
|
|
throw new Error("An attempt to unsubscribe not registered controller from node " + nodeName);
|
2018-08-04 21:46:25 +00:00
|
|
|
}
|
|
|
|
node.foreigns[controller.id].subscribed = false;
|
|
|
|
controller.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-29 19:54:18 +00:00
|
|
|
var SocketState = new Enum("SocketState", ["description"]);
|
|
|
|
SocketState.add("disconnected", {description: "Socket is disconnected"});
|
|
|
|
SocketState.add("connecting", {description: "Socket is connecting to remote host"});
|
|
|
|
SocketState.add("connected", {description: "Socket is connected"});
|
|
|
|
|
2018-11-29 19:41:53 +00:00
|
|
|
var queue = new Address(["queue"]);
|
2019-02-01 22:24:09 +00:00
|
|
|
var play = new Address(["play"]);
|
2018-11-29 19:41:53 +00:00
|
|
|
|
2018-08-04 21:46:25 +00:00
|
|
|
return Lorgar;
|
|
|
|
});
|
|
|
|
})();
|