diff --git a/src/Engine/AsyncBag.js b/src/Core/AsyncBag.js similarity index 100% rename from src/Engine/AsyncBag.js rename to src/Core/AsyncBag.js diff --git a/src/Engine/AsyncException.js b/src/Core/AsyncException.js similarity index 100% rename from src/Engine/AsyncException.js rename to src/Core/AsyncException.js diff --git a/src/Engine/AsyncQueue.js b/src/Core/AsyncQueue.js similarity index 100% rename from src/Engine/AsyncQueue.js rename to src/Core/AsyncQueue.js diff --git a/src/Engine/AsyncReply.js b/src/Core/AsyncReply.js similarity index 100% rename from src/Engine/AsyncReply.js rename to src/Core/AsyncReply.js diff --git a/src/Engine/ErrorType.js b/src/Core/ErrorType.js similarity index 100% rename from src/Engine/ErrorType.js rename to src/Core/ErrorType.js diff --git a/src/Engine/ExceptionCode.js b/src/Core/ExceptionCode.js similarity index 100% rename from src/Engine/ExceptionCode.js rename to src/Core/ExceptionCode.js diff --git a/src/Engine/IDestructible.js b/src/Core/IDestructible.js similarity index 100% rename from src/Engine/IDestructible.js rename to src/Core/IDestructible.js diff --git a/src/Engine/IEventHandler.js b/src/Core/IEventHandler.js similarity index 100% rename from src/Engine/IEventHandler.js rename to src/Core/IEventHandler.js diff --git a/src/Engine/ProgressType.js b/src/Core/ProgressType.js similarity index 100% rename from src/Engine/ProgressType.js rename to src/Core/ProgressType.js diff --git a/src/Data/AutoList.js b/src/Data/AutoList.js index 49b2b36..11dc275 100644 --- a/src/Data/AutoList.js +++ b/src/Data/AutoList.js @@ -26,8 +26,8 @@ "use strict"; -import IEventHandler from '../Engine/IEventHandler.js'; -import IDestructible from '../Engine/IDestructible.js'; +import IEventHandler from '../Core/IEventHandler.js'; +import IDestructible from '../Core/IDestructible.js'; export default class AutoList extends IEventHandler { diff --git a/src/Data/AutoMap.js b/src/Data/AutoMap.js index cb3a586..6d62cb2 100644 --- a/src/Data/AutoMap.js +++ b/src/Data/AutoMap.js @@ -25,8 +25,8 @@ */ "use strict"; -import IEventHandler from '../Engine/IEventHandler.js'; -import IDestructible from '../Engine/IDestructible.js'; +import IEventHandler from '../Core/IEventHandler.js'; +import IDestructible from '../Core/IDestructible.js'; export default class AutoMap extends IEventHandler { diff --git a/src/Data/Codec.js b/src/Data/Codec.js index 802668d..a90373e 100644 --- a/src/Data/Codec.js +++ b/src/Data/Codec.js @@ -30,13 +30,15 @@ import DataType from './DataType.js'; import ResourceComparisonResult from './ResourceComparisionResult.js'; import StructureComparisonResult from './StructureComparisonResult.js'; -import AsyncBag from '../Engine/AsyncBag.js'; -import AsyncReply from '../Engine/AsyncReply.js'; +import AsyncBag from '../Core/AsyncBag.js'; +import AsyncReply from '../Core/AsyncReply.js'; import Structure from './Structure.js'; import PropertyValue from './PropertyValue.js'; import {DC, BL} from './DataConverter.js'; import BinaryList from './BinaryList.js'; -import DistributedPropertyContext from '../Net/IIP/DistributedPropertyContext.JS'; +import DistributedPropertyContext from '../Net/IIP/DistributedPropertyContext.js'; +import DistributedResource from '../Net/IIP/DistributedResource.js' +import IResource from '../Resource/IResource.js'; export default class Codec { diff --git a/src/Data/KeyList.js b/src/Data/KeyList.js index 16448eb..2ffe012 100644 --- a/src/Data/KeyList.js +++ b/src/Data/KeyList.js @@ -25,7 +25,7 @@ */ "use strict"; -import IDestructible from '../Engine/IDestructible.js'; +import IDestructible from '../Core/IDestructible.js'; export default class KeyList { @@ -35,6 +35,14 @@ export default class KeyList this.values = []; } + toObject() + { + var rt = {}; + for(var i = 0; i < this.keys.length; i++) + rt[this.keys[i]]=this.values[i]; + return rt; + } + at(index) { return this.values[index]; diff --git a/src/Net/IIP/DistributedConnection.js b/src/Net/IIP/DistributedConnection.js index d05afe4..b65a821 100644 --- a/src/Net/IIP/DistributedConnection.js +++ b/src/Net/IIP/DistributedConnection.js @@ -35,11 +35,11 @@ import SHA256 from '../../Security/Integrity/SHA256.js'; import {BL, DC} from '../../Data/DataConverter.js'; import SendList from '../SendList.js'; -import AsyncReply from '../../Engine/AsyncReply.js'; +import AsyncReply from '../../Core/AsyncReply.js'; import Codec from '../../Data/Codec.js'; import NetworkBuffer from '../Sockets/NetworkBuffer.js'; import KeyList from '../../Data/KeyList.js'; -import AsyncQueue from '../../Engine/AsyncQueue.js'; +import AsyncQueue from '../../Core/AsyncQueue.js'; import Warehouse from '../../Resource/Warehouse.js'; import IIPAuthPacket from "../Packets/IIPAuthPacket.js"; @@ -53,9 +53,9 @@ import IIPPacketCommand from "../Packets/IIPPacketCommand.js"; import IIPPacketEvent from "../Packets/IIPPacketEvent.js"; import IIPPacketReport from "../Packets//IIPPacketReport.js"; -import ErrorType from "../../Engine/ErrorType.js"; -import ProgressType from "../../Engine/ProgressType.js"; -import ExceptionCode from "../../Engine/ExceptionCode.js"; +import ErrorType from "../../Core/ErrorType.js"; +import ProgressType from "../../Core/ProgressType.js"; +import ExceptionCode from "../../Core/ExceptionCode.js"; import DistributedResource from './DistributedResource.js'; import ResourceTemplate from '../../Resource/Template/ResourceTemplate.js'; @@ -64,6 +64,7 @@ import DistributedResourceQueueItem from './DistributedResourceQueueItem.js'; import DistributedResourceQueueItemType from './DistributedResourceQueueItemType.js'; import DistributedPropertyContext from './DistributedPropertyContext.js'; +import { ResourceTrigger } from '../../Resource/IResource.js'; export default class DistributedConnection extends IStore { @@ -134,81 +135,6 @@ export default class DistributedConnection extends IStore { } - open(settings = {}) - { - - var { domain = null, - secure = false, - username = "guest", - password = "", - checkInterval = 30, - connectionTimeout = 600, - revivingTime = 120, - debug = false} = settings; - - this.openReply = new AsyncReply(); - - var hostname = this.instance.name.split("://", 2)[1].split("/", 2)[0]; - - // assign domain from hostname if not provided - domain = domain ? domain : hostname.split(":")[0]; - - this.session.localAuthentication.domain = domain; - this.session.localAuthentication.username = username; - - this.localPassword = DC.stringToBytes(password); - - var url = `ws${secure ? 's' : ''}://${hostname}`; - - - this.debug = debug; - this.totalReceived = 0; - this.totalSent = 0; - - this.checkInterval = checkInterval * 1000; // check every 30 seconds - this.connectionTimeout = connectionTimeout * 1000; // 10 minutes (4 pings failed) - this.revivingTime = revivingTime * 1000; // 2 minutes - this.lastAction = Date.now(); - - this.socket = new WebSocket(url, "iip"); - this.socket.binaryType = "arraybuffer"; - this.socket.connection = this; - this.socket.networkBuffer = new NetworkBuffer(); - - var un = DC.stringToBytes(username); - var dmn = DC.stringToBytes(domain); - var self = this; - - this.socket.onopen = function () { - var bl = BL(); - bl.addUint8(0x60).addUint8(dmn.length).addUint8Array(dmn).addUint8Array(self.localNonce).addUint8(un.length).addUint8Array(un); - self.send(bl.toArray()); - }; - - this.socket.onmessage = function (msg) { - - //console.log("Rec", msg.data.byteLength); - - this.networkBuffer.writeAll(msg.data); - - self.lastAction = new Date(); - - while (this.networkBuffer.available > 0 && !this.networkBuffer.protected) - self.receive(this.networkBuffer); - - - }; - - this.socket.onclose = function(event) - { - if (this.connection.openReply) - this.connection.openReply.triggerError(0, 0, "Host not reachable"); - - self.close(event); - }; - - return this.openReply; - } processPacket(msg, offset, ends, data) { @@ -590,8 +516,85 @@ export default class DistributedConnection extends IStore { } } - trigger(trigger) { - return true; + trigger(trigger) + { + if (trigger == ResourceTrigger.Open) + { + var { domain = null, + secure = false, + username = "guest", + password = "", + checkInterval = 30, + connectionTimeout = 600, + revivingTime = 120, + debug = false} = this.instance.attributes.toObject(); + + this.openReply = new AsyncReply(); + + var hostname = this.instance.name.split("://", 2)[1].split("/", 2)[0]; + + // assign domain from hostname if not provided + domain = domain ? domain : hostname.split(":")[0]; + + this.session.localAuthentication.domain = domain; + this.session.localAuthentication.username = username; + + this.localPassword = DC.stringToBytes(password); + + var url = `ws${secure ? 's' : ''}://${hostname}`; + + + this.debug = debug; + this.totalReceived = 0; + this.totalSent = 0; + + this.checkInterval = checkInterval * 1000; // check every 30 seconds + this.connectionTimeout = connectionTimeout * 1000; // 10 minutes (4 pings failed) + this.revivingTime = revivingTime * 1000; // 2 minutes + this.lastAction = Date.now(); + + this.socket = new WebSocket(url, "iip"); + this.socket.binaryType = "arraybuffer"; + this.socket.connection = this; + this.socket.networkBuffer = new NetworkBuffer(); + + var un = DC.stringToBytes(username); + var dmn = DC.stringToBytes(domain); + var self = this; + + this.socket.onopen = function () { + var bl = BL(); + bl.addUint8(0x60).addUint8(dmn.length).addUint8Array(dmn).addUint8Array(self.localNonce).addUint8(un.length).addUint8Array(un); + self.send(bl.toArray()); + }; + + this.socket.onmessage = function (msg) { + + //console.log("Rec", msg.data.byteLength); + + this.networkBuffer.writeAll(msg.data); + + self.lastAction = new Date(); + + while (this.networkBuffer.available > 0 && !this.networkBuffer.protected) + self.receive(this.networkBuffer); + + + }; + + this.socket.onclose = function(event) + { + if (this.connection.openReply) + this.connection.openReply.triggerError(0, 0, "Host not reachable"); + + self.close(event); + }; + + return this.openReply; + + } + + return new AsyncReply(true); } put(resource) { @@ -1948,7 +1951,7 @@ export default class DistributedConnection extends IStore { .then(function(ar) { rt.trigger(true); - }).error(function(ex) { rt.triggerChunk(ex); }); + }).error(function(ex) { rt.triggerError(ex); }); } return rt; diff --git a/src/Net/IIP/DistributedResource.js b/src/Net/IIP/DistributedResource.js index 074682d..cebbc6b 100644 --- a/src/Net/IIP/DistributedResource.js +++ b/src/Net/IIP/DistributedResource.js @@ -27,7 +27,7 @@ "use strict"; import IResource from '../../Resource/IResource.js'; -import AsyncReply from '../../Engine/AsyncReply.js'; +import AsyncReply from '../../Core/AsyncReply.js'; import Codec from '../../Data/Codec.js'; import Structure from '../../Data/Structure.js'; import IIPPacketAction from '../Packets//IIPPacketAction.js'; diff --git a/src/Resource/IResource.js b/src/Resource/IResource.js index 3f065f1..0bafc0a 100644 --- a/src/Resource/IResource.js +++ b/src/Resource/IResource.js @@ -26,11 +26,11 @@ "use strict"; -import IDestructible from '../Engine/IDestructible.js'; +import IDestructible from '../Core/IDestructible.js'; export const ResourceTrigger = { - Loaded : 0, + Open : 0, Initialize: 1, Terminate: 2, Configure: 3, diff --git a/src/Resource/Instance.js b/src/Resource/Instance.js index 1b616b8..97c7382 100644 --- a/src/Resource/Instance.js +++ b/src/Resource/Instance.js @@ -26,7 +26,7 @@ "use strict"; -import IEventHandler from '../Engine/IEventHandler.js'; +import IEventHandler from '../Core/IEventHandler.js'; import IPermissionsManager from '../Security/Permissions/IPermissionsManager.js'; import StructureArray from '../Data/StructureArray.js'; import AutoList from '../Data/AutoList.js'; diff --git a/src/Resource/Warehouse.js b/src/Resource/Warehouse.js index 10544a9..b1dd2df 100644 --- a/src/Resource/Warehouse.js +++ b/src/Resource/Warehouse.js @@ -26,15 +26,16 @@ "use strict"; -import AsyncReply from '../Engine/AsyncReply.js'; +import AsyncReply from '../Core/AsyncReply.js'; import ResourceTemplate from '../Resource/Template/ResourceTemplate.js'; -import IEventHandler from '../Engine/IEventHandler.js'; +import IEventHandler from '../Core/IEventHandler.js'; import AutoList from '../Data/AutoList.js'; import KeyList from '../Data/KeyList.js'; import DistributedConnection from '../Net/IIP/DistributedConnection.js'; import MemoryStore from '../Stores/MemoryStore.js'; import Instance from '../Resource/Instance.js'; import IStore from './IStore.js'; +import { ResourceTrigger } from './IResource.js'; export class WH extends IEventHandler @@ -52,14 +53,14 @@ export class WH extends IEventHandler this._register("disconnected"); } - new(type, name, store = null, parent = null, manager = null) + new(type, name, store = null, parent = null, manager = null, attributes = null) { var res = new type(); - this.put(res, name, store, parent, null, 0, manager); + this.put(res, name, store, parent, null, 0, manager, attributes); return res; } - get(id, settings) + get(id, attributes = null, parent = null, manager = null) { if (Number.isInteger(id)) { @@ -114,8 +115,9 @@ export class WH extends IEventHandler if (handler = this.protocols.item(url[0])) { - var store = this.new(handler, url[0] + "://" + hostname); - store.open(settings).then(x=>{ + 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); @@ -177,10 +179,14 @@ export class WH extends IEventHandler return true; } - put(resource, name, store, parent, customTemplate = null, age = 0, manager = null){ + put(resource, name, store, parent, customTemplate = null, age = 0, manager = null, attributes = null){ + resource.instance = new Instance(this.resourceCounter++, name, resource, store, customTemplate, age); //resource.instance.children.on("add", Warehouse._onChildrenAdd).on("remove", Warehouse._onChildrenRemove); //resource.instance.parents.on("add", Warehouse._onParentsAdd).on("remove", Warehouse._onParentsRemove); + + if (attributes != null) + resource.instance.setAttributes(attributes); if (manager != null) resource.instance.managers.add(manager); @@ -294,8 +300,8 @@ export class WH extends IEventHandler let Warehouse = new WH(); -Warehouse.protocols.add("iip", DistributedConnection); -Warehouse.protocols.add("mem", MemoryStore); +Warehouse.protocols.add("iip", () => new DistributedConnection()); +Warehouse.protocols.add("mem", () => new MemoryStore()); export default Warehouse; diff --git a/src/package.json b/src/package.json index 98abefe..02716cb 100644 --- a/src/package.json +++ b/src/package.json @@ -1,6 +1,6 @@ { "name": "esiur", - "version": "1.2.7", + "version": "1.3.0", "description": "Distributed Object Framework", "main": "esiur.js", "scripts": { diff --git a/test/test.js b/test/test.js index 978ced4..2229388 100644 --- a/test/test.js +++ b/test/test.js @@ -11,6 +11,7 @@ class MyStore extends IStore async function load() { window.x = await wh.get("iip://localhost:5001/db/my", {username: "demo", password: "1234"}); + console.log(window.x); }