Skip to content
Browse files

make remote hashes workmake remote hashes work

  • Loading branch information...
1 parent e2268ec commit b732f248b26f7844df7726f542388ed86d0e011c @cloudshift committed Feb 20, 2012
View
8 cloudshift/Data.hx
@@ -6,6 +6,7 @@ import cloudshift.Core;
import cloudshift.Http;
import cloudshift.Remote;
import cloudshift.data.RemoteBucketProxy;
+import cloudshift.data.RemoteHashProxy;
typedef Serializer = {
var serialize:Dynamic->String;
@@ -71,11 +72,16 @@ class Data {
return p;
}
- public static function serve<T>(http:HttpServer,bucket:Bucket<T>,url:String) {
+ public static function serveBucket<T>(http:HttpServer,bucket:Bucket<T>,url:String) {
var rem = Remote.provider("Store",new RemoteBucketProxy(bucket));
http.handler(new EReg(url,""),rem.httpHandler);
}
+ public static function serveHash<T>(http:HttpServer,hash:BHash<T>,url:String) {
+ var rem = Remote.provider("Hash",new RemoteHashProxy(hash));
+ http.handler(new EReg(url,""),rem.httpHandler);
+ }
+
public static function jsonSerializer():Serializer {
return { serialize:Core.stringify,deSerialize:Core.parse};
}
View
44 cloudshift/data/RemoteHashProxy.hx
@@ -0,0 +1,44 @@
+package cloudshift.data;
+
+import cloudshift.Core;
+import cloudshift.Data;
+
+class RemoteHashProxy {
+
+ var _hash:BHash<Dynamic>;
+
+ public function new(hash:BHash<Dynamic>) {
+ _hash = hash;
+ }
+
+ public function get(key:String,cb:Either<String,Dynamic>->Void) {
+ _hash.get(key)
+ .onError(function(e) { cb(Left(e)); })
+ .deliver(function(v) { cb(Right(v)); });
+ }
+
+ public function set(key:String,val:Dynamic,cb:Either<String,Dynamic>->Void) {
+ _hash.set(key,val)
+ .onError(function(e) { cb(Left(e)); })
+ .deliver(function(v) { cb(Right(v)); });
+ }
+
+ public function remove(key:String,cb:Either<String,String>->Void) {
+ _hash.remove(key)
+ .onError(function(e) { cb(Left(e)); })
+ .deliver(function(v) { cb(Right(v)); });
+ }
+
+ public function keys(?like:String,cb:Either<String,Array<String>>->Void) {
+ _hash.keys(like)
+ .onError(function(e) { cb(Left(e)); })
+ .deliver(function(v) { cb(Right(v)); });
+ }
+
+ public function values(?like:String,cb:Either<String,Array<Dynamic>>->Void) {
+ _hash.values(like)
+ .onError(function(e) { cb(Left(e)); })
+ .deliver(function(v) { cb(Right(v)); });
+ }
+
+}
View
37 cloudshift/data/RemoteSqlite3Client.hx
@@ -10,27 +10,27 @@ import cloudshift.Data;
using cloudshift.Mixin;
private class BucketProxy extends haxe.remoting.AsyncProxy<cloudshift.data.RemoteBucketProxy> { }
+private class HashProxy extends haxe.remoting.AsyncProxy<cloudshift.data.RemoteHashProxy> { }
class RemoteSqlite3Client implements Store {
var _url:String;
var _ready:Outcome<String,Store>;
- var _proxy:BucketProxy;
public function new(ready:Outcome<String,Store>,url:String) {
- _url = url;
+ _url = (url.endsWith("/")) ? url : url + "/" ;
_ready = ready;
ready.resolve(Right(cast this));
}
public function bucket<T>(bucketName:String,?serialize:Serializer):Outcome<String,Bucket<T>> {
var p = Core.outcome();
- new RemoteBucket(p,bucketName,_url);
+ new RemoteBucket(p,bucketName,_url+bucketName);
return p;
}
- public function hash<T>(bucketName:String,?serialize:Serializer):Outcome<String,BHash<T>> {
+ public function hash<T>(hashName:String,?serialize:Serializer):Outcome<String,BHash<T>> {
var prm = Core.outcome();
- new RemoteHash(prm,bucketName,_proxy);
+ new RemoteHash(prm,hashName,_url+hashName);
return prm;
}
@@ -167,35 +167,56 @@ private class RemoteHash<T> implements BHash<T> {
var _ready:Outcome<String,BHash<T>>;
var _name:String;
- var _proxy:BucketProxy;
+ var _proxy:HashProxy;
- public function new(ready:Outcome<String,BHash<T>>,name:String,proxy:BucketProxy) {
+ public function new(ready:Outcome<String,BHash<T>>,name:String,url:String) {
_ready = ready;
_name = name;
- _proxy = proxy;
+
+ var cnx = haxe.remoting.HttpAsyncConnection.urlConnect(url);
+ cnx.setErrorHandler( function(err) Core.error(Std.string(err)) );
+ _proxy = new HashProxy(cnx.Hash);
+ var forTyper:BHash<T> = this;
+ ready.resolve(Right(forTyper));
}
+
public function get(key:String):Outcome<String,T> {
var prm = Core.outcome();
+ _proxy.get(key,function(either) {
+ prm.resolve(either);
+ });
return prm;
}
public function set(key:String,val:T):Outcome<String,T> {
var prm = Core.outcome();
+ _proxy.set(key,val,function(either) {
+ prm.resolve(either);
+ });
return prm;
}
public function remove(key:String):Outcome<String,String> {
var prm = Core.outcome();
+ _proxy.remove(key,function(either) {
+ prm.resolve(either);
+ });
return prm;
}
public function keys(?like:String):Outcome<String,Array<String>> {
var prm = Core.outcome();
+ _proxy.keys(like,function(either) {
+ prm.resolve(either);
+ });
return prm;
}
public function values(?like:String):Outcome<String,Array<T>> {
var prm = Core.outcome();
+ _proxy.keys(like,function(either) {
+ prm.resolve(cast either);
+ });
return prm;
}
View
14 usage/data/RemoteClient.hx
@@ -8,7 +8,7 @@ using cloudshift.Mixin;
class RemoteClient {
public static function main(){
- Data.store(REMOTESQLITE("http://localhost:8082/data/woot")).deliver(function(store) {
+ Data.store(REMOTESQLITE("http://localhost:8082/data")).deliver(function(store) {
store.bucket("woot").deliver(function(woot) {
woot.where('name="lore"').deliver(function(recs) {
@@ -20,8 +20,16 @@ class RemoteClient {
trace("lore's id = "+Data.oid(u));
});
});
- });
+ store.hash("peeps").deliver(function(peeps) {
+ peeps.set("me",{email:"lorena@ritchie.com",name:"lore",passwd:"and why not"})
+ .deliver(function(p) {
+ peeps.get("me").deliver(function(me) {
+ if (me.name == "lore")
+ trace("yep got it");
+ });
+ });
+ });
+ });
}
-
}
View
9 usage/data/RemoteSource.hx
@@ -8,14 +8,17 @@ using cloudshift.Mixin;
class RemoteSource {
public static function main(){
- Http.server().root("www").start({host:"localhost",port:8082}).deliver(function(server) {
+ Http.server().root("www").start({host:"localhost",port:8082}).deliver(function(http) {
Data.store(SQLITE("test.db")).deliver(function(store) {
store.bucket("woot").deliver(function(woot) {
woot.indexer("name",function(o:Dynamic) {
return o.name;
}).deliver(function(el) {
- Data.serve(server,woot,"/data/woot");
- });
+ Data.serveBucket(http,woot,"/data/woot");
+ });
+ });
+ store.hash("peeps").deliver(function(peeps) {
+ Data.serveHash(http,peeps,"/data/peeps");
});
});
});

0 comments on commit b732f24

Please sign in to comment.
Something went wrong with that request. Please try again.