2
0
mirror of https://github.com/esiur/esiur-js.git synced 2026-03-18 15:30:38 +00:00
This commit is contained in:
2021-03-10 01:25:20 +03:00
parent ca2b89540a
commit a08519bde8
55 changed files with 4190 additions and 6679 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -31,6 +31,10 @@ import AsyncReply from '../../Core/AsyncReply.js';
import Codec from '../../Data/Codec.js';
import Structure from '../../Data/Structure.js';
import IIPPacketAction from '../Packets//IIPPacketAction.js';
import EventTemplate from '../../Resource/Template/EventTemplate.js';
import AsyncException from '../../Core/AsyncException.js';
import ExceptionCode from '../../Core//ExceptionCode.js';
import ErrorType from '../../Core/ErrorType.js';
export default class DistributedResource extends IResource
{
@@ -166,6 +170,32 @@ export default class DistributedResource extends IResource
return true;
}
listen(event)
{
let et = event instanceof EventTemplate ? event : this.instance.template.getEventTemplateByName(event);
if (et == null)
return new AsyncReply().triggerError(new AsyncException(ErrorType.Management, ExceptionCode.MethodNotFound, ""));
if (!et.listenable)
return new AsyncReply().triggerError(new AsyncException(ErrorType.Management, ExceptionCode.NotListenable, ""));
return this._p.connection.sendListenRequest(this._p.instanceId, et.index);
}
unlisten(event)
{
let et = event instanceof EventTemplate ? event : this.instance.template.getEventTemplateByName(event);
if (et == null)
return new AsyncReply().triggerError(new AsyncException(ErrorType.Management, ExceptionCode.MethodNotFound, ""));
if (!et.listenable)
return new AsyncReply().triggerError(new AsyncException(ErrorType.Management, ExceptionCode.NotListenable, ""));
return this._p.connection.sendUnlistenRequest(this._p.instanceId, et.index);
}
_emitEventByIndex(index, args)
{
var et = this.instance.template.getEventTemplateByIndex(index);
@@ -225,7 +255,7 @@ export default class DistributedResource extends IResource
{
if (!this._p.attached)
{
console.log("What ?");
console.log("Resource not attached.");
return;
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright (c) 2017-2021 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 03/05/2021.
*/
"use strict";
import IResource from '../../Resource/IResource.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';
import EventTemplate from '../../Resource/Template/EventTemplate.js';
import AsyncException from '../../Core/AsyncException.js';
import ExceptionCode from '../../Core//ExceptionCode.js';
import ErrorType from '../../Core/ErrorType.js';
import DistributedConnection from './DistributedConnection.js';
export default class DistributedServer extends IResource
{
destroy()
{
this.connections = [];
this.destroyed = true;
this._emit("destroy", this);
}
trigger(type)
{
return new AsyncReply(true);
}
get membership() {
return this.instance.attributes.get("membership");
}
get entryPoint() {
return this.instance.attributes.get("entryPoint");
}
constructor()
{
super();
this.connections = [];
}
add() {
let self = this;
let con = new DistributedConnection(this);
con.on("close", () => self.remove(con));
this.connections.push(con);
return con;
}
remove(connection){
let i = this.connections.indexOf(connection);
if (i > -1)
this.connections.splice(i, 1);
}
}

View File

@@ -0,0 +1,8 @@
import IResource from "../../Resource/IResource";
export default class EntryPoint extends IResource
{
query(path, sender) {}
create() {}
}

View File

@@ -0,0 +1,7 @@
import IDestructible from "../Core/IDestructible";
export default class INetworkReceiver extends IDestructible {
networkClose(sender);
networkReceive(sender, buffer);
networkConnect(sender);
}

View File

@@ -26,7 +26,7 @@
"use strict";
import DC from '../../Data/DataConverter.js';
import DC from '../Data/DataConverter.js';
export default class NetworkBuffer {
constructor() {

View File

@@ -0,0 +1,5 @@
export default class NetowrkConnection extends INetworkReceiver
{
}

0
src/Net/NetworkServer.js Normal file
View File

View File

View File

@@ -391,7 +391,9 @@ export default class IIPPacket
offset += cl;
}
else if (this.action == IIPPacketAction.GetProperty)
else if (this.action == IIPPacketAction.Listen
|| this.action == IIPPacketAction.Unlisten)
//this.action == IIPPacketAction.GetProperty)
{
if (this.notEnough(offset, ends, 5))
return -this.dataLengthNeeded;
@@ -402,20 +404,20 @@ export default class IIPPacket
this.methodIndex = data.getUint8(offset++);
}
else if (this.action == IIPPacketAction.GetPropertyIfModified)
{
if (this.notEnough(offset, ends, 9))
return -this.dataLengthNeeded;
// else if (this.action == IIPPacketAction.GetPropertyIfModified)
// {
// if (this.notEnough(offset, ends, 9))
// return -this.dataLengthNeeded;
this.resourceId = data.getUint32(offset);
offset += 4;
// this.resourceId = data.getUint32(offset);
// offset += 4;
this.methodIndex = data[offset++];
// this.methodIndex = data[offset++];
this.resourceAge = data.getUint64(offset);
offset += 8;
// this.resourceAge = data.getUint64(offset);
// offset += 8;
}
// }
else if (this.action == IIPPacketAction.SetProperty)
{
if (this.notEnough(offset, ends, 6))
@@ -552,9 +554,9 @@ export default class IIPPacket
offset += cl;
}
else if (this.action == IIPPacketAction.InvokeFunctionArrayArguments
|| this.action == IIPPacketAction.InvokeFunctionNamedArguments
|| this.action == IIPPacketAction.GetProperty
|| this.action == IIPPacketAction.GetPropertyIfModified)
|| this.action == IIPPacketAction.InvokeFunctionNamedArguments)
//|| this.action == IIPPacketAction.GetProperty
//|| this.action == IIPPacketAction.GetPropertyIfModified)
{
if (this.notEnough(offset, ends, 1))
@@ -586,7 +588,9 @@ export default class IIPPacket
offset += size;
}
}
else if (this.action == IIPPacketAction.SetProperty)
else if (this.action == IIPPacketAction.SetProperty
|| this.action == IIPPacketAction.Listen
|| this.action == IIPPacketAction.Unlisten)
{
// nothing to do
}

View File

@@ -21,10 +21,10 @@ export default // const IIPPacketAction =
// Request Invoke
InvokeFunctionArrayArguments: 16,
GetProperty: 17,
GetPropertyIfModified: 18,
SetProperty: 19,
InvokeFunctionNamedArguments: 20,
InvokeFunctionNamedArguments: 17,
Listen: 18,
Unlisten: 19,
SetProperty: 20,
// Request Attribute
GetAllAttributes: 24,

View File

@@ -39,7 +39,7 @@ export default class SendList extends BinaryList
done()
{
this.connection.send(this.toArray());
this.connection.sendAll(this.toArray());
return this.reply;
}
}

View File

@@ -0,0 +1,34 @@
"use strict";
import IDestructible from "../../Core/IDestructible.js";
import SocketState from "./SocketState.js";
export default class ISocket extends IDestructible
{
//SocketState State { get; }
//INetworkReceiver<ISocket> Receiver { get; set; }
constructor(){
super();
this.state = SocketState.Initial;
}
// get state() {}
sendAsync(message, offset, length) { }
send(message, offset, length) {}
close() {}
connect(hostname, port) {}
begin() {}
beginAsync() {}
acceptAsync() {}
accept() {}
get remoteEndPoint(){}
get localEndPoint() {}
hold() {}
unhold() {}
}

View File

@@ -0,0 +1,7 @@
export default {
Initial : 0,
Listening : 1,
Connecting : 2,
Established : 3,
Closed: 4,
}

165
src/Net/Sockets/WSSocket.js Normal file
View File

@@ -0,0 +1,165 @@
import AsyncReply from "../../Core/AsyncReply.js";
import ErrorType from "../../Core/ErrorType.js";
import ExceptionCode from "../../Core/ExceptionCode.js";
import ISocket from "./ISocket.js";
import SocketState from "./SocketState.js";
import NetworkBuffer from "../NetworkBuffer.js";
export default class WSSocket extends ISocket
{
//SocketState State { get; }
//INetworkReceiver<ISocket> Receiver { get; set; }
constructor(websocket){
super();
this.receiveNetworkBuffer = new NetworkBuffer();
this.sendNetworkBuffer = new NetworkBuffer();
this.held = false;
if (websocket != null)// instanceof WebSocket)
{
//websocket.onerror = () => {
// self.state = SocketState.Closed;
//};
websocket.onopen = () => {
self.state = SocketState.Established;
};
websocket.onerror = () => {
self.state = SocketState.Closed;
};
this._assign(websocket);
}
}
destroy(){
this.close();
this.receiveNetworkBuffer = null;
this.receiver = null;
thsi.ws = null;
this._emit("destroy");
}
sendAsync(message, offset, length) {
}
sendAll(message)
{
if (this.held)
this.sendNetworkBuffer.writeAll(message);
else
{
try
{
this.ws.send(message);
} catch {
this.state = SocketState.Closed;
}
}
}
send(message, offset, length) {
this.sendAll(message.clip(offset, length));
}
close() {
this.ws.close();
}
connect(hostname, port, secure = false) {
let self = this;
var rt = new AsyncReply();
this.state = SocketState.Connecting;
this.url = `ws${secure ? 's' : ''}://${hostname}:${port}`;
let ws = new WebSocket(this.url, "iip");
ws.binaryType = "arraybuffer";
ws.onopen = () => {
self.state = SocketState.Established;
rt.trigger(true);
};
ws.onerror = () => {
self.state = SocketState.Closed;
rt.triggerError(ErrorType.Management, ExceptionCode.HostNotReachable);
};
this._assign(ws);
return rt;// new AsyncReply(true);
}
_assign(ws)
{
let self = this;
ws.onclose = () => {
self.state = SocketState.Closed;
self.receiver?.networkClose(self);
};
ws.onmessage = function(msg) {
self.receiveNetworkBuffer.writeAll(msg.data);
self.receiver.networkReceive(this, self.receiveNetworkBuffer);
//self.lastAction = new Date();
};
this.ws = ws;
}
begin() {
}
beginAsync() {
}
acceptAsync() {
}
accept() {
}
get remoteEndPoint(){}
get localEndPoint() {}
hold()
{
this.held = true;
}
unhold()
{
this.held = false;
var message = this.sendNetworkBuffer.read();
if (message == null)
return;
// totalSent += message.Length;
try {
this.ws.send(message);
} catch {
this.state = SocketState.Closed;
}
}
}
// if (this.holdSending) {
// //console.log("hold ", data.length);
// this.sendBuffer.writeAll(data);
// }
// else
// //console.log("Send", data.length);