2
0
mirror of https://github.com/esiur/esiur-dart.git synced 2025-05-06 12:02:57 +00:00
This commit is contained in:
Ahmed Zamil 2020-01-04 17:55:09 +03:00
parent 9c15b830dd
commit f6e7a2629c
10 changed files with 123 additions and 35 deletions

View File

@ -26,7 +26,7 @@ class AsyncBag<T> extends AsyncReply<List<T>>
var k = _replies[i]; var k = _replies[i];
var index = i; var index = i;
k.then((r) k.then<dynamic>((r)
{ {
_results[index] = r; _results[index] = r;
_count++; _count++;

View File

@ -59,8 +59,15 @@ class AsyncReply<T> implements Future<T>
{ {
_resultReady = val; _resultReady = val;
} }
AsyncReply<R> then<R>(FutureOr<R> onValue(T value), {Function onError}) AsyncReply<T> next(Function(T) callback)
{
then(callback);
return this;
}
AsyncReply<R> then<R>(FutureOr<R> onValue(T value), {Function onError})
{ {
_callbacks.add(onValue); _callbacks.add(onValue);
if (onError != null) if (onError != null)
@ -83,8 +90,10 @@ class AsyncReply<T> implements Future<T>
if (_resultReady) if (_resultReady)
onValue(result); onValue(result);
return this as AsyncReply<R>; if (R == Null)
return null;
else
return this as AsyncReply<R>;
} }
AsyncReply<T> whenComplete(FutureOr action()) AsyncReply<T> whenComplete(FutureOr action())

View File

@ -175,11 +175,13 @@ class DistributedConnection extends NetworkConnection with IStore
if (instance.attributes.containsKey("username") if (instance.attributes.containsKey("username")
&& instance.attributes.containsKey("password")) && 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 // assign domain from hostname if not provided
var address = hostname.split(":")[0]; var address = host[0];
var port = int.parse(hostname.split(":")[1]); var port = int.parse(host[1]);
var username = instance.attributes["username"].toString(); var username = instance.attributes["username"].toString();
var domain = instance.attributes.containsKey("domain") ? instance.attributes["domain"] : address; var domain = instance.attributes.containsKey("domain") ? instance.attributes["domain"] : address;
@ -195,7 +197,7 @@ class DistributedConnection extends NetworkConnection with IStore
var sock = new TCPSocket(); var sock = new TCPSocket();
sock.connect(domain, port).then((x){ sock.connect(domain, port).then<dynamic>((x){
assign(sock); assign(sock);
//rt.trigger(true); //rt.trigger(true);
}).error((x)=>_openReply.triggerError(x)); }).error((x)=>_openReply.triggerError(x));
@ -1987,7 +1989,7 @@ class DistributedConnection extends NetworkConnection with IStore
if (r is DistributedResource) if (r is DistributedResource)
{ {
// propagation // propagation
(r as DistributedResource).set(index, value).then((x) (r as DistributedResource).set(index, value).then<dynamic>((x)
{ {
sendReply(IIPPacketAction.SetProperty, callback).done(); sendReply(IIPPacketAction.SetProperty, callback).done();
}).error((x) }).error((x)
@ -2087,7 +2089,7 @@ class DistributedConnection extends NetworkConnection with IStore
sendRequest(IIPPacketAction.TemplateFromClassId) sendRequest(IIPPacketAction.TemplateFromClassId)
.addGuid(classId) .addGuid(classId)
.done() .done()
.then((rt) .then<dynamic>((rt)
{ {
_templateRequests.remove(classId); _templateRequests.remove(classId);
_templates[(rt[0] as ResourceTemplate).classId] = rt[0] as ResourceTemplate; _templates[(rt[0] as ResourceTemplate).classId] = rt[0] as ResourceTemplate;
@ -2112,7 +2114,7 @@ class DistributedConnection extends NetworkConnection with IStore
var rt = new AsyncReply<IResource>(); var rt = new AsyncReply<IResource>();
query(path).then((ar) query(path).then<dynamic>((ar)
{ {
if (ar?.length > 0) if (ar?.length > 0)
rt.trigger(ar[0]); rt.trigger(ar[0]);
@ -2167,7 +2169,7 @@ class DistributedConnection extends NetworkConnection with IStore
sendRequest(IIPPacketAction.AttachResource) sendRequest(IIPPacketAction.AttachResource)
.addUint32(id) .addUint32(id)
.done() .done()
.then((rt) .then<dynamic>((rt)
{ {
//print("fetched ${id}"); //print("fetched ${id}");
@ -2175,7 +2177,7 @@ class DistributedConnection extends NetworkConnection with IStore
var dr = new DistributedResource(this, id, rt[1], rt[2]); var dr = new DistributedResource(this, id, rt[1], rt[2]);
getTemplate(rt[0] as Guid).then((tmp) getTemplate(rt[0] as Guid).then<dynamic>((tmp)
{ {
//print("New template "); //print("New template ");
@ -2211,7 +2213,7 @@ class DistributedConnection extends NetworkConnection with IStore
sendRequest(IIPPacketAction.ResourceChildren) sendRequest(IIPPacketAction.ResourceChildren)
.addUint32(resource.instance.id) .addUint32(resource.instance.id)
.done() .done()
.then((ar) .then<dynamic>((ar)
{ {
var d = ar[0] as DC; var d = ar[0] as DC;
Codec.parseResourceArray(d, 0, d.length, this).then((resources) Codec.parseResourceArray(d, 0, d.length, this).then((resources)
@ -2230,10 +2232,10 @@ class DistributedConnection extends NetworkConnection with IStore
sendRequest(IIPPacketAction.ResourceParents) sendRequest(IIPPacketAction.ResourceParents)
.addUint32(resource.instance.id) .addUint32(resource.instance.id)
.done() .done()
.then((ar) .then<dynamic>((ar)
{ {
var d = ar[0] as DC; var d = ar[0] as DC;
Codec.parseResourceArray(d, 0, d.length, this).then((resources) Codec.parseResourceArray(d, 0, d.length, this).then<dynamic>((resources)
{ {
rt.trigger(resources); rt.trigger(resources);
}).error((ex) => rt.triggerError(ex)); }).error((ex) => rt.triggerError(ex));
@ -2250,7 +2252,7 @@ class DistributedConnection extends NetworkConnection with IStore
sendRequest(IIPPacketAction.ClearAllAttributes) sendRequest(IIPPacketAction.ClearAllAttributes)
.addUint32(resource.instance.id) .addUint32(resource.instance.id)
.done() .done()
.then((ar) => rt.trigger(true)) .then<dynamic>((ar) => rt.trigger(true))
.error((ex) => rt.triggerError(ex)); .error((ex) => rt.triggerError(ex));
else else
{ {
@ -2260,7 +2262,7 @@ class DistributedConnection extends NetworkConnection with IStore
.addInt32(attrs.length) .addInt32(attrs.length)
.addDC(attrs) .addDC(attrs)
.done() .done()
.then((ar) => rt.trigger(true)) .then<dynamic>((ar) => rt.trigger(true))
.error((ex) => rt.triggerError(ex)); .error((ex) => rt.triggerError(ex));
} }
@ -2275,7 +2277,7 @@ class DistributedConnection extends NetworkConnection with IStore
.addUint32(resource.instance.id) .addUint32(resource.instance.id)
.addDC(Codec.composeStructure(attributes, this, true, true, true)) .addDC(Codec.composeStructure(attributes, this, true, true, true))
.done() .done()
.then((ar) => rt.trigger(true)) .then<dynamic>((ar) => rt.trigger(true))
.error((ex) => rt.triggerError(ex)); .error((ex) => rt.triggerError(ex));
return rt; return rt;
@ -2293,7 +2295,7 @@ class DistributedConnection extends NetworkConnection with IStore
.then((ar) .then((ar)
{ {
var d = ar[0] as DC; var d = ar[0] as DC;
Codec.parseStructure(d, 0, d.length, this).then((st) Codec.parseStructure(d, 0, d.length, this).then<dynamic>((st)
{ {
resource.instance.setAttributes(st); resource.instance.setAttributes(st);
rt.trigger(st); rt.trigger(st);
@ -2311,7 +2313,7 @@ class DistributedConnection extends NetworkConnection with IStore
.then((ar) .then((ar)
{ {
var d = ar[0] as DC; var d = ar[0] as DC;
Codec.parseStructure(d, 0, d.length, this).then((st) Codec.parseStructure(d, 0, d.length, this).then<dynamic>((st)
{ {
resource.instance.setAttributes(st); resource.instance.setAttributes(st);
@ -2347,7 +2349,7 @@ class DistributedConnection extends NetworkConnection with IStore
.addDateTime(fromDate) .addDateTime(fromDate)
.addDateTime(toDate) .addDateTime(toDate)
.done() .done()
.then((rt) .then<dynamic>((rt)
{ {
var content = rt[0] as DC; var content = rt[0] as DC;
@ -2376,7 +2378,7 @@ class DistributedConnection extends NetworkConnection with IStore
.addUint16(str.length) .addUint16(str.length)
.addDC(str) .addDC(str)
.done() .done()
.then((args) .then<dynamic>((args)
{ {
var content = args[0] as DC; var content = args[0] as DC;

View File

@ -153,9 +153,10 @@ class NetworkConnection extends IDestructible
void close() void close()
{ {
try try
{ {
if (_sock != null) if (_sock != null)
_sock.close(); _sock.close();
} }

View File

@ -102,6 +102,7 @@ class TCPSocket extends ISocket
} }
void doneHandler(){ void doneHandler(){
close();
sock.destroy(); sock.destroy();
} }

View File

@ -22,6 +22,8 @@ SOFTWARE.
*/ */
/*
import 'dart:io'; import 'dart:io';
import 'ISocket.dart'; import 'ISocket.dart';
import '../../Data/DC.dart'; import '../../Data/DC.dart';
@ -186,4 +188,5 @@ class WSSocket extends ISocket
var reply = new AsyncReply<ISocket>(); var reply = new AsyncReply<ISocket>();
return reply; return reply;
} }
} }
*/

View File

@ -46,7 +46,6 @@ class Warehouse
static KeyList<Guid, ResourceTemplate> _templates = new KeyList<Guid, ResourceTemplate>(); static KeyList<Guid, ResourceTemplate> _templates = new KeyList<Guid, ResourceTemplate>();
static bool storeIsOpen = false;
//public delegate void StoreConnectedEvent(IStore store, string name); //public delegate void StoreConnectedEvent(IStore store, string name);
//public delegate void StoreDisconnectedEvent(IStore store); //public delegate void StoreDisconnectedEvent(IStore store);
@ -54,8 +53,12 @@ class Warehouse
//public static event StoreConnectedEvent StoreConnected; //public static event StoreConnectedEvent StoreConnected;
///public static event StoreDisconnectedEvent StoreDisconnected; ///public static event StoreDisconnectedEvent StoreDisconnected;
static bool _warehouseIsOpen = false;
static KeyList<String, IStore Function()> protocols = _getSupportedProtocols(); static KeyList<String, IStore Function()> protocols = _getSupportedProtocols();
static final _urlRegex = RegExp(r'^(?:([^\s|:]*):\/\/([^\/]*)\/?(.*))');
/// <summary> /// <summary>
/// Get a store by its name. /// Get a store by its name.
/// </summary> /// </summary>
@ -125,7 +128,7 @@ class Warehouse
} }
rt.trigger(true); rt.trigger(true);
storeIsOpen = true; _warehouseIsOpen = true;
}); });
}); });
@ -240,7 +243,65 @@ class Warehouse
/// <returns>Resource instance.</returns> /// <returns>Resource instance.</returns>
static AsyncReply<dynamic> get(String path, [attributes = null, IResource parent = null, IPermissionsManager manager = null]) static AsyncReply<dynamic> get(String path, [attributes = null, IResource parent = null, IPermissionsManager manager = null])
{ {
var rt = new AsyncReply<IResource>();
// 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<dynamic>((x)
{
_warehouseIsOpen = true;
if (url[3].length > 0 && url[3] != "")
store.get(url[3]).then<dynamic>((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('/'); var p = path.split('/');
IResource res; IResource res;
@ -286,9 +347,9 @@ class Warehouse
put(store, url[0] + "://" + hostname, null, parent, null, 0, manager, attributes); put(store, url[0] + "://" + hostname, null, parent, null, 0, manager, attributes);
store.trigger(ResourceTrigger.Open).then((x){ store.trigger(ResourceTrigger.Open).then<dynamic>((x){
if (pathname.length > 0 && pathname != "") if (pathname.length > 0 && pathname != "")
store.get(pathname).then((r) => rt.trigger(r) store.get(pathname).then<dynamic>((r) => rt.trigger(r)
).error((e) => ).error((e) =>
rt.triggerError(e) rt.triggerError(e)
@ -307,6 +368,7 @@ class Warehouse
return new AsyncReply<IResource>.ready(null); return new AsyncReply<IResource>.ready(null);
*/
} }
/// <summary> /// <summary>
@ -348,8 +410,8 @@ class Warehouse
_resources[resource.instance.id] = resource; _resources[resource.instance.id] = resource;
//if (!storeIsOpen) if (_warehouseIsOpen)
// resource.trigger(ResourceTrigger.Initialize); resource.trigger(ResourceTrigger.Initialize);
} }

View File

@ -1,6 +1,6 @@
name: esyur name: esyur
description: Distributed Object Framework. description: Distributed Object Framework.
version: 1.0.5 version: 1.1.2
author: Ahmed Zamil <ahmed@dijlh.com> author: Ahmed Zamil <ahmed@dijlh.com>
homepage: https://github.com/esyur/esyur-dart homepage: https://github.com/esyur/esyur-dart

View File

@ -6,12 +6,22 @@ main()
{ {
test("Connect to server", () async { test("Connect to server", () async {
// connect to the server // 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 // get property
print(x.Level); print(x.Level);