2
0
mirror of https://github.com/esiur/esiur-js.git synced 2025-05-06 12:32:58 +00:00

Instance.Link

This commit is contained in:
Esiur Project 2022-08-13 04:00:26 +03:00
parent 1985363ad7
commit 2689bb7fd5
12 changed files with 1595 additions and 829 deletions

File diff suppressed because it is too large Load Diff

View File

@ -13,6 +13,9 @@ import TypedMap from "../../src/Data/TypedMap.js";
const require = createRequire(import.meta.url); const require = createRequire(import.meta.url);
const WebSocket = require('ws'); const WebSocket = require('ws');
const http = require("http"); const http = require("http");
const fs = require("fs"); const fs = require("fs");
@ -65,14 +68,15 @@ class MyChat extends IResource {
} }
let x = TransmissionType.compose(TransmissionTypeIdentifier.List, new DC([1,2,3,4,5,6]));
let tt = TransmissionType.parse(x, 0, x.length);
let sys = await Warehouse.new(MemoryStore, "sys"); let sys = await Warehouse.new(MemoryStore, "sys");
let ms = await Warehouse.new(MyMembership, "ms"); let ms = await Warehouse.new(MyMembership, "ms");
let chat = await Warehouse.new(MyChat, "chat", sys); let chat = await Warehouse.new(MyChat, "chat", sys);
server = await Warehouse.new(DistributedServer, "dss", sys, null, null, {membership: ms, entryPoint: chat}); server = await Warehouse.new(DistributedServer, "dss", sys, null, null, {membership: ms, entryPoint: chat});
wss.on('connection', function connection(ws) wss.on('connection', function connection(ws)
{ {
let con = server.add(); let con = server.add();
@ -97,5 +101,6 @@ http.createServer(function (req, res) {
}); });
}).listen(8000); }).listen(8000);
console.log(`HTTP Server running http://localhost:8000/demo/chat/index.html`); console.log(`HTTP Server running http://localhost:8000/demo/chat/index.html`);
console.log(`IIP Server running iip://localhost:8001`); console.log(`IIP Server running iip://localhost:8001`);

View File

@ -15,7 +15,7 @@
try { try {
status.innerHTML = "Connecting..."; status.innerHTML = "Connecting...";
service = await wh.get("iip://localhost:8001/chat", {username, password: "1234"}); service = await wh.get("iip://localhost:8001/chat", {username, password: "1234", autoReconnect: true});
login.style.display = "none"; login.style.display = "none";
service.on("message", appendMessage) service.on("message", appendMessage)
.on(":title", updateTitle) .on(":title", updateTitle)

View File

@ -1,6 +1,6 @@
{ {
"name": "esiur", "name": "esiur",
"version": "2.0.7", "version": "2.0.9",
"description": "Distributed Object Framework", "description": "Distributed Object Framework",
"main": "esiur.js", "main": "esiur.js",
"type": "module", "type": "module",

View File

@ -105,7 +105,7 @@ export default class AsyncReply extends Promise
if (this.ready) if (this.ready)
return this; return this;
if (this.exception != null) if (this.exception.raised)
return this; return this;
this.result = result; this.result = result;

File diff suppressed because it is too large Load Diff

View File

@ -213,7 +213,7 @@ export default class DistributedResource extends IResource
if (!et.listenable) if (!et.listenable)
return new AsyncReply().triggerError(new AsyncException(ErrorType.Management, ExceptionCode.NotListenable, "")); return new AsyncReply().triggerError(new AsyncException(ErrorType.Management, ExceptionCode.NotListenable, ""));
return this._p.connection.sendListenRequest(this._p.instanceId, et.index); return this._p.connection._sendListenRequest(this._p.instanceId, et.index);
} }
unlisten(event) unlisten(event)
@ -226,7 +226,7 @@ export default class DistributedResource extends IResource
if (!et.listenable) if (!et.listenable)
return new AsyncReply().triggerError(new AsyncException(ErrorType.Management, ExceptionCode.NotListenable, "")); return new AsyncReply().triggerError(new AsyncException(ErrorType.Management, ExceptionCode.NotListenable, ""));
return this._p.connection.sendUnlistenRequest(this._p.instanceId, et.index); return this._p.connection._sendUnlistenRequest(this._p.instanceId, et.index);
} }
_emitEventByIndex(index, args) _emitEventByIndex(index, args)
@ -257,9 +257,8 @@ export default class DistributedResource extends IResource
if (ft.isStatic) if (ft.isStatic)
return this._p.connection.staticCall(this.instance.template.classId, index, args); return this._p.connection.staticCall(this.instance.template.classId, index, args);
else else
return this._p.connection.sendInvoke(this._p.instanceId, index, args); return this._p.connection._sendInvoke(this._p.instanceId, index, args);
//return this._p.connection.sendInvoke(this._p.instanceId, index, args);
} }
_get(index) _get(index)
@ -301,7 +300,7 @@ export default class DistributedResource extends IResource
var parameters = Codec.compose(value, this._p.connection); var parameters = Codec.compose(value, this._p.connection);
var self = this; var self = this;
this._p.connection.sendRequest(IIPPacketAction.SetProperty) this._p.connection._sendRequest(IIPPacketAction.SetProperty)
.addUint32(self._p.instanceId).addUint8(index).addUint8Array(parameters) .addUint32(self._p.instanceId).addUint8(index).addUint8Array(parameters)
.done() .done()
.then(function(res) .then(function(res)

View File

@ -31,6 +31,7 @@ import IIPPacketCommand from "./IIPPacketCommand.js";
import IIPPacketEvent from "./IIPPacketEvent.js"; import IIPPacketEvent from "./IIPPacketEvent.js";
import IIPPacketReport from "./IIPPacketReport.js"; import IIPPacketReport from "./IIPPacketReport.js";
import TransmissionType from '../../Data/TransmissionType.js'; import TransmissionType from '../../Data/TransmissionType.js';
import ExceptionCode from "../../Core/ExceptionCode.js";
export default class IIPPacket export default class IIPPacket
{ {
@ -81,6 +82,7 @@ export default class IIPPacket
this.command = (data.getUint8(offset) >> 6); this.command = (data.getUint8(offset) >> 6);
if (this.command == IIPPacketCommand.Event) if (this.command == IIPPacketCommand.Event)
{ {
this.event = (data.getUint8(offset++) & 0x3f); this.event = (data.getUint8(offset++) & 0x3f);
@ -183,6 +185,9 @@ export default class IIPPacket
offset += cl; offset += cl;
} }
else {
throw new Error("Unknown event packet.");
}
} }
else if (this.command == IIPPacketCommand.Request) else if (this.command == IIPPacketCommand.Request)
{ {
@ -478,6 +483,9 @@ export default class IIPPacket
offset += parsed.size; offset += parsed.size;
} }
else {
throw new Error("Unknown request packet.");
}
} }
else if (this.command == IIPPacketCommand.Reply) else if (this.command == IIPPacketCommand.Reply)
{ {
@ -580,9 +588,12 @@ export default class IIPPacket
this.currentTime = data.getDateTime(offset); this.currentTime = data.getDateTime(offset);
offset += 8; offset += 8;
this.jitter = data.GetUint32(offset); this.jitter = data.getUint32(offset);
offset += 4; offset += 4;
} }
else {
throw new Error("Unknown reply packet.");
}
} }
else if (this.command == IIPPacketCommand.Report) else if (this.command == IIPPacketCommand.Report)
{ {
@ -637,6 +648,9 @@ export default class IIPPacket
offset += parsed.size; offset += parsed.size;
} }
else {
throw new Error("Unknown report packet.");
}
} }
return offset - this.originalOffset; return offset - this.originalOffset;

View File

@ -4,6 +4,7 @@ import ExceptionCode from "../../Core/ExceptionCode.js";
import ISocket from "./ISocket.js"; import ISocket from "./ISocket.js";
import SocketState from "./SocketState.js"; import SocketState from "./SocketState.js";
import NetworkBuffer from "../NetworkBuffer.js"; import NetworkBuffer from "../NetworkBuffer.js";
import DC from '../../Data/DC.js';
export default class WSocket extends ISocket export default class WSocket extends ISocket
{ {
@ -51,6 +52,7 @@ export default class WSocket extends ISocket
{ {
try try
{ {
//console.log("TX", new DC(message));
this.ws.send(message); this.ws.send(message);
} catch { } catch {
this.state = SocketState.Closed; this.state = SocketState.Closed;
@ -172,6 +174,7 @@ export default class WSocket extends ISocket
try { try {
this.ws.send(message); this.ws.send(message);
//console.log("TX", message);
} catch { } catch {
this.state = SocketState.Closed; this.state = SocketState.Closed;
} }

View File

@ -82,12 +82,19 @@ export default class Instance extends IEventHandler
loadProperty(name, age, modificationDate, value) loadProperty(name, age, modificationDate, value)
{ {
let r = self.resource.deref();
if (r == null) return;
var pt = this.template.getPropertyTemplateByName(name); var pt = this.template.getPropertyTemplateByName(name);
if (pt == null) if (pt == null)
return false; return false;
this.resource[name] = value;
r[name] = value;
this.setAge(pt.index, age); this.setAge(pt.index, age);
this.setModificationDate(pt.index, modificationDate); this.setModificationDate(pt.index, modificationDate);
@ -113,10 +120,15 @@ export default class Instance extends IEventHandler
serialize() serialize()
{ {
let r = this.resource.deref();
if (r == null) return;
var props = new PropertyValueArray(); var props = new PropertyValueArray();
for (var i = 0; i < this.template.properties.length; i++) for (var i = 0; i < this.template.properties.length; i++)
props.push(new PropertyValue(this.resource[this.template.properties[i].name], props.push(new PropertyValue(r[this.template.properties[i].name],
this.ages[this.template.properties[i].index], this.ages[this.template.properties[i].index],
this.modificationDates[this.template.properties[i].index])); this.modificationDates[this.template.properties[i].index]));
@ -130,6 +142,11 @@ export default class Instance extends IEventHandler
emitModification(pt, value) emitModification(pt, value)
{ {
let resource = this.resource.deref();
if (resource == null) return;
this.instanceAge++; this.instanceAge++;
var now = new Date(); var now = new Date();
@ -138,18 +155,15 @@ export default class Instance extends IEventHandler
this.modificationDates[pt.index] = now; this.modificationDates[pt.index] = now;
if (pt.recordable) if (pt.recordable)
this.store.record(this.resource, pt.name, value, this.ages[pt.index], now); this.store.record(resource, pt.name, value, this.ages[pt.index], now);
else else
this.store.modify(this.resource, pt.name, value, this.ages[pt.index], now); this.store.modify(resource, pt.name, value, this.ages[pt.index], now);
let pmInfo = new PropertyModificationInfo(this.resource, pt, value, this.instanceAge); let pmInfo = new PropertyModificationInfo(resource, pt, value, this.instanceAge);
super._emit("PropertyModified", pmInfo); super._emit("PropertyModified", pmInfo);
this.resource._emit(`:${pt.name}`, value); resource._emit(`:${pt.name}`, value);
//this.resource.emitProperty(pmInfo);
//super._emit("ResourceModified", this.resource, pt.name, value);
//this.resource._emit(":" + pt.name, value);
} }
modified(propertyName = null) modified(propertyName = null)
@ -167,17 +181,25 @@ export default class Instance extends IEventHandler
_emitResourceEvent(issuer, receivers, eventTemplate, value) _emitResourceEvent(issuer, receivers, eventTemplate, value)
{ {
let resource = this.resource.deref();
if (resource == null) return;
super._emit("EventOccurred", super._emit("EventOccurred",
new EventOccurredInfo(this.resource, eventTemplate, value, issuer, receivers)); new EventOccurredInfo(resource, eventTemplate, value, issuer, receivers));
//super._emit("ResourceEventOccurred", this.resource, issuer, receivers, name, args);
} }
getPropertyValue(name, resultObject) getPropertyValue(name, resultObject)
{ {
let resource = this.resource.deref();
if (resource == null) return;
for (var i = 0; i < this.template.properties.length; i++) for (var i = 0; i < this.template.properties.length; i++)
if (this.template.properties[i].name == name) if (this.template.properties[i].name == name)
{ {
resultObject.value = this.resource[name]; resultObject.value = resource[name];
return true; return true;
} }
@ -194,7 +216,7 @@ export default class Instance extends IEventHandler
super(); super();
this.store = store; this.store = store;
this.resource = resource; this.resource = new WeakRef(resource);
this.id = id; this.id = id;
this.name = name; this.name = name;
@ -210,22 +232,27 @@ export default class Instance extends IEventHandler
var self = this; var self = this;
this.children.on("add", function(value){ this.children.on("add", function(value){
value.instance.parents.add(self.resource); let r = self.resource.deref();
if (r != null)
value.instance.parents.add(r);
}); });
this.children.on("remove", function(value){ this.children.on("remove", function(value){
value.instance.parents.remove(self.resource); let r = self.resource.deref();
if (r != null)
value.instance.parents.remove(r);
}); });
this.resource.on("Destroy", function(sender){ resource.on("destroy", function(sender){
self._emit("ResourceDestroyed", sender); self._emit("ResourceDestroyed", sender);
}); });
if (customTemplate != null) if (customTemplate != null)
this.template = customTemplate; this.template = customTemplate;
else else
this.template = Warehouse.getTemplateByType(this.resource.constructor); this.template = Warehouse.getTemplateByType(resource.constructor);
// set ages // set ages
this.ages = []; this.ages = [];
@ -239,7 +266,7 @@ export default class Instance extends IEventHandler
// connect events // connect events
for (let i = 0; i < this.template.events.length; i++) for (let i = 0; i < this.template.events.length; i++)
this.resource.on(this.template.events[i].name, this._makeHandler(this.template.events[i])); resource.on(this.template.events[i].name, this._makeHandler(this.template.events[i]));
} }
@ -265,11 +292,15 @@ export default class Instance extends IEventHandler
/// <returns>Ruling.</returns> /// <returns>Ruling.</returns>
applicable(session, action, member, inquirer) applicable(session, action, member, inquirer)
{ {
let resource = this.resource.deref();
if (resource == null) return;
for (var i = 0; i < this.managers.length; i++) for (var i = 0; i < this.managers.length; i++)
{ {
var r = this.managers.item(i).applicable(this.resource, session, action, member, inquirer); var ruling = this.managers.item(i).applicable(resource, session, action, member, inquirer);
if (r != Ruling.DontCare) if (ruling != Ruling.DontCare)
return r; return ruling;
} }
return Ruling.DontCare; return Ruling.DontCare;
@ -360,7 +391,10 @@ export default class Instance extends IEventHandler
if (manager instanceof IPermissionsManager) if (manager instanceof IPermissionsManager)
{ {
manager.initialize(settings, this.resource); let r = this.resource.deref();
if (r == null) return;
manager.initialize(settings, r);
this.managers.add(manager); this.managers.add(manager);
} }
else else
@ -375,4 +409,17 @@ export default class Instance extends IEventHandler
return true; return true;
} }
get link()
{
let resource = this.resource.deref();
if (resource == null)
return;
if (resource == this.store){
return this.name;
} else {
return this.store.link(resource);
}
}
} }

View File

@ -318,7 +318,9 @@ export default class TypeTemplate {
?? RepresentationType.Dynamic, fi[1][ai][2]?.optional, ai)); ?? RepresentationType.Dynamic, fi[1][ai][2]?.optional, ai));
// [name, {param1: type, param2: int}, returnType, "Description"] // [name, {param1: type, param2: int}, returnType, "Description"]
var ft = new FunctionTemplate(this, i, fi[0], false, args, let isStatic = type[fi[0]] instanceof Function;
var ft = new FunctionTemplate(this, i, fi[0], false, isStatic, args,
RepresentationType.fromType(fi[2]) ?? RepresentationType.Void, RepresentationType.fromType(fi[2]) ?? RepresentationType.Void,
fi[3]); fi[3]);

View File

@ -51,15 +51,30 @@ export default class MemoryStore extends IStore
return new AsyncReply(null); return new AsyncReply(null);
} }
get(resource) get(path)
{ {
if (path.startsWith("$"))
{
let id = parseInt(path.substring(1));
return new AsyncReply (this.resources.get(id));
}
else
{
for(let r of this.resources.values())
{
if (r.instance.name == path)
return new AsyncReply(r);
}
}
return new AsyncReply(null); return new AsyncReply(null);
} }
link(resource) link(resource)
{ {
if (resource.instance.store == this) if (resource.instance.store == this)
return this.instance.name + "/" + resource.instance.id; return this.instance.name + "/$" + resource.instance.id;
} }
trigger(trigger) trigger(trigger)