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 2019-12-02 03:29:30 +03:00
parent 5886cbbdac
commit 9c15b830dd
16 changed files with 298 additions and 30 deletions

View File

@ -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.

View File

@ -11,6 +11,8 @@ class AsyncBag<T> extends AsyncReply<List<T>>
seal()
{
//print("SEALED");
if (_sealedBag)
return;
@ -28,8 +30,11 @@ class AsyncBag<T> extends AsyncReply<List<T>>
{
_results[index] = r;
_count++;
//print("Seal ${_count}/${_results.length}");
if (_count == _results.length)
trigger(_results);
}).error((ex){
triggerError(ex);
});
}
}

View File

@ -1,4 +1,4 @@
library esiur;
library esyur;
import 'AsyncReply.dart';

View File

@ -108,17 +108,6 @@ class AsyncReply<T> implements Future<T>
return this;
}
AsyncReply<T> _then_old(Function(T) callback)
{
_callbacks.add(callback);
if (_resultReady)
callback(result);
return this;
}
AsyncReply<T> error(Function(dynamic) callback)
{
_errorCallbacks.add(callback);

View File

@ -1,4 +1,4 @@
// library esiur;
// library esyur;
import 'IEventHandler.dart';

View File

@ -493,10 +493,10 @@ class Codec
return new AsyncReply<int>.ready(data.getUint64(offset));
case DataType.Float32:
return new AsyncReply<int>.ready(data.getFloat32(offset));
return new AsyncReply<double>.ready(data.getFloat32(offset));
case DataType.Float64:
return new AsyncReply<int>.ready(data.getFloat64(offset));
return new AsyncReply<double>.ready(data.getFloat64(offset));
case DataType.String:
return new AsyncReply<String>.ready(data.getString(offset, contentLength));
@ -649,6 +649,8 @@ class Codec
/// <returns>Array of resources.</returns>
static AsyncBag<IResource> parseResourceArray(DC data, int offset, int length, DistributedConnection connection)
{
//print("parseResourceArray ${offset} ${length}");
var reply = new AsyncBag<IResource>();
if (length == 0)
{
@ -912,6 +914,9 @@ class Codec
/// <returns></returns>
static AsyncBag<PropertyValue> parsePropertyValueArray(DC data, int offset, int length, DistributedConnection connection)//, bool ageIncluded = true)
{
//print("parsePropertyValueArray ${offset} ${length}");
var rt = new AsyncBag<PropertyValue>();
var sizeObject = new SizeObject();

View File

@ -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<DistributedResource>();
_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);

View File

@ -80,7 +80,7 @@ class DistributedResource extends IResource
/// <summary>
/// Resource is attached when all its properties are received.
/// </summary>
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)
{

View File

@ -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()

View File

@ -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;
}

View File

@ -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]);

View File

@ -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<int> 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<bool> connect(String hostname, int port)
{
var rt = new AsyncReply<bool>();
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<ISocket> accept()
{
var reply = new AsyncReply<ISocket>();
return reply;
}
}

View File

@ -433,6 +433,7 @@ class Instance extends IEventHandler
}
emitArgs("resourceModified", [_resource, pt.name, value]);
_resource.emitArgs("modified", [pt.name, value]);
}
/// <summary>

View File

@ -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

View File

@ -1,8 +1,8 @@
name: esiur
name: esyur
description: Distributed Object Framework.
version: 1.0.0
version: 1.0.5
author: Ahmed Zamil <ahmed@dijlh.com>
homepage: https://github.com/esiur/esiur-dart
homepage: https://github.com/esyur/esyur-dart
environment:
sdk: ">=2.1.0 <3.0.0"

View File

@ -1,13 +1,18 @@
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);
// listen to event
@ -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());
}
}