diff --git a/README.md b/README.md index b96f96b..4308fe8 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Esiur.JS -Esiur Library for Javascript +# Esyur.JS +Esyur Library for Javascript # Usage \ No newline at end of file diff --git a/module.js b/module.js index 0f978fa..4c433c6 100644 --- a/module.js +++ b/module.js @@ -1,5 +1,5 @@ exports.printMsg = function() { - console.log("Esiur 1.1"); + console.log("Esyur 1.1"); } module.exports = { wh }; diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..40b878d --- /dev/null +++ b/src/.gitignore @@ -0,0 +1 @@ +node_modules/ \ No newline at end of file diff --git a/src/Core/AsyncException.js b/src/Core/AsyncException.js index 66749ad..03f5308 100644 --- a/src/Core/AsyncException.js +++ b/src/Core/AsyncException.js @@ -36,7 +36,7 @@ export default class AsyncException extends Error raise(type, code, message) { - this.type = (type == 0 ? "Management" : "Execusion"); + this.type = type; this.code = code; if (type == 0) @@ -56,6 +56,6 @@ export default class AsyncException extends Error toString() { - return this.type + " (" + this.code + ") : " + this.message; + return (this.type == 0 ? "Management" : "Exception") + " (" + this.code + ") : " + this.message; } } \ No newline at end of file diff --git a/src/Core/AsyncReply.js b/src/Core/AsyncReply.js index a9441a9..5cf72b7 100644 --- a/src/Core/AsyncReply.js +++ b/src/Core/AsyncReply.js @@ -30,13 +30,21 @@ import AsyncException from './AsyncException.js'; export default class AsyncReply extends Promise { - then(callback) + then(callback, onError) { - this.callbacks.push(callback); - - if (this.ready) - callback(this.result, this); - + if (callback != undefined) + { + this.callbacks.push(callback); + + if (this.ready) + callback(this.result, this); + } + + if (onError != undefined) + { + this.error(onError); + } + return this; } @@ -46,11 +54,6 @@ export default class AsyncReply extends Promise this.then(callback); } - // Alias for error() - catch(callback) - { - return this.error(callback); - } error(callback) { @@ -103,7 +106,7 @@ export default class AsyncReply extends Promise this.exception.raise(type, code, message); if (this.errorCallbacks.length == 0) - throw this.exception; + throw this.exception; else for(var i = 0; i < this.errorCallbacks.length; i++) this.errorCallbacks[i](this.exception, this); diff --git a/src/Data/Codec.js b/src/Data/Codec.js index a90373e..c922e24 100644 --- a/src/Data/Codec.js +++ b/src/Data/Codec.js @@ -200,7 +200,7 @@ export default class Codec { } static parseResource(data, offset) { - return Warehouse.get(data.getUint32(offset)); + return Warehouse.getById(data.getUint32(offset)); } static parseDistributedResource(data, offset, connection) { @@ -210,7 +210,7 @@ export default class Codec { // find the object var iid = data.getUint32(offset); - return connection.fetch(iid);// Warehouse.Get(iid); + return connection.fetch(iid);// Warehouse.getById(iid); } /// @@ -241,7 +241,7 @@ export default class Codec { previous = new AsyncReply(null); else if (result == ResourceComparisonResult.Local) { - previous = Warehouse.get(data.getUint32(offset)); + previous = Warehouse.getById(data.getUint32(offset)); offset += 4; } else if (result == ResourceComparisonResult.Distributed) @@ -269,7 +269,7 @@ export default class Codec { } else if (result == ResourceComparisonResult.Local) { - current = Warehouse.get(data.getUint32(offset)); + current = Warehouse.getById(data.getUint32(offset)); offset += 4; } else if (result == ResourceComparisonResult.Distributed) @@ -766,7 +766,7 @@ static isLocalResource(resource, connection) { -static getDataType(value) { +static getDataType(value, connection) { switch (typeof value) { case "number": // float or ? diff --git a/src/Net/IIP/DistributedConnection.js b/src/Net/IIP/DistributedConnection.js index b65a821..5126917 100644 --- a/src/Net/IIP/DistributedConnection.js +++ b/src/Net/IIP/DistributedConnection.js @@ -68,9 +68,17 @@ import { ResourceTrigger } from '../../Resource/IResource.js'; export default class DistributedConnection extends IStore { + send(data) { - //console.log("Send", data.length); - this.socket.send(data.buffer); + + if (this.holdSending) + { + //console.log("hold ", data.length); + this.sendBuffer.writeAll(data); + } + else + //console.log("Send", data.length); + this.socket.send(data.buffer); } sendParams(doneReply) { @@ -516,6 +524,25 @@ export default class DistributedConnection extends IStore { } } + hold() + { + this.holdSending = true; + } + + unhold() + { + if (this.holdSending) + { + var msg = this.sendBuffer.read(); + + if (msg == null || msg.length == 0) + return; + + this.socket.send(msg); + this.holdSending = false; + } + } + trigger(trigger) { if (trigger == ResourceTrigger.Open) @@ -576,9 +603,10 @@ export default class DistributedConnection extends IStore { self.lastAction = new Date(); + self.hold(); while (this.networkBuffer.available > 0 && !this.networkBuffer.protected) self.receive(this.networkBuffer); - + self.unhold(); }; @@ -873,7 +901,7 @@ export default class DistributedConnection extends IStore { var self = this; - Warehouse.get(resourceId).then(function (r) { + Warehouse.getById(resourceId).then(function (r) { if (r != null) { if (r.instance.applicable(self.session, ActionType.Attach, null) == Ruling.Denied) @@ -916,7 +944,7 @@ export default class DistributedConnection extends IStore { IIPRequestReattachResource(callback, resourceId, resourceAge) { var self = this; - Warehouse.get(resourceId).then(function (r) { + Warehouse.getById(resourceId).then(function (r) { if (res != null) { r.instance.on("ResourceEventOccurred", self.instance_eventOccurred, self); r.instance.on("ResourceModified", self.instance_propertyModified, self); @@ -937,7 +965,7 @@ export default class DistributedConnection extends IStore { IIPRequestDetachResource(callback, resourceId) { var self = this; - Warehouse.get(resourceId).then(function (r) { + Warehouse.getById(resourceId).then(function (r) { if (r != null) { r.instance.off("ResourceEventOccurred", self.instance_eventOccurred); r.instance.off("ResourceModified", self.instance_propertyModified); @@ -955,7 +983,7 @@ export default class DistributedConnection extends IStore { IIPRequestCreateResource(callback, storeId, parentId, content) { var self = this; - Warehouse.get(storeId).then(function(store) + Warehouse.getById(storeId).then(function(store) { if (store == null) { @@ -976,7 +1004,7 @@ export default class DistributedConnection extends IStore { return; } - Warehouse.get(parentId).then(function(parent) + Warehouse.getById(parentId).then(function(parent) { // check security @@ -1039,7 +1067,7 @@ export default class DistributedConnection extends IStore { IIPRequestDeleteResource(callback, resourceId) { var self = this; - Warehouse.get(resourceId).then(function(r) + Warehouse.getById(resourceId).then(function(r) { if (r == null) { @@ -1096,7 +1124,7 @@ export default class DistributedConnection extends IStore { var self = this; - Warehouse.get(resourceId).then(function (r) { + Warehouse.getById(resourceId).then(function (r) { if (r != null) self.sendReply(IIPPacketAction.TemplateFromResourceId, callback) .addUint32(r.instance.template.content.length) @@ -1113,7 +1141,7 @@ export default class DistributedConnection extends IStore { var self = this; - Warehouse.get(resourceId).then(function (r) { + Warehouse.getById(resourceId).then(function (r) { if (r != null) { Codec.parseVarArray(content, 0, content.length, self).then(function (args) { var ft = r.instance.template.getFunctionTemplateByIndex(index); @@ -1194,7 +1222,7 @@ export default class DistributedConnection extends IStore { var self = this; - Warehouse.get(resourceId).then(function (r) { + Warehouse.getById(resourceId).then(function (r) { if (r != null) { Codec.parseStructure(content, 0, content.Length, self).then(function (namedArgs) { var ft = r.instance.template.getFunctionTemplateByIndex(index); @@ -1282,7 +1310,7 @@ export default class DistributedConnection extends IStore { var self = this; - Warehouse.get(resourceId).then(function (r) { + Warehouse.getById(resourceId).then(function (r) { if (r != null) { var pt = r.instance.template.getFunctionTemplateByIndex(index); if (pt != null) { @@ -1312,7 +1340,7 @@ export default class DistributedConnection extends IStore { var self = this; - Warehouse.get(resourceId).then(function (r) { + Warehouse.getById(resourceId).then(function (r) { if (r != null) { var pt = r.instance.template.getFunctionTemplateByIndex(index); if (pt != null) { @@ -1343,7 +1371,7 @@ export default class DistributedConnection extends IStore { var self = this; - Warehouse.get(resourceId).then(function (r) { + Warehouse.getById(resourceId).then(function (r) { if (r != null) { @@ -1399,7 +1427,7 @@ export default class DistributedConnection extends IStore { IIPRequestInquireResourceHistory(callback, resourceId, fromDate, toDate) { var self = this; - Warehouse.get(resourceId).then(function(r) + Warehouse.getById(resourceId).then(function(r) { if (r != null) { @@ -1678,7 +1706,7 @@ export default class DistributedConnection extends IStore { IIPRequestAddChild(callback, parentId, childId) { var self = this; - Warehouse.get(parentId).then(function(parent) + Warehouse.getById(parentId).then(function(parent) { if (parent == null) { @@ -1686,7 +1714,7 @@ export default class DistributedConnection extends IStore { return; } - Warehouse.get(childId).then(function(child) + Warehouse.getById(childId).then(function(child) { if (child == null) { @@ -1720,7 +1748,7 @@ export default class DistributedConnection extends IStore { { var self = this; - Warehouse.get(parentId).then(function(parent) + Warehouse.getById(parentId).then(function(parent) { if (parent == null) { @@ -1728,7 +1756,7 @@ export default class DistributedConnection extends IStore { return; } - Warehouse.get(childId).then(function(child) + Warehouse.getById(childId).then(function(child) { if (child == null) { @@ -1761,7 +1789,7 @@ export default class DistributedConnection extends IStore { IIPRequestRenameResource(callback, resourceId, name) { var self = this; - Warehouse.get(resourceId).then(function(resource) + Warehouse.getById(resourceId).then(function(resource) { if (resource == null) { @@ -1784,7 +1812,7 @@ export default class DistributedConnection extends IStore { IIPRequestResourceChildren(callback, resourceId) { var self = this; - Warehouse.get(resourceId).then(function(resource) + Warehouse.getById(resourceId).then(function(resource) { if (resource == null) { @@ -1803,7 +1831,7 @@ export default class DistributedConnection extends IStore { { var self = this; - Warehouse.get(resourceId).then(function(resource) + Warehouse.getById(resourceId).then(function(resource) { if (resource == null) { @@ -1819,7 +1847,7 @@ export default class DistributedConnection extends IStore { IIPRequestClearAttributes(callback, resourceId, attributes, all = false) { - Warehouse.get(resourceId).then(function(r) + Warehouse.getById(resourceId).then(function(r) { if (r == null) { @@ -1851,7 +1879,7 @@ export default class DistributedConnection extends IStore { { var self = this; - Warehouse.get(resourceId).then(function(r) + Warehouse.getById(resourceId).then(function(r) { if (r == null) { diff --git a/src/Net/Packets/IIPPacket.js b/src/Net/Packets/IIPPacket.js index b9946c0..27a66a4 100644 --- a/src/Net/Packets/IIPPacket.js +++ b/src/Net/Packets/IIPPacket.js @@ -59,7 +59,8 @@ export default class IIPPacket { if (offset + needed > ends) { - this.dataLengthNeeded = needed - (ends - this.originalOffset); + this.dataLengthNeeded = needed - (ends - offset); +// this.dataLengthNeeded = needed - (ends - this.originalOffset); return true; } else diff --git a/src/Resource/IResource.js b/src/Resource/IResource.js index 0bafc0a..742b7ae 100644 --- a/src/Resource/IResource.js +++ b/src/Resource/IResource.js @@ -54,7 +54,7 @@ export default class IResource extends IDestructible static getTemplate() { return { - namespace: "Esiur", + namespace: "Esyur", properties: [], functions: [], events: [] diff --git a/src/Resource/Warehouse.js b/src/Resource/Warehouse.js index b1dd2df..eac8951 100644 --- a/src/Resource/Warehouse.js +++ b/src/Resource/Warehouse.js @@ -60,82 +60,78 @@ export class WH extends IEventHandler return res; } + getById(id) + { + return new AsyncReply(this.resources.item(id)); + } + get(id, attributes = null, parent = null, manager = null) { - if (Number.isInteger(id)) + + var p = id.split('/'); + var res = null; + + for(var s = 0; s < this.stores.length; s++) { - //if (Warehouse.resources.contains(id)) - return new AsyncReply(this.resources.item(id)); - //else - // return null; - } - else - { - var p = id.split('/'); - var res = null; - - for(var s = 0; s < this.stores.length; s++) + var d = this.stores.at(s); + if (p[0] == d.instance.name) { - var d = this.stores.at(s); - if (p[0] == d.instance.name) + var i = 1; + res = d; + while(p.length > i) { - var i = 1; - res = d; - while(p.length > i) - { - var si = i; + var si = i; - for (var r = 0; r < res.instance.children.length; r++) - if (res.instance.children.item(r).instance.name == p[i]) - { - i++; - res = res.instance.children.item(r); - break; - } + for (var r = 0; r < res.instance.children.length; r++) + if (res.instance.children.item(r).instance.name == p[i]) + { + i++; + res = res.instance.children.item(r); + break; + } - if (si == i) - // not found, ask the store - return d.get(id.substring(p[0].length + 1)); - } - - return new AsyncReply(res); - } - } - - // Should we create a new store ? - if (id.includes("://")) - { - var url = id.split("://", 2); - var hostname = url[1].split("/", 2)[0]; - var pathname = url[1].split("/").splice(1).join("/"); - var handler; - - var rt = new AsyncReply(); - var self = this; - - if (handler = this.protocols.item(url[0])) - { - var store = handler(); - this.put(store, url[0] + "://" + hostname, null, parent, null, 0, manager, attributes); - store.trigger(ResourceTrigger.Open).then(x=>{ - if (pathname.length > 0 && pathname != "") - store.get(pathname).then(r=>{ - rt.trigger(r); - }).error(e => rt.triggerError(e)); - else - rt.trigger(store); - - }).error(e => { - rt.triggerError(e); - self.remove(store); - }); + if (si == i) + // not found, ask the store + return d.get(id.substring(p[0].length + 1)); } - return rt; + return new AsyncReply(res); + } + } + + // Should we create a new store ? + if (id.includes("://")) + { + var url = id.split("://", 2); + var hostname = url[1].split("/", 2)[0]; + var pathname = url[1].split("/").splice(1).join("/"); + var handler; + + var rt = new AsyncReply(); + var self = this; + + if (handler = this.protocols.item(url[0])) + { + var store = handler(); + this.put(store, url[0] + "://" + hostname, null, parent, null, 0, manager, attributes); + store.trigger(ResourceTrigger.Open).then(x=>{ + if (pathname.length > 0 && pathname != "") + store.get(pathname).then(r=>{ + rt.trigger(r); + }).error(e => rt.triggerError(e)); + else + rt.trigger(store); + + }).error(e => { + rt.triggerError(e); + self.remove(store); + }); } - return new AsyncReply(null); + return rt; } + + return new AsyncReply(null); } diff --git a/src/package-lock.json b/src/package-lock.json index 99dd3d5..77c62ad 100644 --- a/src/package-lock.json +++ b/src/package-lock.json @@ -1,5 +1,5 @@ { - "name": "esiur", + "name": "esyur", "version": "1.2.2", "lockfileVersion": 1, "requires": true, diff --git a/src/package.json b/src/package.json index 02716cb..e910afb 100644 --- a/src/package.json +++ b/src/package.json @@ -1,21 +1,21 @@ { - "name": "esiur", + "name": "Esyur", "version": "1.3.0", "description": "Distributed Object Framework", - "main": "esiur.js", + "main": "Esyur.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", - "url": "git+https://github.com/esiur/esiur-js.git" + "url": "git+https://github.com/esyur/esyur-js.git" }, "author": "Ahmed Zamil", "license": "MIT", "bugs": { - "url": "https://github.com/esiur/esiur-js/issues" + "url": "https://github.com/esyur/esyur-js/issues" }, - "homepage": "https://github.com/esiur/esiur-js#readme", + "homepage": "https://github.com/esyur/esyur-js#readme", "dependencies": { "ws": "^6.2.0" } diff --git a/test/test.js b/test/test.js index 2229388..ddad4b3 100644 --- a/test/test.js +++ b/test/test.js @@ -10,7 +10,8 @@ class MyStore extends IStore async function load() { - window.x = await wh.get("iip://localhost:5001/db/my", {username: "demo", password: "1234"}); +// window.x = await wh.get("iip://localhost:5001/db/my", {username: "demo", password: "1234"}); + window.x = await wh.get("iip://chat.go.iq:5001/sys/hd", {username: "admin", password: "delta"}); console.log(window.x); } diff --git a/tools/manager/index.html b/tools/manager/index.html index 9a1ea61..fd66eca 100644 --- a/tools/manager/index.html +++ b/tools/manager/index.html @@ -55,7 +55,7 @@ - + diff --git a/tools/manager/viewers/Esiur.Stores.MemoryStore.js b/tools/manager/viewers/Esiur.Stores.MemoryStore.js index a3f3e0b..5edc55d 100644 --- a/tools/manager/viewers/Esiur.Stores.MemoryStore.js +++ b/tools/manager/viewers/Esiur.Stores.MemoryStore.js @@ -1,4 +1,4 @@ -class Esiur_Stores_MemoryStore extends IUIWidget +class Esyur_Stores_MemoryStore extends IUIWidget { constuctor() { @@ -6,4 +6,4 @@ class Esiur_Stores_MemoryStore extends IUIWidget } } -IUI.module("Esiur.Stores.MemoryStore", Esiur_Stores_MemoryStore); \ No newline at end of file +IUI.module("Esyur.Stores.MemoryStore", Esyur_Stores_MemoryStore); \ No newline at end of file