2
0
mirror of https://github.com/esiur/esiur-js.git synced 2025-05-06 04:22: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 WebSocket = require('ws');
const http = require("http");
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 ms = await Warehouse.new(MyMembership, "ms");
let chat = await Warehouse.new(MyChat, "chat", sys);
server = await Warehouse.new(DistributedServer, "dss", sys, null, null, {membership: ms, entryPoint: chat});
wss.on('connection', function connection(ws)
{
let con = server.add();
@ -97,5 +101,6 @@ http.createServer(function (req, res) {
});
}).listen(8000);
console.log(`HTTP Server running http://localhost:8000/demo/chat/index.html`);
console.log(`IIP Server running iip://localhost:8001`);

View File

@ -15,7 +15,7 @@
try {
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";
service.on("message", appendMessage)
.on(":title", updateTitle)

View File

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

View File

@ -105,7 +105,7 @@ export default class AsyncReply extends Promise
if (this.ready)
return this;
if (this.exception != null)
if (this.exception.raised)
return this;
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)
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)
@ -226,7 +226,7 @@ export default class DistributedResource extends IResource
if (!et.listenable)
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)
@ -257,9 +257,8 @@ export default class DistributedResource extends IResource
if (ft.isStatic)
return this._p.connection.staticCall(this.instance.template.classId, index, args);
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)
@ -301,7 +300,7 @@ export default class DistributedResource extends IResource
var parameters = Codec.compose(value, this._p.connection);
var self = this;
this._p.connection.sendRequest(IIPPacketAction.SetProperty)
this._p.connection._sendRequest(IIPPacketAction.SetProperty)
.addUint32(self._p.instanceId).addUint8(index).addUint8Array(parameters)
.done()
.then(function(res)

View File

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

View File

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

View File

@ -82,12 +82,19 @@ export default class Instance extends IEventHandler
loadProperty(name, age, modificationDate, value)
{
let r = self.resource.deref();
if (r == null) return;
var pt = this.template.getPropertyTemplateByName(name);
if (pt == null)
return false;
this.resource[name] = value;
r[name] = value;
this.setAge(pt.index, age);
this.setModificationDate(pt.index, modificationDate);
@ -113,10 +120,15 @@ export default class Instance extends IEventHandler
serialize()
{
let r = this.resource.deref();
if (r == null) return;
var props = new PropertyValueArray();
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.modificationDates[this.template.properties[i].index]));
@ -130,6 +142,11 @@ export default class Instance extends IEventHandler
emitModification(pt, value)
{
let resource = this.resource.deref();
if (resource == null) return;
this.instanceAge++;
var now = new Date();
@ -138,18 +155,15 @@ export default class Instance extends IEventHandler
this.modificationDates[pt.index] = now;
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
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);
this.resource._emit(`:${pt.name}`, value);
//this.resource.emitProperty(pmInfo);
//super._emit("ResourceModified", this.resource, pt.name, value);
//this.resource._emit(":" + pt.name, value);
resource._emit(`:${pt.name}`, value);
}
modified(propertyName = null)
@ -167,17 +181,25 @@ export default class Instance extends IEventHandler
_emitResourceEvent(issuer, receivers, eventTemplate, value)
{
let resource = this.resource.deref();
if (resource == null) return;
super._emit("EventOccurred",
new EventOccurredInfo(this.resource, eventTemplate, value, issuer, receivers));
//super._emit("ResourceEventOccurred", this.resource, issuer, receivers, name, args);
new EventOccurredInfo(resource, eventTemplate, value, issuer, receivers));
}
getPropertyValue(name, resultObject)
{
let resource = this.resource.deref();
if (resource == null) return;
for (var i = 0; i < this.template.properties.length; i++)
if (this.template.properties[i].name == name)
{
resultObject.value = this.resource[name];
resultObject.value = resource[name];
return true;
}
@ -194,7 +216,7 @@ export default class Instance extends IEventHandler
super();
this.store = store;
this.resource = resource;
this.resource = new WeakRef(resource);
this.id = id;
this.name = name;
@ -210,22 +232,27 @@ export default class Instance extends IEventHandler
var self = this;
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){
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);
});
if (customTemplate != null)
this.template = customTemplate;
else
this.template = Warehouse.getTemplateByType(this.resource.constructor);
this.template = Warehouse.getTemplateByType(resource.constructor);
// set ages
this.ages = [];
@ -239,7 +266,7 @@ export default class Instance extends IEventHandler
// connect events
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>
applicable(session, action, member, inquirer)
{
let resource = this.resource.deref();
if (resource == null) return;
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;
var ruling = this.managers.item(i).applicable(resource, session, action, member, inquirer);
if (ruling != Ruling.DontCare)
return ruling;
}
return Ruling.DontCare;
@ -335,8 +366,8 @@ export default class Instance extends IEventHandler
setAttributes(attributes, clearAttributes = false)
{
if (clearAttributes)
this.attributes.clear();
if (clearAttributes)
this.attributes.clear();
for (var attr in attributes)
@ -360,7 +391,10 @@ export default class Instance extends IEventHandler
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);
}
else
@ -375,4 +409,17 @@ export default class Instance extends IEventHandler
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));
// [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,
fi[3]);

View File

@ -51,15 +51,30 @@ export default class MemoryStore extends IStore
return new AsyncReply(null);
}
get(resource)
get(path)
{
return new AsyncReply(null);
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);
}
link(resource)
{
if (resource.instance.store == this)
return this.instance.name + "/" + resource.instance.id;
return this.instance.name + "/$" + resource.instance.id;
}
trigger(trigger)