2
0
mirror of https://github.com/esiur/esiur-js.git synced 2026-04-03 21:48:21 +00:00

ES6 modules

This commit is contained in:
2019-07-15 14:22:40 +03:00
parent 1e522af044
commit fdfa690b23
50 changed files with 474 additions and 244 deletions

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) 2017-2018 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 10/11/2018.
*/
"use strict";
export default class CustomResourceEvent
{
constructor(issuer, receivers, params)
{
this.issuer = issuer;
this.receivers = receivers;
this.params = params;
}
}

63
src/Resource/IResource.js Normal file
View File

@@ -0,0 +1,63 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 25/07/2017.
*/
"use strict";
import IDestructible from '../Engine/IDestructible.js';
export const ResourceTrigger =
{
Loaded : 0,
Initialize: 1,
Terminate: 2,
Configure: 3,
SystemInitialized: 4,
SystemTerminated: 5,
SystemReload: 6
};
export default class IResource extends IDestructible
{
trigger(trigger)
{
}
constructor()
{
super();
}
static getTemplate()
{
return {
namespace: "Esiur",
properties: [],
functions: [],
events: []
}
}
}

63
src/Resource/IStore.js Normal file
View File

@@ -0,0 +1,63 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 25/07/2017.
*/
"use strict";
import IResource from './IResource.js';
export default class IStore extends IResource {
get(path) {
}
retrieve(iid) {
}
put(resource) {
}
record(resource, propertyName, value, age, dateTime)
{
}
getRecord(resource, fromDate, toDate)
{
}
remove(resource)
{
}
constructor()
{
super();
}
}

361
src/Resource/Instance.js Normal file
View File

@@ -0,0 +1,361 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 29/08/2017.
*/
"use strict";
import IEventHandler from '../Engine/IEventHandler.js';
import IPermissionsManager from '../Security/Permissions/IPermissionsManager.js';
import StructureArray from '../Data/StructureArray.js';
import AutoList from '../Data/AutoList.js';
import KeyList from '../Data/KeyList.js';
import Structure from '../Data/Structure.js';
import PropertyValue from '../Data/PropertyValue.js';
import CustomResourceEvent from './CustomResourceEvent.js';
import Warehouse from './Warehouse.js';
export default class Instance extends IEventHandler
{
getAge(index)
{
if (index < this.ages.length)
return this.ages[index];
else
return 0;
}
setAge(index, value)
{
if (index < this.ages.length)
{
this.ages[index] = value;
if (value > this.instanceAge)
this.instanceAge = value;
}
}
getModificationDate(index)
{
if (index < this.modificationDates.length)
return this.modificationDates[index];
else
return new Date(0);
}
setModificationDate(index, value)
{
if (index < this.modificationDates.length)
{
this.modificationDates[index] = value;
if (value > this.instanceModificationDate)
this.instanceModificationDate = value;
}
}
loadProperty(name, age, modificationDate, value)
{
var pt = this.template.getPropertyTemplateByName(name);
if (pt == null)
return false;
this.resource[name] = value;
this.setAge(pt.index, age);
this.setModificationDate(pt.index, modificationDate);
return true;
}
deserialize(properties)
{
for (var i = 0; i < properties.length; i++)
{
var pt = this.template.GetPropertyTemplateByIndex(i);
if (pt != null)
{
var pv = properties[i];
this.loadProperty(pt.name, pv.age, pv.date, pv.value);
}
}
return true;
}
serialize()
{
var props = [];
for (var i = 0; i < this.template.properties.length; i++)
props.push(new PropertyValue(this.resource[this.template.properties[i].name],
this.ages[this.template.properties[i].index],
this.modificationDates[this.template.properties[i].index]));
return props;
}
isStorable()
{
return resource instanceof Storable;
}
emitModification(pt, value)
{
this.instanceAge++;
var now = new Date();
this.ages[pt.index] = this.instanceAge;
this.modificationDates[pt.index] = now;
if (pt.recordable)
this.store.record(this.resource, pt.name, value, this.ages[pt.index], now);
super._emit("ResourceModified", this.resource, pt.name, value);
this.resource._emit("modified", pt.name, value);
}
modified(propertyName = null)
{
if (propertyName == null)
propertyName = modified.caller.name;
var val = {};
if (this.getPropertyValue(propertyName, val))
{
var pt = this.template.getPropertyTemplateByName(propertyName);
this.emitModification(pt, val.value)
}
}
_emitResourceEvent(issuer, receivers, name, args)
{
super._emit("ResourceEventOccurred", this.resource, issuer, receivers, name, args);
}
getPropertyValue(name, resultObject)
{
for (var i = 0; i < this.template.properties.length; i++)
if (this.template.properties[i].name == name)
{
resultObject.value = this.resource[name];
return true;
}
return false;
}
constructor(id, name, resource, store, customTemplate = null, age = 0)
{
super();
this.store = store;
this.resource = resource;
this.id = id;
this.name = name;
this.instanceAge = age;
this.instanceModificationDate = new Date(0);
this.children = new AutoList();
this.parents = new AutoList();
this.managers = new AutoList();
this.attributes = new KeyList();
var self = this;
this.children.on("add", function(value){
value.instance.parents.add(self.resource);
});
this.children.on("remove", function(value){
value.instance.parents.remove(self.resource);
});
this.resource.on("Destroy", function(sender){
self._emit("ResourceDestroyed", sender);
});
if (customTemplate != null)
this.template = customTemplate;
else
this.template = Warehouse.getTemplateByType(this.resource.constructor);
// set ages
this.ages = [];
this.modificationDates = [];
for(var i = 0; i < this.template.properties.length; i++)
{
this.ages.push(0);
this.modificationDates.push(new Date(0));
}
// connect events
for (var i = 0; i < this.template.events.length; i++)
this.resource.on(this.template.events[i].name, this._makeHandler(this.template.events[i].name));
}
_makeHandler(name)
{
var self = this;
return function(args)
{
if (args instanceof CustomResourceEvent)
self._emitResourceEvent(args.issuer, args.receivers, name, args.params);
else
self._emitResourceEvent(null, null, name, args);
};
}
/// <summary>
/// Check for permission.
/// </summary>
/// <param name="session">Caller sessions.</param>
/// <param name="action">Action type</param>
/// <param name="member">Function or property to check for permission.</param>
/// <returns>Ruling.</returns>
applicable(session, action, member, inquirer)
{
for (var i = 0; i < this.managers.length; i++)
{
var r = this.managers.item(i).applicable(this.resource, session, action, member, inquirer);
if (r != Ruling.DontCare)
return r;
}
return Ruling.DontCare;
}
removeAttributes(attributes = null)
{
if (attributes == null)
this.attributes.clear();
else
{
for(var i = 0; i < attributes.length; i++)
this.attributes.remove(attributes[i]);
}
return true;
}
getAttributes(attributes = null)
{
var st = new Structure();
if (attributes == null)
{
attributes = this.attributes.keys.slice(0);
attributes.push("managers");
}
for(var i = 0; i < attributes.length; i++)
{
var attr = attributes[i];
if (attr == "name")
st["name"] = this.name;
else if (attr == "managers")
{
var mngrs = new StructureArray();
for(var j = 0; j < this.managers.length; j++)
{
var manager = this.managers.item(j);
var sm = new Structure();
sm["type"] = manager.constructor.name;
sm["settings"] = manager.settings;
mngrs.push(sm);
}
st["managers"] = mngrs;
}
else
st[attr] = this.attributes.item(attr);
}
return st;
}
setAttributes(attributes, clearAttributes = false)
{
if (clearAttributes)
this.attributes.clear();
for (var attr in attributes)
if (attr == "name")
this.name = attributes[attr];
else if (attr == "managers")
{
this.managers.clear();
var mngrs = attributes[attr];
for (var i = 0; i < mngrs.length; i++)
{
var mngr = mngrs[i];
var type = window[mngr];
var settings = mngr["settings"];
var manager = new (Function.prototype.bind.apply(type));
if (manager instanceof IPermissionsManager)
{
manager.initialize(settings, this.resource);
this.managers.add(manager);
}
else
return false;
}
}
else
{
this.attributes.set(attr, attributes[attr]);
}
return true;
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 24/08/2017.
*/
"use strict";
import {DC, BL} from '../../Data/DataConverter.js';
import MemberTemplate from './MemberTemplate.js';
export default class EventTemplate extends MemberTemplate
{
constructor()
{
super();
this.type = MemberType.Event;
}
compose()
{
var rt = BL();
var name = super.compose();
if (this.expansion != null) {
var exp = DC.stringToBytes(this.expansion);
return rt.addUint8(0x50).addUint8(name.length).addUint8Array(name).addUint32(exp.length).addUint8Array(exp).toArray();
}
else
return rt.addUint8(0x40).addUint32(name.length).addUint8Array(name).toArray();
}
}

View File

@@ -0,0 +1,52 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 27/08/2017.
*/
"use strict";
import {DC, BL} from '../../Data/DataConverter.js';
import MemberTemplate from './MemberTemplate.js';
export default class FunctionTemplate extends MemberTemplate {
compose() {
var name = super.compose();
var rt = BL();
if (this.expansion != null) {
var exp = DC.stringToBytes(this.expansion);
return rt.addUint8(0x10 | (this.isVoid ? 0x8 : 0x0))
.addUint8(name.length).addUint8Array(name)
.addUint32(exp.length).addUint8Array(exp).toArray();
}
else
return rt.addUint8(this.isVoid ? 0x8 : 0x0).addUint8(name.length).addUint8Array(name).toArray();
}
constructor() {
super();
this.type = MemberType.Function;
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 24/08/2017.
*/
"use strict";
import DC from '../../Data/DataConverter.js';
export const MemberType = {
Function: 0,
Property: 1,
Event: 2
};
export default class MemberTemplate {
compose() {
return DC.stringToBytes(this.name);
}
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 27/08/2017.
*/
"use strict";
import {DC, BL} from '../../Data/DataConverter.js';
import MemberTemplate from './MemberTemplate.js';
export const PropertyPermission = {
Read: 1,
Write: 2,
ReadWrite: 3
};
export default class PropertyTemplate extends MemberTemplate
{
constructor()
{
super();
this.type = MemberType.Property;
}
compose()
{
var name = super.compose();
var rt = BL();
var pv = (this.permission >> 1) | (this.recordable ? 1 : 0);
if (this.writeExpansion != null && this.readExpansion != null)
{
var rexp = DC.stringToBytes(this.readExpansion);
var wexp = DC.stringToBytes(this.writeExpansion);
return rt.addUint8(0x38 | pv)
.addUint8(name.length)
.addUint8Array(name)
.addUint32(wexp.length)
.addUint8Array(wexp)
.addUint32(rexp.length)
.addUint8Array(rexp)
.toArray();
}
else if (this.writeExpansion != null)
{
var wexp = DC.stringToBytes(this.writeExpansion);
return rt.addUint8(0x30 | pv)
.addUint8(name.length)
.addUint8Array(name)
.addUint32(wexp.length)
.addUint8Array(wexp)
.toArray();
}
else if (this.readExpansion != null)
{
var rexp = DC.stringToBytes(this.readExpansion);
return rt.addUint8(0x28 | pv)
.addUint8(name.length)
.addUint8Array(name)
.addUint32(rexp.length)
.addUint8Array(rexp)
.toArray();
}
else
return rt.addUint8(0x20 | pv)
.addUint32(name.length)
.addUint8Array(name)
.toArray();
}
}

View File

@@ -0,0 +1,297 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
"use strict";
import FunctionTemplate from './FunctionTemplate.js';
import PropertyTemplate from './PropertyTemplate.js';
import EventTemplate from './EventTemplate.js';
import SHA256 from '../../Security/Integrity/SHA256.js';
import {DC, BL} from '../../Data/DataConverter.js';
export default class ResourceTemplate {
getEventTemplateByName(eventName) {
for (var i = 0; i < this.events.length; i++)
if (this.events[i].name == eventName)
return this.events[i];
return null;
}
getEventTemplateByIndex(index) {
for (var i = 0; i < this.events.length; i++)
if (this.events[i].index == index)
return this.events[i];
return null;
}
getFunctionTemplateByName(functionName) {
for (var i = 0; i < this.functions.length; i++)
if (this.functions[i].name == functionName)
return this.functions[i];
return null;
}
getFunctionTemplateByIndex(index) {
for (var i = 0; i < this.functions.length; i++)
if (this.functions[i].index == index)
return this.functions[i];
return null;
}
getPropertyTemplateByName(propertyName) {
for (var i = 0; i < this.properties.length; i++)
if (this.properties[i].name == propertyName)
return this.properties[i];
return null;
}
getPropertyTemplateByIndex(index) {
for (var i = 0; i < this.properties.length; i++)
if (this.properties[i].index == index)
return this.properties[i];
return null;
}
/*
template: {
properties: [
{name: 'size', read: null, write: null}
],
functions: [
],
events: [
]
}
*/
constructor(type) {
this.properties = [];
this.events = [];
this.functions = [];
this.members = [];
if (type === undefined)
return;
var template = type.getTemplate();
// set guid
this.className = template.namespace + "." + type.prototype.constructor.name;
this.classId = SHA256.compute(DC.stringToBytes(this.className)).getGuid(0);
//byte currentIndex = 0;
for (var i = 0; i < template.properties.length; i++) {
var pt = new PropertyTemplate();
pt.name = template.properties[i].name;
pt.index = i;
pt.readExpansion = template.properties[i].read;
pt.writeExpansion = template.properties[i].write;
pt.recordable = template.properties[i].recordable;
this.properties.push(pt);
}
for (var i = 0; i < template.events.length; i++) {
var et = new EventTemplate();
et.name = template.events[i].name;
et.index = i;
et.expansion = template.events[i].expansion;
this.events.push(et);
}
for (var i = 0; i < template.functions.length; i++) {
var ft = new FunctionTemplate();
ft.name = template.functions[i].name;
ft.index = i;
ft.isVoid = template.functions[i].void;
ft.expansion = template.functions[i].expansion;
this.functions.push(ft);
}
// append signals
for (var i = 0; i < this.events.length; i++)
this.members.push(this.events[i]);
// append slots
for (var i = 0; i < this.functions.length; i++)
this.members.push(this.functions[i]);
// append properties
for (var i = 0; i < this.properties.length; i++)
this.members.push(this.properties[i]);
// bake it binarily
var b = BL();
var cls = DC.stringToBytes(this.className);
b.addUint8Array(this.classId.value)
.addUint8(cls.length).addUint8Array(cls).addUint32(template.version).addUint16(this.members.length);
for (var i = 0; i < this.functions.length; i++)
b.addUint8Array(this.functions[i].compose());
for (var i = 0; i < this.properties.length; i++)
b.addUint8Array(this.properties[i].compose());
for (var i = 0; i < this.events.length; i++)
b.addUint8Array(this.events[i].compose());
this.content = b.toArray();
}
static getFunctionParameters(func)
{
var STRIP_COMMENTS = /(\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s*=[^,\)]*(('(?:\\'|[^'\r\n])*')|("(?:\\"|[^"\r\n])*"))|(\s*=[^,\)]*))/mg;
var ARGUMENT_NAMES = /([^\s,]+)/g;
var fnStr = func.toString().replace(STRIP_COMMENTS, '');
var result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
if(result === null)
result = [];
return result;
}
static parse(data, offset = 0, contentLength = -1) {
if (contentLength == -1)
contentLength = data.length;
var ends = offset + contentLength;
var oOffset = offset;
// start parsing...
var od = new ResourceTemplate();
od.content = data.clip(offset, contentLength);
od.classId = data.getGuid(offset);
offset += 16;
od.className = data.getString(offset + 1, data.getUint8(offset));
offset += data.getUint8(offset) + 1;
od.version = data.getInt32(offset);
offset += 4;
var methodsCount = data.getUint16(offset);
offset += 2;
var functionIndex = 0;
var propertyIndex = 0;
var eventIndex = 0;
for (var i = 0; i < methodsCount; i++) {
var type = data.getUint8(offset) >> 5;
if (type == 0) // function
{
var ft = new FunctionTemplate();
ft.index = functionIndex++;
var expansion = ((data.getUint8(offset) & 0x10) == 0x10);
ft.isVoid = ((data.getUint8(offset++) & 0x08) == 0x08);
var len = data.getUint8(offset++);
ft.name = data.getString(offset, len);
offset += len;
if (expansion) // expansion ?
{
var cs = data.getUint32(offset);
offset += 4;
ft.expansion = data.getString(offset, cs);
offset += cs;
}
od.functions.push(ft);
}
else if (type == 1) // property
{
var pt = new PropertyTemplate();
pt.index = propertyIndex++;
var readExpansion = ((data.getUint8(offset) & 0x8) == 0x8);
var writeExpansion = ((data.getUint8(offset) & 0x10) == 0x10);
pt.recordable = ((data.getUint8(offset) & 1) == 1);
pt.permission = ((data.getUint8(offset++) >> 1) & 0x3);
var len = data.getUint8(offset++);
pt.name = data.getString(offset, len);
offset += len;
if (readExpansion) // expansion ?
{
var cs = data.getUint32(offset);
offset += 4;
pt.readExpansion = data.getString(offset, cs);
offset += cs;
}
if (writeExpansion) // expansion ?
{
var cs = data.getUint32(offset);
offset += 4;
pt.writeExpansion = data.getString(offset, cs);
offset += cs;
}
od.properties.push(pt);
}
else if (type == 2) // Event
{
var et = new EventTemplate();
et.index = eventIndex++;
var expansion = ((data.getUint8(offset++) & 0x10) == 0x10);
var len = data.getUint8(offset++);
et.name = data.getString(offset, len);
offset += len;
if (expansion) // expansion ?
{
var cs = data.getUint32(offset);
offset += 4;
et.expansion = data.getString(offset, cs);
offset += cs;
}
od.events.push(et);
}
}
// append signals
for (var i = 0; i < od.events.length; i++)
od.members.push(od.events[i]);
// append slots
for (var i = 0; i < od.functions.length; i++)
od.members.push(od.functions[i]);
// append properties
for (var i = 0; i < od.properties.length; i++)
od.members.push(od.properties[i]);
return od;
}
}

307
src/Resource/Warehouse.js Normal file
View File

@@ -0,0 +1,307 @@
/*
* Copyright (c) 2017 Ahmed Kh. Zamil
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Created by Ahmed Zamil on 25/07/2017.
*/
"use strict";
import AsyncReply from '../Engine/AsyncReply.js';
import ResourceTemplate from '../Resource/Template/ResourceTemplate.js';
import IEventHandler from '../Engine/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';
export class WH extends IEventHandler
{
constructor()
{
super();
this.stores = new AutoList();
this.resources = new KeyList();
this.resourceCounter = 0;
this.templates = new KeyList();
this.protocols = new KeyList();
this._register("connected");
this._register("disconnected");
}
new(type, name, store = null, parent = null, manager = null)
{
var res = new type();
this.put(res, name, store, parent, null, 0, manager);
return res;
}
get(id, settings)
{
if (Number.isInteger(id))
{
//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 i = 1;
res = d;
while(p.length > 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;
}
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 = this.new(handler, url[0] + "://" + hostname);
store.open(settings).then(x=>{
if (success)
{
if (pathname.length > 0 && pathname[1] != "")
store.get(pathname[1]).then(r=>{
rt.trigger(r);
}).error(e => rt.triggerError(e));
else
rt.trigger(store);
}
else
{
self.remove(store);
}
}).error(e => {
rt.triggerError(e);
self.remove(store);
});
}
return rt;
}
return new AsyncReply(null);
}
}
remove(resource)
{
if (this.resources.contains(resource.instance.id))
this.resources.remove(resource.instance.id);
else
return false;
if (resource instanceof IStore)
{
this.stores.remove(resource);
// remove all objects associated with the store
var toBeRemoved = null;
for (var i = 0; i < this.resources.length; i++)
{
var o = this.resources.at(i);
if (o.instance.store == resource)
{
if (toBeRemoved == null)
toBeRemoved = [];
toBeRemoved.push(o);
}
}
if (toBeRemoved != null)
for(var i = 0; i < toBeRemoved.length; i++)
this.remove(toBeRemoved[i]);
this._emit("disconnected", resource);
}
if (resource.instance.store != null)
resource.instance.store.remove(resource);
resource.destroy();
return true;
}
put(resource, name, store, parent, customTemplate = null, age = 0, manager = 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 (manager != null)
resource.instance.managers.add(manager);
if (parent)
{
parent.instance.children.add(resource);
}
else
{
if (!(resource instanceof IStore))
store.instance.children.add(resource);
}
if (resource instanceof IStore)
{
this.stores.add(resource);
this._emit("connected", resource);
}
else
store.put(resource);
this.resources.add(resource.instance.id, resource);
}
_onParentsRemove(value)
{
if (value.instance.children.contains(value))
value.instance.children.remove(value);
}
_onParentsAdd(value)
{
if (!value.instance.children.contains(value))
value.instance.children.add(value);
}
_onChildrenRemove(value)
{
if (value.instance.parents.contains(value))
value.instance.parents.remove(value);
}
_onChildrenAdd(value)
{
if (!value.instance.parents.contains(value))
value.instance.parents.add(value);
}
putTemplate(template)
{
this.templates.add(template.classId.valueOf(), template);
}
getTemplateByType(type)
{
// loaded ?
for (var i = 0; i < this.templates.length; i++)
if (this.templates.at(i).className == typeof(type))
return this.templates.at(i);
var template = new ResourceTemplate(type);
this.templates.add(template.classId.valueOf(), template);
return template;
}
getTemplateByClassId(classId)
{
var template = this.templates.item(classId);
return new AsyncReply(template);
}
getTemplateByClassName(className)
{
for(var i = 0; i < this.templates.length; i++)
if (this.templates.at(i).className == className)
return new AsyncReply(this.templates.at(i));
return new AsyncReply(null);
}
_qureyIn(path, index, resources)
{
var rt = [];
if (index == path.length - 1)
{
if (path[index] == "")
for(var i = 0; i < resources.length; i++)
rt.push(resources.at(i));
else
for(var i = 0; i < resources.length; i++)
if (resources.at(i).instance.name == path[index])
rt.push(resources.at(i));
}
else
for(var i = 0; i < resources.length; i++)
if (resources.at(i).instance.name == path[index])
rt = rt.concat(this._qureyIn(path, index+1, resources.at(i).instance.children));
return rt;
}
query(path)
{
var p = path.split('/');
return new AsyncReply(this._qureyIn(p, 0, this.stores));
}
}
let Warehouse = new WH();
Warehouse.protocols.add("iip", DistributedConnection);
Warehouse.protocols.add("mem", MemoryStore);
export default Warehouse;