2
0
mirror of https://github.com/esiur/esiur-js.git synced 2025-05-06 12:32:58 +00:00
This commit is contained in:
Ahmed Zamil 2019-08-07 05:00:25 +03:00
parent 399a096c58
commit 7d2bcc60f7
20 changed files with 126 additions and 106 deletions

View File

@ -26,8 +26,8 @@
"use strict"; "use strict";
import IEventHandler from '../Engine/IEventHandler.js'; import IEventHandler from '../Core/IEventHandler.js';
import IDestructible from '../Engine/IDestructible.js'; import IDestructible from '../Core/IDestructible.js';
export default class AutoList extends IEventHandler export default class AutoList extends IEventHandler
{ {

View File

@ -25,8 +25,8 @@
*/ */
"use strict"; "use strict";
import IEventHandler from '../Engine/IEventHandler.js'; import IEventHandler from '../Core/IEventHandler.js';
import IDestructible from '../Engine/IDestructible.js'; import IDestructible from '../Core/IDestructible.js';
export default class AutoMap extends IEventHandler export default class AutoMap extends IEventHandler
{ {

View File

@ -30,13 +30,15 @@ import DataType from './DataType.js';
import ResourceComparisonResult from './ResourceComparisionResult.js'; import ResourceComparisonResult from './ResourceComparisionResult.js';
import StructureComparisonResult from './StructureComparisonResult.js'; import StructureComparisonResult from './StructureComparisonResult.js';
import AsyncBag from '../Engine/AsyncBag.js'; import AsyncBag from '../Core/AsyncBag.js';
import AsyncReply from '../Engine/AsyncReply.js'; import AsyncReply from '../Core/AsyncReply.js';
import Structure from './Structure.js'; import Structure from './Structure.js';
import PropertyValue from './PropertyValue.js'; import PropertyValue from './PropertyValue.js';
import {DC, BL} from './DataConverter.js'; import {DC, BL} from './DataConverter.js';
import BinaryList from './BinaryList.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 { export default class Codec {

View File

@ -25,7 +25,7 @@
*/ */
"use strict"; "use strict";
import IDestructible from '../Engine/IDestructible.js'; import IDestructible from '../Core/IDestructible.js';
export default class KeyList export default class KeyList
{ {
@ -35,6 +35,14 @@ export default class KeyList
this.values = []; 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) at(index)
{ {
return this.values[index]; return this.values[index];

View File

@ -35,11 +35,11 @@ import SHA256 from '../../Security/Integrity/SHA256.js';
import {BL, DC} from '../../Data/DataConverter.js'; import {BL, DC} from '../../Data/DataConverter.js';
import SendList from '../SendList.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 Codec from '../../Data/Codec.js';
import NetworkBuffer from '../Sockets/NetworkBuffer.js'; import NetworkBuffer from '../Sockets/NetworkBuffer.js';
import KeyList from '../../Data/KeyList.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 Warehouse from '../../Resource/Warehouse.js';
import IIPAuthPacket from "../Packets/IIPAuthPacket.js"; import IIPAuthPacket from "../Packets/IIPAuthPacket.js";
@ -53,9 +53,9 @@ import IIPPacketCommand from "../Packets/IIPPacketCommand.js";
import IIPPacketEvent from "../Packets/IIPPacketEvent.js"; import IIPPacketEvent from "../Packets/IIPPacketEvent.js";
import IIPPacketReport from "../Packets//IIPPacketReport.js"; import IIPPacketReport from "../Packets//IIPPacketReport.js";
import ErrorType from "../../Engine/ErrorType.js"; import ErrorType from "../../Core/ErrorType.js";
import ProgressType from "../../Engine/ProgressType.js"; import ProgressType from "../../Core/ProgressType.js";
import ExceptionCode from "../../Engine/ExceptionCode.js"; import ExceptionCode from "../../Core/ExceptionCode.js";
import DistributedResource from './DistributedResource.js'; import DistributedResource from './DistributedResource.js';
import ResourceTemplate from '../../Resource/Template/ResourceTemplate.js'; import ResourceTemplate from '../../Resource/Template/ResourceTemplate.js';
@ -64,6 +64,7 @@ import DistributedResourceQueueItem from './DistributedResourceQueueItem.js';
import DistributedResourceQueueItemType from './DistributedResourceQueueItemType.js'; import DistributedResourceQueueItemType from './DistributedResourceQueueItemType.js';
import DistributedPropertyContext from './DistributedPropertyContext.js'; import DistributedPropertyContext from './DistributedPropertyContext.js';
import { ResourceTrigger } from '../../Resource/IResource.js';
export default class DistributedConnection extends IStore { 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) processPacket(msg, offset, ends, data)
{ {
@ -590,8 +516,85 @@ export default class DistributedConnection extends IStore {
} }
} }
trigger(trigger) { trigger(trigger)
return true; {
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) { put(resource) {
@ -1948,7 +1951,7 @@ export default class DistributedConnection extends IStore {
.then(function(ar) .then(function(ar)
{ {
rt.trigger(true); rt.trigger(true);
}).error(function(ex) { rt.triggerChunk(ex); }); }).error(function(ex) { rt.triggerError(ex); });
} }
return rt; return rt;

View File

@ -27,7 +27,7 @@
"use strict"; "use strict";
import IResource from '../../Resource/IResource.js'; 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 Codec from '../../Data/Codec.js';
import Structure from '../../Data/Structure.js'; import Structure from '../../Data/Structure.js';
import IIPPacketAction from '../Packets//IIPPacketAction.js'; import IIPPacketAction from '../Packets//IIPPacketAction.js';

View File

@ -26,11 +26,11 @@
"use strict"; "use strict";
import IDestructible from '../Engine/IDestructible.js'; import IDestructible from '../Core/IDestructible.js';
export const ResourceTrigger = export const ResourceTrigger =
{ {
Loaded : 0, Open : 0,
Initialize: 1, Initialize: 1,
Terminate: 2, Terminate: 2,
Configure: 3, Configure: 3,

View File

@ -26,7 +26,7 @@
"use strict"; "use strict";
import IEventHandler from '../Engine/IEventHandler.js'; import IEventHandler from '../Core/IEventHandler.js';
import IPermissionsManager from '../Security/Permissions/IPermissionsManager.js'; import IPermissionsManager from '../Security/Permissions/IPermissionsManager.js';
import StructureArray from '../Data/StructureArray.js'; import StructureArray from '../Data/StructureArray.js';
import AutoList from '../Data/AutoList.js'; import AutoList from '../Data/AutoList.js';

View File

@ -26,15 +26,16 @@
"use strict"; "use strict";
import AsyncReply from '../Engine/AsyncReply.js'; import AsyncReply from '../Core/AsyncReply.js';
import ResourceTemplate from '../Resource/Template/ResourceTemplate.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 AutoList from '../Data/AutoList.js';
import KeyList from '../Data/KeyList.js'; import KeyList from '../Data/KeyList.js';
import DistributedConnection from '../Net/IIP/DistributedConnection.js'; import DistributedConnection from '../Net/IIP/DistributedConnection.js';
import MemoryStore from '../Stores/MemoryStore.js'; import MemoryStore from '../Stores/MemoryStore.js';
import Instance from '../Resource/Instance.js'; import Instance from '../Resource/Instance.js';
import IStore from './IStore.js'; import IStore from './IStore.js';
import { ResourceTrigger } from './IResource.js';
export class WH extends IEventHandler export class WH extends IEventHandler
@ -52,14 +53,14 @@ export class WH extends IEventHandler
this._register("disconnected"); 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(); 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; return res;
} }
get(id, settings) get(id, attributes = null, parent = null, manager = null)
{ {
if (Number.isInteger(id)) if (Number.isInteger(id))
{ {
@ -114,8 +115,9 @@ export class WH extends IEventHandler
if (handler = this.protocols.item(url[0])) if (handler = this.protocols.item(url[0]))
{ {
var store = this.new(handler, url[0] + "://" + hostname); var store = handler();
store.open(settings).then(x=>{ this.put(store, url[0] + "://" + hostname, null, parent, null, 0, manager, attributes);
store.trigger(ResourceTrigger.Open).then(x=>{
if (pathname.length > 0 && pathname != "") if (pathname.length > 0 && pathname != "")
store.get(pathname).then(r=>{ store.get(pathname).then(r=>{
rt.trigger(r); rt.trigger(r);
@ -177,11 +179,15 @@ export class WH extends IEventHandler
return true; 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 = new Instance(this.resourceCounter++, name, resource, store, customTemplate, age);
//resource.instance.children.on("add", Warehouse._onChildrenAdd).on("remove", Warehouse._onChildrenRemove); //resource.instance.children.on("add", Warehouse._onChildrenAdd).on("remove", Warehouse._onChildrenRemove);
//resource.instance.parents.on("add", Warehouse._onParentsAdd).on("remove", Warehouse._onParentsRemove); //resource.instance.parents.on("add", Warehouse._onParentsAdd).on("remove", Warehouse._onParentsRemove);
if (attributes != null)
resource.instance.setAttributes(attributes);
if (manager != null) if (manager != null)
resource.instance.managers.add(manager); resource.instance.managers.add(manager);
@ -294,8 +300,8 @@ export class WH extends IEventHandler
let Warehouse = new WH(); let Warehouse = new WH();
Warehouse.protocols.add("iip", DistributedConnection); Warehouse.protocols.add("iip", () => new DistributedConnection());
Warehouse.protocols.add("mem", MemoryStore); Warehouse.protocols.add("mem", () => new MemoryStore());
export default Warehouse; export default Warehouse;

View File

@ -1,6 +1,6 @@
{ {
"name": "esiur", "name": "esiur",
"version": "1.2.7", "version": "1.3.0",
"description": "Distributed Object Framework", "description": "Distributed Object Framework",
"main": "esiur.js", "main": "esiur.js",
"scripts": { "scripts": {

View File

@ -11,6 +11,7 @@ class MyStore extends IStore
async function load() 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"});
console.log(window.x); console.log(window.x);
} }