a new oblect utils - enum, enum view, refactoring of mainLayout, debugging, states for sockets
This commit is contained in:
parent
b5ea024faf
commit
f4529f0023
14 changed files with 260 additions and 57 deletions
|
@ -4,6 +4,7 @@
|
|||
|
||||
var defineArray = [];
|
||||
defineArray.push("lib/utils/class");
|
||||
defineArray.push("lib/utils/enum");
|
||||
defineArray.push("lib/wSocket/socket");
|
||||
defineArray.push("lib/wDispatcher/dispatcher");
|
||||
defineArray.push("lib/wDispatcher/handler");
|
||||
|
@ -27,6 +28,7 @@
|
|||
|
||||
define(moduleName, defineArray, function lorgar_module() {
|
||||
var Class = require("lib/utils/class");
|
||||
var Enum = require("lib/utils/enum");
|
||||
var Socket = require("lib/wSocket/socket");
|
||||
var Dispatcher = require("lib/wDispatcher/dispatcher");
|
||||
var Handler = require("lib/wDispatcher/handler");
|
||||
|
@ -53,16 +55,18 @@
|
|||
"constructor": function() {
|
||||
Class.fn.constructor.call(this);
|
||||
|
||||
this._playerCtl = undefined;
|
||||
this._currentPageCtl = undefined;
|
||||
this._nodes = Object.create(null);
|
||||
|
||||
this._initDispatcher();
|
||||
this._initModels();
|
||||
this._initViews();
|
||||
|
||||
this._prepareNode("Magnus", "localhost", 8081);
|
||||
this._prepareNode("Corax", "localhost", 8080);
|
||||
|
||||
this._initModels();
|
||||
this._initViews();
|
||||
this._registerModels();
|
||||
|
||||
this.connectNode("Magnus");
|
||||
this.connectNode("Corax");
|
||||
|
@ -70,6 +74,8 @@
|
|||
},
|
||||
"destructor": function() {
|
||||
window.onpopstate = undefined;
|
||||
this._unregisterModels();
|
||||
|
||||
if (this._currentPageCtl) {
|
||||
this._currentPage.destructor();
|
||||
this._currentPageCtl.destructor();
|
||||
|
@ -77,16 +83,17 @@
|
|||
|
||||
this._gc.destructor();
|
||||
this._ps.destructor();
|
||||
this._mainColorHelper.destructor();
|
||||
this._emptyHelper.destructor();
|
||||
|
||||
this._body.destructor();
|
||||
|
||||
this.coraxSocket.close();
|
||||
this.dispatcher.unregisterHandler(this._playerResponseHandler);
|
||||
this.dispatcher.unregisterDefaultHandler(this._logger);
|
||||
|
||||
this._logger.destructor();
|
||||
this.dispatcher.destructor();
|
||||
this._playerResponseHandler.destructor();
|
||||
//this.magnusSocket.destructor();
|
||||
//this.coraxSocket.destructor();
|
||||
|
||||
|
@ -106,31 +113,34 @@
|
|||
}
|
||||
|
||||
node.socket.open(node.address, node.port);
|
||||
node.state.setData(SocketState.straight.connecting);
|
||||
},
|
||||
"_initCoraxSocket": function() {
|
||||
this.coraxSocket = new Socket("Lorgar");
|
||||
this.coraxSocket.on("connected", this._coraxSocketConnected, this);
|
||||
this.coraxSocket.on("disconnected", this._coraxSocketDisconnected, this);
|
||||
this.coraxSocket.on("error", this._coraxSocketError, this);
|
||||
this.coraxSocket.on("message", this.dispatcher.pass, this.dispatcher);
|
||||
_registerModels: function () {
|
||||
this._gc.register(this.dispatcher, this._nodes.Magnus.socket);
|
||||
this._ps.register(this.dispatcher, this._nodes.Magnus.socket);
|
||||
},
|
||||
_unregisterModels: function() {
|
||||
if (this._currentPageCtl) {
|
||||
this._currentPageCtl.unregister();
|
||||
}
|
||||
|
||||
this._gc.unregister();
|
||||
this._ps.unregister();
|
||||
},
|
||||
"_initDispatcher": function() {
|
||||
this.dispatcher = new Dispatcher();
|
||||
this._logger = new Logger();
|
||||
this._playerResponseHandler = new Handler(new Address(["getPlayer"]), this, this._responsePlayer);
|
||||
this.dispatcher.registerDefaultHandler(this._logger);
|
||||
this.dispatcher.registerHandler(this._playerResponseHandler);
|
||||
},
|
||||
"_initModels": function() {
|
||||
this._gc = new GlobalControls(new Address(["magnus", "gc"]));
|
||||
this._ps = new PageStorage(new Address(["magnus", "ps"]));
|
||||
|
||||
this._mainColorHelper = new LocalModel({backgroundColor: "mainColor"});
|
||||
this._emptyHelper = new LocalModel();
|
||||
|
||||
this._gc.on("themeSelected", this.setTheme, this);
|
||||
|
||||
this._gc.register(this.dispatcher, this._nodes.Magnus.socket);
|
||||
this._ps.register(this.dispatcher, this._nodes.Magnus.socket);
|
||||
|
||||
this._ps.on("pageName", this._onPageName, this);
|
||||
},
|
||||
"_initPageController": function(addr) {
|
||||
|
@ -150,18 +160,10 @@
|
|||
|
||||
document.body.innerHTML = "";
|
||||
document.body.appendChild(this._body._e);
|
||||
window.addEventListener("resize",this._onWindowResize.bind(this) ,false);
|
||||
window.addEventListener("resize", this._onWindowResize.bind(this), false);
|
||||
|
||||
this._body.setSize(document.body.offsetWidth, document.body.offsetHeight);
|
||||
this._body.append(this._mainLayout);
|
||||
var spacerL = new View(this._mainColorHelper, {
|
||||
maxWidth: 50
|
||||
});
|
||||
var spacerR = new View(this._mainColorHelper, {
|
||||
maxWidth: 50
|
||||
});
|
||||
this._mainLayout.append(spacerL, 1, 0, 1, 1);
|
||||
this._mainLayout.append(spacerR, 1, 2, 1, 1);
|
||||
},
|
||||
"_onHistoryPopState": function(e) {
|
||||
this._initPageController(new Address(e.state.address));
|
||||
|
@ -175,6 +177,7 @@
|
|||
console.log(name + " socket connected");
|
||||
var node = this._nodes[name];
|
||||
node.connected = true;
|
||||
node.state.setData(SocketState.straight.connected);
|
||||
|
||||
for (var id in node.foreigns) {
|
||||
if (node.foreigns[id].subscribed) {
|
||||
|
@ -182,13 +185,18 @@
|
|||
}
|
||||
}
|
||||
|
||||
if (name === "Magnus") {
|
||||
this._gc.subscribe();
|
||||
switch (name) {
|
||||
case "Magnus":
|
||||
this._gc.subscribe();
|
||||
|
||||
if (!this._currentPageCtl) {
|
||||
this._ps.getPageAddress(location.pathname);
|
||||
this._ps.one("pageAddress", this._initPageController, this);
|
||||
}
|
||||
if (!this._currentPageCtl) {
|
||||
this._ps.getPageAddress(location.pathname);
|
||||
this._ps.one("pageAddress", this._initPageController, this);
|
||||
}
|
||||
break;
|
||||
case "Corax":
|
||||
this._requestPlayer();
|
||||
break;
|
||||
}
|
||||
},
|
||||
"_onSocketDisconnected": function(name) {
|
||||
|
@ -201,8 +209,9 @@
|
|||
node.foreigns[id].controller._onSocketDisconnected;
|
||||
}
|
||||
}
|
||||
node.state.setData(SocketState.straight.disconnected);
|
||||
},
|
||||
"_onSocketError": function(name) {
|
||||
"_onSocketError": function(name, e) {
|
||||
console.log(name + " socket error: ");
|
||||
console.log(e);
|
||||
},
|
||||
|
@ -220,12 +229,17 @@
|
|||
obj.socket = new Socket("Lorgar");
|
||||
obj.connected = false;
|
||||
obj.foreigns = Object.create(null);
|
||||
obj.state = new LocalModel({fontFamily: "casualFont"});
|
||||
obj.state.enum = SocketState;
|
||||
obj.state.setData(SocketState.straight.disconnected);
|
||||
|
||||
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);
|
||||
|
||||
this._mainLayout.addState(name, obj.state);
|
||||
|
||||
this._nodes[name] = obj;
|
||||
},
|
||||
"registerForeignController": function(node, controller) {
|
||||
|
@ -243,6 +257,22 @@
|
|||
node.foreigns[controller.id] = obj;
|
||||
controller.register(this.dispatcher, node.socket);
|
||||
},
|
||||
"_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();
|
||||
|
||||
console.log('Received player address: ' + data.at("address").toString());
|
||||
},
|
||||
"setTheme": function(theme) {
|
||||
View.setTheme(theme);
|
||||
},
|
||||
|
@ -284,6 +314,11 @@
|
|||
}
|
||||
});
|
||||
|
||||
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"});
|
||||
|
||||
return Lorgar;
|
||||
});
|
||||
})();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue