From 9c15b830dd5cd9baede203e6342b5d7863b790f9 Mon Sep 17 00:00:00 2001 From: Ahmed Zamil Date: Mon, 2 Dec 2019 03:29:30 +0300 Subject: [PATCH] Rename --- README.md | 6 +- lib/src/Core/AsyncBag.dart | 5 + lib/src/Core/AsyncQueue.dart | 2 +- lib/src/Core/AsyncReply.dart | 13 +- lib/src/Core/IDestructible.dart | 2 +- lib/src/Data/Codec.dart | 9 +- lib/src/Net/IIP/DistributedConnection.dart | 13 ++ lib/src/Net/IIP/DistributedResource.dart | 15 +- lib/src/Net/NetworkConnection.dart | 3 + lib/src/Net/Packets/IIPPacket.dart | 4 +- lib/src/Net/Sockets/TCPSocket.dart | 1 + lib/src/Net/Sockets/Websocket.dart | 189 +++++++++++++++++++++ lib/src/Resource/Instance.dart | 1 + pubspec.lock | 2 +- pubspec.yaml | 6 +- test/main.dart | 57 ++++++- 16 files changed, 298 insertions(+), 30 deletions(-) create mode 100644 lib/src/Net/Sockets/Websocket.dart diff --git a/README.md b/README.md index 73c45cc..a908c63 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# Esiur +# Esyur Distributed Object Framework ## Getting Started -For help getting started with Esiur, view our -[online documentation](https://esiur.com), which offers tutorials, +For help getting started with Esyur, view our +[online documentation](http://www.esyur.com), which offers tutorials, samples, guidance on mobile development, and a full API reference. diff --git a/lib/src/Core/AsyncBag.dart b/lib/src/Core/AsyncBag.dart index c806fd4..ab3a509 100644 --- a/lib/src/Core/AsyncBag.dart +++ b/lib/src/Core/AsyncBag.dart @@ -11,6 +11,8 @@ class AsyncBag extends AsyncReply> seal() { + //print("SEALED"); + if (_sealedBag) return; @@ -28,8 +30,11 @@ class AsyncBag extends AsyncReply> { _results[index] = r; _count++; + //print("Seal ${_count}/${_results.length}"); if (_count == _results.length) trigger(_results); + }).error((ex){ + triggerError(ex); }); } } diff --git a/lib/src/Core/AsyncQueue.dart b/lib/src/Core/AsyncQueue.dart index c1339d7..4c76435 100644 --- a/lib/src/Core/AsyncQueue.dart +++ b/lib/src/Core/AsyncQueue.dart @@ -1,4 +1,4 @@ -library esiur; +library esyur; import 'AsyncReply.dart'; diff --git a/lib/src/Core/AsyncReply.dart b/lib/src/Core/AsyncReply.dart index 2be9080..28396b9 100644 --- a/lib/src/Core/AsyncReply.dart +++ b/lib/src/Core/AsyncReply.dart @@ -107,18 +107,7 @@ class AsyncReply implements Future { return this; } - - AsyncReply _then_old(Function(T) callback) - { - _callbacks.add(callback); - - if (_resultReady) - callback(result); - - return this; - } - - + AsyncReply error(Function(dynamic) callback) { _errorCallbacks.add(callback); diff --git a/lib/src/Core/IDestructible.dart b/lib/src/Core/IDestructible.dart index 748d135..3bdbf0f 100644 --- a/lib/src/Core/IDestructible.dart +++ b/lib/src/Core/IDestructible.dart @@ -1,4 +1,4 @@ -// library esiur; +// library esyur; import 'IEventHandler.dart'; diff --git a/lib/src/Data/Codec.dart b/lib/src/Data/Codec.dart index 255a2b0..271bc3c 100644 --- a/lib/src/Data/Codec.dart +++ b/lib/src/Data/Codec.dart @@ -493,10 +493,10 @@ class Codec return new AsyncReply.ready(data.getUint64(offset)); case DataType.Float32: - return new AsyncReply.ready(data.getFloat32(offset)); + return new AsyncReply.ready(data.getFloat32(offset)); case DataType.Float64: - return new AsyncReply.ready(data.getFloat64(offset)); + return new AsyncReply.ready(data.getFloat64(offset)); case DataType.String: return new AsyncReply.ready(data.getString(offset, contentLength)); @@ -649,6 +649,8 @@ class Codec /// Array of resources. static AsyncBag parseResourceArray(DC data, int offset, int length, DistributedConnection connection) { + //print("parseResourceArray ${offset} ${length}"); + var reply = new AsyncBag(); if (length == 0) { @@ -912,6 +914,9 @@ class Codec /// static AsyncBag parsePropertyValueArray(DC data, int offset, int length, DistributedConnection connection)//, bool ageIncluded = true) { + + //print("parsePropertyValueArray ${offset} ${length}"); + var rt = new AsyncBag(); var sizeObject = new SizeObject(); diff --git a/lib/src/Net/IIP/DistributedConnection.dart b/lib/src/Net/IIP/DistributedConnection.dart index 11bbda5..cb19862 100644 --- a/lib/src/Net/IIP/DistributedConnection.dart +++ b/lib/src/Net/IIP/DistributedConnection.dart @@ -341,14 +341,18 @@ class DistributedConnection extends NetworkConnection with IStore if (rt <= 0) { + // print("hold"); var size = ends - offset; data.holdFor(msg, offset, size, size - rt); return ends; } else { + //print("CMD ${packet.command} ${offset} ${ends}"); + offset += rt; + if (packet.command == IIPPacketCommand.Event) { switch (packet.event) @@ -2158,15 +2162,23 @@ class DistributedConnection extends NetworkConnection with IStore var reply = new AsyncReply(); _resourceRequests.add(id, reply); + //print("fetch ${id}"); + sendRequest(IIPPacketAction.AttachResource) .addUint32(id) .done() .then((rt) { + + //print("fetched ${id}"); + var dr = new DistributedResource(this, id, rt[1], rt[2]); + getTemplate(rt[0] as Guid).then((tmp) { + //print("New template "); + // ClassId, ResourceAge, ResourceLink, Content Warehouse.put(dr, id.toString(), this, null, tmp); @@ -2174,6 +2186,7 @@ class DistributedConnection extends NetworkConnection with IStore Codec.parsePropertyValueArray(d, 0, d.length, this).then((ar) { + //print("attached"); dr.attached(ar); _resourceRequests.remove(id); reply.trigger(dr); diff --git a/lib/src/Net/IIP/DistributedResource.dart b/lib/src/Net/IIP/DistributedResource.dart index dfbbd23..e9120a7 100644 --- a/lib/src/Net/IIP/DistributedResource.dart +++ b/lib/src/Net/IIP/DistributedResource.dart @@ -80,7 +80,7 @@ class DistributedResource extends IResource /// /// Resource is attached when all its properties are received. /// - bool get IsAttached => _isAttached; + bool get isAttached => _isAttached; // public DistributedResourceStack Stack @@ -185,6 +185,19 @@ class DistributedResource extends IResource return connection.sendInvokeByArrayArguments(_instanceId, index, args); } + operator [](String index) + { + var pt = instance.template.getPropertyTemplateByName(index); + if (pt != null) + return get(pt.index); + } + + operator []=(String index, value) + { + var pt = instance.template.getPropertyTemplateByName(index); + if (pt != null) + set(pt.index, value); + } String _getMemberName(Symbol symbol) { diff --git a/lib/src/Net/NetworkConnection.dart b/lib/src/Net/NetworkConnection.dart index e2c4014..c73a317 100644 --- a/lib/src/Net/NetworkConnection.dart +++ b/lib/src/Net/NetworkConnection.dart @@ -91,6 +91,7 @@ class NetworkConnection extends IDestructible { try { + // Unassigned ? if (_sock == null) return; @@ -121,8 +122,10 @@ class NetworkConnection extends IDestructible } catch (ex) { + print(ex); //Global.Log("NetworkConnection", LogType.Warning, ex.ToString()); } + } ISocket unassign() diff --git a/lib/src/Net/Packets/IIPPacket.dart b/lib/src/Net/Packets/IIPPacket.dart index 1beefcb..ffb3a43 100644 --- a/lib/src/Net/Packets/IIPPacket.dart +++ b/lib/src/Net/Packets/IIPPacket.dart @@ -86,8 +86,8 @@ class IIPPacket { if (offset + needed > ends) { - //dataLengthNeeded = needed - (ends - offset); - _dataLengthNeeded = needed - (ends - _originalOffset); + _dataLengthNeeded = needed - (ends - offset); + //_dataLengthNeeded = (needed - (ends - offset)) + (offset - _originalOffset); return true; } diff --git a/lib/src/Net/Sockets/TCPSocket.dart b/lib/src/Net/Sockets/TCPSocket.dart index 69477be..f8e53fa 100644 --- a/lib/src/Net/Sockets/TCPSocket.dart +++ b/lib/src/Net/Sockets/TCPSocket.dart @@ -79,6 +79,7 @@ class TCPSocket extends ISocket if (_state == SocketState.Closed || _state == SocketState.Terminated) return; + var dc = new DC.fromList(data); receiveNetworkBuffer.write(dc, 0, dc.length); emitArgs("receive", [receiveNetworkBuffer]); diff --git a/lib/src/Net/Sockets/Websocket.dart b/lib/src/Net/Sockets/Websocket.dart new file mode 100644 index 0000000..51ed614 --- /dev/null +++ b/lib/src/Net/Sockets/Websocket.dart @@ -0,0 +1,189 @@ +/* + +Copyright (c) 2019 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. + +*/ + +import 'dart:io'; +import 'ISocket.dart'; +import '../../Data/DC.dart'; +import '../NetworkBuffer.dart'; +import 'SocketState.dart'; +import 'IPEndPoint.dart'; +import '../../Core/AsyncReply.dart'; + +import 'package:web_socket_channel/io.dart'; +import 'package:web_socket_channel/web_socket_channel.dart'; +import 'package:web_socket_channel/status.dart' as status; + +class WSSocket extends ISocket +{ + WebSocketChannel sock; + + NetworkBuffer receiveNetworkBuffer = new NetworkBuffer(); + + bool began = false; + + SocketState _state = SocketState.Initial; + + + IPEndPoint _localEP, _remoteEP; + + bool begin() + { + if (began) + return false; + + began = true; + + _localEP = IPEndPoint(sock. address.rawAddress, sock.port); + _remoteEP = IPEndPoint(sock.remoteAddress.rawAddress, sock.remotePort); + return true; + } + + void dataHandler(List data){ + //print(new String.fromCharCodes(data).trim()); + + + try + { + if (_state == SocketState.Closed || _state == SocketState.Terminated) + return; + + + var dc = new DC.fromList(data); + receiveNetworkBuffer.write(dc, 0, dc.length); + emitArgs("receive", [receiveNetworkBuffer]); + + } + catch (ex) + { + if (_state != SocketState.Closed)// && !sock.connected) + { + _state = SocketState.Terminated; + close(); + } + + } + + } + + void errorHandler(error, StackTrace trace){ + print(error); + } + + void doneHandler(){ + sock.destroy(); + } + + + AsyncReply connect(String hostname, int port) + { + var rt = new AsyncReply(); + + try + { + _state = SocketState.Connecting; + + WebSocket() + prefix0.WebSocket(url) + IOWebSocketChannel. + Socket.connect(hostname, port).then((s){ + sock = s; + s.listen(dataHandler, + onError: errorHandler, + onDone: doneHandler, + cancelOnError: false); + _state = SocketState.Established; + emitArgs("connect", []); + begin(); + rt.trigger(true); + + }).catchError((ex){ + close(); + rt.triggerError(ex); + }); + + + } + catch(ex) + { + rt.triggerError(ex); + } + + return rt; + } + + + + + IPEndPoint get localEndPoint => _localEP; + IPEndPoint get remoteEndPoint => _remoteEP; + + + SocketState get state => _state; + + + TCPSocket.fromSocket(Socket socket) + { + sock = socket; + //if (socket.) + // _state = SocketState.Established; + } + + TCPSocket() + { + // default constructor + } + + void close() + { + if (state != SocketState.Closed && state != SocketState.Terminated) + _state = SocketState.Closed; + + sock?.close(); + + emitArgs("close", []); + } + + + void send(DC message, [int offset, int size]) + { + if (state == SocketState.Established) + sock.add(message.toList()); + } + + + + void destroy() + { + close(); + emitArgs("destroy", [this]); + } + + AsyncReply accept() + { + + + var reply = new AsyncReply(); + return reply; + } +} \ No newline at end of file diff --git a/lib/src/Resource/Instance.dart b/lib/src/Resource/Instance.dart index 285ec19..8a824f1 100644 --- a/lib/src/Resource/Instance.dart +++ b/lib/src/Resource/Instance.dart @@ -433,6 +433,7 @@ class Instance extends IEventHandler } emitArgs("resourceModified", [_resource, pt.name, value]); + _resource.emitArgs("modified", [pt.name, value]); } /// diff --git a/pubspec.lock b/pubspec.lock index 5cf1ecb..c391e7f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,5 +1,5 @@ # Generated by pub -# See https://www.dartlang.org/tools/pub/glossary#lockfile +# See https://dart.dev/tools/pub/glossary#lockfile packages: analyzer: dependency: transitive diff --git a/pubspec.yaml b/pubspec.yaml index 5d39935..d466584 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,8 +1,8 @@ -name: esiur +name: esyur description: Distributed Object Framework. -version: 1.0.0 +version: 1.0.5 author: Ahmed Zamil -homepage: https://github.com/esiur/esiur-dart +homepage: https://github.com/esyur/esyur-dart environment: sdk: ">=2.1.0 <3.0.0" diff --git a/test/main.dart b/test/main.dart index 9552c98..b732e14 100644 --- a/test/main.dart +++ b/test/main.dart @@ -1,12 +1,17 @@ import "package:test/test.dart"; -import 'package:esiur/esiur.dart'; +import 'package:esyur/esyur.dart'; +import 'dart:io'; main() { test("Connect to server", () async { + // connect to the server var x = await Warehouse.get("iip://localhost:5000/db/my", {"username": "demo", "password": "1234"}); + + // x.instance.store.on("close", ()=>print("Closed")); + // get property print(x.Level); @@ -17,10 +22,54 @@ main() // use named arguments print(await x.Add(value: 20)); // test chunks - x.Stream(10).chunk((c)=>print(c)); + //x.Stream(10).chunk((c)=>print(c)); // property setter - x.Level += 900; + //x.Level += 900; + + + //var msg = await stdin.readLineSync(); + + print("Done"); - print("Done"); }); + + } + + + +// describe object + desc(dynamic x) { + if (x is List) + { + for(var i = 0; i < x.length; i++) + desc(x[i]); + } + else if (x is DistributedResource) + { + var y = x.instance.template; + print("Fucntions = ${y.functions.length}\n"); + for (var i = 0; i < y.functions.length; i++) { + print("name = ${y.functions[i].name}"); + print("args = ${y.functions[i].expansion}"); + } + print("------------------------------\n"); + print("Events = ${y.events.length}\n"); + for (var i = 0; i < y.events.length; i++) { + print("name = ${y.events[i].name}"); + print("args = ${y.events[i].expansion}"); + } + + print("------------------------------\n"); + print("Properties = ${y.properties.length}\n"); + for (var i = 0; i < y.properties.length; i++) { + print("name = ${y.properties[i].name}"); + // recursion + print("value = ${desc(x.get(y.properties[i].index))}"); + } + } + else + { + print(x.toString()); + } +} \ No newline at end of file