diff --git a/lib/esiur.dart b/lib/esyur.dart similarity index 100% rename from lib/esiur.dart rename to lib/esyur.dart diff --git a/lib/src/Core/AsyncBag.dart b/lib/src/Core/AsyncBag.dart index ab3a509..fe76dab 100644 --- a/lib/src/Core/AsyncBag.dart +++ b/lib/src/Core/AsyncBag.dart @@ -26,7 +26,7 @@ class AsyncBag extends AsyncReply> var k = _replies[i]; var index = i; - k.then((r) + k.then((r) { _results[index] = r; _count++; diff --git a/lib/src/Core/AsyncReply.dart b/lib/src/Core/AsyncReply.dart index 28396b9..33be361 100644 --- a/lib/src/Core/AsyncReply.dart +++ b/lib/src/Core/AsyncReply.dart @@ -59,8 +59,15 @@ class AsyncReply implements Future { _resultReady = val; } + - AsyncReply then(FutureOr onValue(T value), {Function onError}) + AsyncReply next(Function(T) callback) + { + then(callback); + return this; + } + + AsyncReply then(FutureOr onValue(T value), {Function onError}) { _callbacks.add(onValue); if (onError != null) @@ -83,8 +90,10 @@ class AsyncReply implements Future if (_resultReady) onValue(result); - return this as AsyncReply; - + if (R == Null) + return null; + else + return this as AsyncReply; } AsyncReply whenComplete(FutureOr action()) diff --git a/lib/src/Net/IIP/DistributedConnection.dart b/lib/src/Net/IIP/DistributedConnection.dart index cb19862..4b1266f 100644 --- a/lib/src/Net/IIP/DistributedConnection.dart +++ b/lib/src/Net/IIP/DistributedConnection.dart @@ -175,11 +175,13 @@ class DistributedConnection extends NetworkConnection with IStore if (instance.attributes.containsKey("username") && instance.attributes.containsKey("password")) { - var hostname = instance.name.split ("://").skip(1).join("://").split("/")[0]; + + + var host = instance.name.split(":");// ("://").skip(1).join("://").split("/")[0]; // assign domain from hostname if not provided - var address = hostname.split(":")[0]; - var port = int.parse(hostname.split(":")[1]); + var address = host[0]; + var port = int.parse(host[1]); var username = instance.attributes["username"].toString(); var domain = instance.attributes.containsKey("domain") ? instance.attributes["domain"] : address; @@ -195,7 +197,7 @@ class DistributedConnection extends NetworkConnection with IStore var sock = new TCPSocket(); - sock.connect(domain, port).then((x){ + sock.connect(domain, port).then((x){ assign(sock); //rt.trigger(true); }).error((x)=>_openReply.triggerError(x)); @@ -1987,7 +1989,7 @@ class DistributedConnection extends NetworkConnection with IStore if (r is DistributedResource) { // propagation - (r as DistributedResource).set(index, value).then((x) + (r as DistributedResource).set(index, value).then((x) { sendReply(IIPPacketAction.SetProperty, callback).done(); }).error((x) @@ -2087,7 +2089,7 @@ class DistributedConnection extends NetworkConnection with IStore sendRequest(IIPPacketAction.TemplateFromClassId) .addGuid(classId) .done() - .then((rt) + .then((rt) { _templateRequests.remove(classId); _templates[(rt[0] as ResourceTemplate).classId] = rt[0] as ResourceTemplate; @@ -2112,7 +2114,7 @@ class DistributedConnection extends NetworkConnection with IStore var rt = new AsyncReply(); - query(path).then((ar) + query(path).then((ar) { if (ar?.length > 0) rt.trigger(ar[0]); @@ -2167,7 +2169,7 @@ class DistributedConnection extends NetworkConnection with IStore sendRequest(IIPPacketAction.AttachResource) .addUint32(id) .done() - .then((rt) + .then((rt) { //print("fetched ${id}"); @@ -2175,7 +2177,7 @@ class DistributedConnection extends NetworkConnection with IStore var dr = new DistributedResource(this, id, rt[1], rt[2]); - getTemplate(rt[0] as Guid).then((tmp) + getTemplate(rt[0] as Guid).then((tmp) { //print("New template "); @@ -2211,7 +2213,7 @@ class DistributedConnection extends NetworkConnection with IStore sendRequest(IIPPacketAction.ResourceChildren) .addUint32(resource.instance.id) .done() - .then((ar) + .then((ar) { var d = ar[0] as DC; Codec.parseResourceArray(d, 0, d.length, this).then((resources) @@ -2230,10 +2232,10 @@ class DistributedConnection extends NetworkConnection with IStore sendRequest(IIPPacketAction.ResourceParents) .addUint32(resource.instance.id) .done() - .then((ar) + .then((ar) { var d = ar[0] as DC; - Codec.parseResourceArray(d, 0, d.length, this).then((resources) + Codec.parseResourceArray(d, 0, d.length, this).then((resources) { rt.trigger(resources); }).error((ex) => rt.triggerError(ex)); @@ -2250,7 +2252,7 @@ class DistributedConnection extends NetworkConnection with IStore sendRequest(IIPPacketAction.ClearAllAttributes) .addUint32(resource.instance.id) .done() - .then((ar) => rt.trigger(true)) + .then((ar) => rt.trigger(true)) .error((ex) => rt.triggerError(ex)); else { @@ -2260,7 +2262,7 @@ class DistributedConnection extends NetworkConnection with IStore .addInt32(attrs.length) .addDC(attrs) .done() - .then((ar) => rt.trigger(true)) + .then((ar) => rt.trigger(true)) .error((ex) => rt.triggerError(ex)); } @@ -2275,7 +2277,7 @@ class DistributedConnection extends NetworkConnection with IStore .addUint32(resource.instance.id) .addDC(Codec.composeStructure(attributes, this, true, true, true)) .done() - .then((ar) => rt.trigger(true)) + .then((ar) => rt.trigger(true)) .error((ex) => rt.triggerError(ex)); return rt; @@ -2293,7 +2295,7 @@ class DistributedConnection extends NetworkConnection with IStore .then((ar) { var d = ar[0] as DC; - Codec.parseStructure(d, 0, d.length, this).then((st) + Codec.parseStructure(d, 0, d.length, this).then((st) { resource.instance.setAttributes(st); rt.trigger(st); @@ -2311,7 +2313,7 @@ class DistributedConnection extends NetworkConnection with IStore .then((ar) { var d = ar[0] as DC; - Codec.parseStructure(d, 0, d.length, this).then((st) + Codec.parseStructure(d, 0, d.length, this).then((st) { resource.instance.setAttributes(st); @@ -2347,7 +2349,7 @@ class DistributedConnection extends NetworkConnection with IStore .addDateTime(fromDate) .addDateTime(toDate) .done() - .then((rt) + .then((rt) { var content = rt[0] as DC; @@ -2376,7 +2378,7 @@ class DistributedConnection extends NetworkConnection with IStore .addUint16(str.length) .addDC(str) .done() - .then((args) + .then((args) { var content = args[0] as DC; diff --git a/lib/src/Net/NetworkConnection.dart b/lib/src/Net/NetworkConnection.dart index c73a317..224b72c 100644 --- a/lib/src/Net/NetworkConnection.dart +++ b/lib/src/Net/NetworkConnection.dart @@ -153,9 +153,10 @@ class NetworkConnection extends IDestructible void close() { - + try { + if (_sock != null) _sock.close(); } diff --git a/lib/src/Net/Sockets/TCPSocket.dart b/lib/src/Net/Sockets/TCPSocket.dart index f8e53fa..1042320 100644 --- a/lib/src/Net/Sockets/TCPSocket.dart +++ b/lib/src/Net/Sockets/TCPSocket.dart @@ -102,6 +102,7 @@ class TCPSocket extends ISocket } void doneHandler(){ + close(); sock.destroy(); } diff --git a/lib/src/Net/Sockets/Websocket.dart b/lib/src/Net/Sockets/Websocket.dart index 51ed614..4a3f58a 100644 --- a/lib/src/Net/Sockets/Websocket.dart +++ b/lib/src/Net/Sockets/Websocket.dart @@ -22,6 +22,8 @@ SOFTWARE. */ +/* + import 'dart:io'; import 'ISocket.dart'; import '../../Data/DC.dart'; @@ -186,4 +188,5 @@ class WSSocket extends ISocket var reply = new AsyncReply(); return reply; } -} \ No newline at end of file +} +*/ \ No newline at end of file diff --git a/lib/src/Resource/Warehouse.dart b/lib/src/Resource/Warehouse.dart index 67a0aa7..3ba2f0c 100644 --- a/lib/src/Resource/Warehouse.dart +++ b/lib/src/Resource/Warehouse.dart @@ -46,7 +46,6 @@ class Warehouse static KeyList _templates = new KeyList(); - static bool storeIsOpen = false; //public delegate void StoreConnectedEvent(IStore store, string name); //public delegate void StoreDisconnectedEvent(IStore store); @@ -54,8 +53,12 @@ class Warehouse //public static event StoreConnectedEvent StoreConnected; ///public static event StoreDisconnectedEvent StoreDisconnected; + static bool _warehouseIsOpen = false; + static KeyList protocols = _getSupportedProtocols(); + static final _urlRegex = RegExp(r'^(?:([^\s|:]*):\/\/([^\/]*)\/?(.*))'); + /// /// Get a store by its name. /// @@ -125,7 +128,7 @@ class Warehouse } rt.trigger(true); - storeIsOpen = true; + _warehouseIsOpen = true; }); }); @@ -240,7 +243,65 @@ class Warehouse /// Resource instance. static AsyncReply get(String path, [attributes = null, IResource parent = null, IPermissionsManager manager = null]) { + var rt = new AsyncReply(); + // Should we create a new store ? + + if (_urlRegex.hasMatch(path)) + //if (path.contains("://")) + { + var url = _urlRegex.allMatches(path).first; + + //var url = path.split(_urlRegex); + + //var url = path.split(new string[] { "://" }, 2, StringSplitOptions.None); + //var hostname = url[1].Split(new char[] { '/' }, 2)[0]; + //var pathname = string.Join("/", url[1].Split(new char[] { '/' }).Skip(1)); + + + if (protocols.containsKey(url[1])) + { + var handler = protocols[url[1]]; + + var store = handler(); + put(store, url[2], null, parent, null, 0, manager, attributes); + + + store.trigger(ResourceTrigger.Open).then((x) + { + + _warehouseIsOpen = true; + + if (url[3].length > 0 && url[3] != "") + store.get(url[3]).then((r) + { + rt.trigger(r); + }).error((e) => rt.triggerError(e)); + else + rt.trigger(store); + }).error((e) + { + rt.triggerError(e); + Warehouse.remove(store); + }); + + return rt; + } + } + + + query(path).then((rs) + { + if (rs != null && rs.length > 0) + rt.trigger(rs[0]); + else + rt.trigger(null); + }); + + return rt; + + +/* var p = path.split('/'); IResource res; @@ -286,9 +347,9 @@ class Warehouse put(store, url[0] + "://" + hostname, null, parent, null, 0, manager, attributes); - store.trigger(ResourceTrigger.Open).then((x){ + store.trigger(ResourceTrigger.Open).then((x){ if (pathname.length > 0 && pathname != "") - store.get(pathname).then((r) => rt.trigger(r) + store.get(pathname).then((r) => rt.trigger(r) ).error((e) => rt.triggerError(e) @@ -307,6 +368,7 @@ class Warehouse return new AsyncReply.ready(null); + */ } /// @@ -348,8 +410,8 @@ class Warehouse _resources[resource.instance.id] = resource; - //if (!storeIsOpen) - // resource.trigger(ResourceTrigger.Initialize); + if (_warehouseIsOpen) + resource.trigger(ResourceTrigger.Initialize); } diff --git a/pubspec.yaml b/pubspec.yaml index d466584..18abf7a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: esyur description: Distributed Object Framework. -version: 1.0.5 +version: 1.1.2 author: Ahmed Zamil homepage: https://github.com/esyur/esyur-dart diff --git a/test/main.dart b/test/main.dart index b732e14..c38eb74 100644 --- a/test/main.dart +++ b/test/main.dart @@ -6,12 +6,22 @@ main() { test("Connect to server", () async { - // connect to the server - var x = await Warehouse.get("iip://localhost:5000/db/my", {"username": "demo", "password": "1234"}); + var x = await Warehouse.get("iip://192.168.88.220:5000/sys/su", {"username": "admin", "password": "1234" + , "domain": "khalid.com"}); - // x.instance.store.on("close", ()=>print("Closed")); + x.instance.store.on("close", (x){ + print("Closed"); + }); + x.on("modified", (peoperty, value){ + + }); + + // var users = await x.Users.Slice(0, 10); + + // print(users); + // await sleep(Duration(seconds: 10)); // get property print(x.Level);