diff --git a/java/core/src/core/callback/Callback.java b/java/core/src/core/callback/Callback.java new file mode 100644 index 0000000..f7efdcd --- /dev/null +++ b/java/core/src/core/callback/Callback.java @@ -0,0 +1,44 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callback; + + +public abstract class Callback implements CallbackInterface +{ + protected Callback callback; + + public Callback () + { + + } + + public void next (Object...arguments) + { + if (callback!=null) + callback.invoke(arguments); + } + + public void call (Callback callback, Object...arguments) + { + if (callback != null) + { + callback.setReturn(this.callback); + callback.invoke(arguments); + } + } + + public CallbackChain addCallback (Callback callback) + { + CallbackChain chain = new CallbackChain(); + return chain.addCallback(this).addCallback(callback); + } + + public Callback setReturn (Callback callback) + { + this.callback = callback; + return this; + } +} diff --git a/java/core/src/core/callback/CallbackChain.java b/java/core/src/core/callback/CallbackChain.java new file mode 100644 index 0000000..c81b25a --- /dev/null +++ b/java/core/src/core/callback/CallbackChain.java @@ -0,0 +1,133 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callback; + +import java.util.ArrayList; + +import core.util.LogNull; +import core.util.LogOut; + +public class CallbackChain extends Callback +{ + static LogNull log = new LogNull(CallbackChain.class); + Callback head, tail, execute, next; + + boolean fastFail = true; + boolean propagateOriginalArguments = false; + + Object[] originalArguments; + + int index = -1; + + public CallbackChain() + { + + } + + @Override + public CallbackChain addCallback (Callback callback) + { + log.debug(this,"adding callback",callback); + if (tail != null) + tail.callback = callback; + + if (head == null) + head = callback; + + if (next == null) + next = head; + + tail = callback; + + return this; + } + + public CallbackChain setPropagateOriginalArguments () + { + propagateOriginalArguments = true; + return this; + } + + public CallbackChain setSlowFail () + { + fastFail = false; + return this; + } + + @Override + public void invoke(Object... arguments) + { + log.debug("invoke"); + + if (next == head) + originalArguments = arguments; + + // reset the modifications we made during traversal + if (execute != null) + execute.callback = next; + + if (tail != null && tail.callback != null) + log.error(this,"tail callback is not null!",execute,tail); + + boolean hasException = + arguments != null && + arguments.length > 0 && + arguments[0] instanceof Exception; + + if (hasException) + { + log.debug(this,"has exception"); + log.exception((Exception)arguments[0]); + } + + // if we should skip to the end, and we haven't already skipped to the end + if (hasException && fastFail && execute != tail) + { + log.debug(this,"advancing to tail",tail); + next = tail; + } + + if (propagateOriginalArguments) + { + log.debug(this,"using original arguments"); + arguments = originalArguments; + } + + execute = next; + if (execute != null) + { + next = execute.callback; + execute.callback = this; + + log.debug(this, "execute", execute, next); + execute.invoke(arguments); + } + else + { + log.debug(this,"executing self callback"); + + // reset + next = head; + originalArguments = null; + next(arguments); + } + } + + public Callback[] toArray () + { + ArrayList array = new ArrayList(); + + Callback i = head; + while (i!=null) + { + array.add(i); + i = i.callback; + } + + return array.toArray(new Callback[0]); + } + +} diff --git a/java/core/src/core/callback/CallbackDefault.java b/java/core/src/core/callback/CallbackDefault.java new file mode 100644 index 0000000..f65dea5 --- /dev/null +++ b/java/core/src/core/callback/CallbackDefault.java @@ -0,0 +1,62 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callback; + +import core.util.LogNull; + +public abstract class CallbackDefault extends CallbackWithVariables +{ + static LogNull log = new LogNull(CallbackDefault.class); + + int shouldFireNext=0; + + public CallbackDefault(Object...args) + { + super(args); + } + + @Override + public void invoke(Object... arguments) + { + shouldFireNext++; + + try + { + if (arguments!=null && arguments.length==1 && arguments[0] instanceof Exception) + throw (Exception)arguments[0]; + + onSuccess(arguments); + } + catch (Exception e) + { + onFailure(e); + } + + if (shouldFireNext!=0) + log.debug("**************************** NEXT NEVER CALLED, IS THIS CORRECT? *************************"); + } + + public abstract void onSuccess (Object... arguments) throws Exception; + + public void next(Object...arguments) + { + shouldFireNext--; + super.next(arguments); + } + + public void call(Callback callback, Object...arguments) + { + shouldFireNext--; + super.call(callback, arguments); + } + + public void onFailure (Exception e) + { + log.error("propagating", e); + log.exception(e); + next(e); + } +} diff --git a/java/core/src/core/callback/CallbackEmpty.java b/java/core/src/core/callback/CallbackEmpty.java new file mode 100644 index 0000000..aada614 --- /dev/null +++ b/java/core/src/core/callback/CallbackEmpty.java @@ -0,0 +1,16 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callback; + + +public class CallbackEmpty extends CallbackDefault +{ + @Override + public void onSuccess(Object... arguments) throws Exception { + next(arguments); + } + +} diff --git a/java/core/src/core/callback/CallbackInterface.java b/java/core/src/core/callback/CallbackInterface.java new file mode 100644 index 0000000..b5dcdc8 --- /dev/null +++ b/java/core/src/core/callback/CallbackInterface.java @@ -0,0 +1,15 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callback; + +import org.timepedia.exporter.client.Export; +import org.timepedia.exporter.client.Exportable; + +@Export +public interface CallbackInterface extends Exportable +{ + public abstract void invoke (Object ... arguments); +} diff --git a/java/core/src/core/callback/CallbackSync.java b/java/core/src/core/callback/CallbackSync.java new file mode 100644 index 0000000..e42d330 --- /dev/null +++ b/java/core/src/core/callback/CallbackSync.java @@ -0,0 +1,65 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callback; + + +public class CallbackSync +{ + CallbackChain chain; + Object[] results; + + public CallbackSync(Callback callback) + { + chain = callback.addCallback(setResults_()); + } + + public CallbackSync invoke (Object...args) + { + chain.invoke(args); + return this; + } + + public Callback setResults_() + { + return new Callback() { + public void invoke(Object... arguments) { + results = arguments; + } + }; + } + + public T exportNoException () + { + try + { + return export(); + } + catch (RuntimeException e) + { + throw e; + } + catch (Exception e) + { + throw new RuntimeException(e); + } + } + + public T export() throws Exception + { + if (results != null && results.length > 0) + { + if (results[0] instanceof Exception) + { + Exception e = (Exception)results[0]; + throw e; + } + + return (T)results[0]; + } + + return null; + } +} diff --git a/java/core/src/core/callback/CallbackWithVariables.java b/java/core/src/core/callback/CallbackWithVariables.java new file mode 100644 index 0000000..a4e0c1d --- /dev/null +++ b/java/core/src/core/callback/CallbackWithVariables.java @@ -0,0 +1,28 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callback; + + +public abstract class CallbackWithVariables extends Callback +{ + protected Object[] v; + + public CallbackWithVariables() + { + } + + public CallbackWithVariables(Object...v) + { + this.v = v; + } + + + @SuppressWarnings("unchecked") + public T V(int i) + { + return (T)v[i]; + } +} diff --git a/java/core/src/core/callbacks/CountDown.java b/java/core/src/core/callbacks/CountDown.java new file mode 100644 index 0000000..57cb7a6 --- /dev/null +++ b/java/core/src/core/callbacks/CountDown.java @@ -0,0 +1,36 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callbacks; + +import core.callback.Callback; +import core.callback.CallbackDefault; +import core.util.LogNull; +import core.util.LogOut; + +public class CountDown extends CallbackDefault +{ + static LogNull log = new LogNull(CountDown.class); + + int count; + Callback onFinished; + + public CountDown (int from, Callback onFinished) + { + this.count = from; + this.onFinished = onFinished; + } + + @Override + public void onSuccess(Object... arguments) throws Exception + { + log.debug("Countdown: ", count); + + if (--count == 0) + onFinished.invoke(arguments); + + next(); + } +} diff --git a/java/core/src/core/callbacks/IoOnReceive.java b/java/core/src/core/callbacks/IoOnReceive.java new file mode 100644 index 0000000..440bc73 --- /dev/null +++ b/java/core/src/core/callbacks/IoOnReceive.java @@ -0,0 +1,27 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callbacks; + +import core.callback.Callback; +import core.callback.CallbackDefault; +import core.io.IoChain; + +public class IoOnReceive extends CallbackDefault +{ + public IoOnReceive(IoChain chain) + { + super(chain); + } + + public void onSuccess (Object... args) throws Exception + { + IoChain chain = V(0); + if (chain != null) + chain.receive((byte[])args[0]); + + next(args); + } +} diff --git a/java/core/src/core/callbacks/IoOpen.java b/java/core/src/core/callbacks/IoOpen.java new file mode 100644 index 0000000..2264486 --- /dev/null +++ b/java/core/src/core/callbacks/IoOpen.java @@ -0,0 +1,27 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callbacks; + +import core.callback.Callback; +import core.callback.CallbackDefault; +import core.io.IoChain; + +public class IoOpen extends CallbackDefault +{ + public IoOpen(IoChain chain) + { + super(chain); + } + + public void onSuccess (Object... args) throws Exception + { + IoChain chain = (IoChain)V(0); + if (chain != null) + chain.open(); + + next(args); + } +} diff --git a/java/core/src/core/callbacks/IoSend.java b/java/core/src/core/callbacks/IoSend.java new file mode 100644 index 0000000..e4f9f81 --- /dev/null +++ b/java/core/src/core/callbacks/IoSend.java @@ -0,0 +1,26 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callbacks; + +import core.callback.Callback; +import core.callback.CallbackDefault; +import core.io.IoChain; + +public class IoSend extends CallbackDefault +{ + public IoSend(IoChain chain) + { + super(chain); + } + + public void onSuccess (Object... args) throws Exception + { + IoChain chain = V(0); + chain.send((byte[])args[0]); + + next(args); + } +} diff --git a/java/core/src/core/callbacks/IoStop.java b/java/core/src/core/callbacks/IoStop.java new file mode 100644 index 0000000..9115456 --- /dev/null +++ b/java/core/src/core/callbacks/IoStop.java @@ -0,0 +1,24 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callbacks; + +import core.callback.CallbackDefault; +import core.io.IoChain; + +public class IoStop extends CallbackDefault +{ + public IoStop() + { + } + + public void onSuccess (Object... arguments) throws Exception + { + IoChain io = (IoChain)arguments[0]; + io.stop(); + + next(arguments[1]); + } +} diff --git a/java/core/src/core/callbacks/JSONDeserialize.java b/java/core/src/core/callbacks/JSONDeserialize.java new file mode 100644 index 0000000..9e9f2b4 --- /dev/null +++ b/java/core/src/core/callbacks/JSONDeserialize.java @@ -0,0 +1,19 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callbacks; + +import core.callback.Callback; +import core.callback.CallbackDefault; +import core.util.JSONSerializer; +import core.util.Zip; + +public class JSONDeserialize extends CallbackDefault +{ + public void onSuccess (Object... args) throws Exception + { + callback.invoke(JSONSerializer.deserialize((byte[])args[0])); + } +}; diff --git a/java/core/src/core/callbacks/JSONSerialize.java b/java/core/src/core/callbacks/JSONSerialize.java new file mode 100644 index 0000000..db3dc21 --- /dev/null +++ b/java/core/src/core/callbacks/JSONSerialize.java @@ -0,0 +1,17 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callbacks; + +import core.callback.CallbackDefault; +import core.util.JSONSerializer; + +public class JSONSerialize extends CallbackDefault +{ + public void onSuccess (Object... args) throws Exception + { + callback.invoke(JSONSerializer.serialize(args[0])); + } +}; diff --git a/java/core/src/core/callbacks/Memory.java b/java/core/src/core/callbacks/Memory.java new file mode 100644 index 0000000..1f6d2c9 --- /dev/null +++ b/java/core/src/core/callbacks/Memory.java @@ -0,0 +1,67 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.callbacks; + +import java.util.HashMap; +import java.util.Map; + +import core.callback.Callback; +import core.callback.CallbackDefault; + +public class Memory +{ + public Map memory = new HashMap(); + + @SuppressWarnings("unchecked") + public T get (String key) + { + return (T)memory.get(key); + } + + public void put (String key, Object o) + { + memory.put(key, o); + } + + public Callback store_(Object...keys) + { + return new CallbackDefault(keys) { + @Override + public void onSuccess(Object... arguments) throws Exception + { + for (int i=0; i () { + @Override + public JSONObject invoke(Object v) throws Exception + { + Get get = (Get)v; + JSONObject object = new JSONObject(); + return object; + } + }, + new CallSingle () { + @Override + public Object invoke(JSONObject v) throws Exception + { + return new Get(); + } + } + ); + + JSONRegistry.register ( + "core.client.messages.Delete", + new CallSingle () { + @Override + public JSONObject invoke(Object v) throws Exception + { + JSONObject object = new JSONObject(); + return object; + } + }, + new CallSingle () { + @Override + public Object invoke(JSONObject v) throws Exception + { + return new Delete(); + } + } + ); + + JSONRegistry.register ( + "core.client.messages.Put", + new CallSingle () { + @Override + public JSONObject invoke(Object v) throws Exception + { + Put set = (Put)v; + JSONObject object = new JSONObject(); + object.put("block", Base64.encode(set.getBlock())); + + return object; + } + }, + new CallSingle () { + @Override + public Object invoke(JSONObject v) throws Exception + { + return new Put(Base64.decode(v.getString("block"))); + } + } + ); + + JSONRegistry.register ( + "core.client.messages.Response", + new CallSingle () { + @Override + public JSONObject invoke(Object v) throws Exception + { + Response response = (Response)v; + JSONObject object = new JSONObject(); + if (response.getBlock() != null) + object.put("block", Base64.encode(response.getBlock())); + + return object; + } + }, + new CallSingle () { + @Override + public Object invoke(JSONObject v) throws Exception + { + if (v.has("block")) + return new Response(Base64.decode(v.getString("block"))); + else + return new Response(); + } + } + ); + } +} diff --git a/java/core/src/core/client/messages/Delete.java b/java/core/src/core/client/messages/Delete.java new file mode 100644 index 0000000..ba0f74f --- /dev/null +++ b/java/core/src/core/client/messages/Delete.java @@ -0,0 +1,14 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.client.messages; + +public class Delete extends Message +{ + public Delete () + { + + } +} diff --git a/java/core/src/core/client/messages/Get.java b/java/core/src/core/client/messages/Get.java new file mode 100644 index 0000000..0830d93 --- /dev/null +++ b/java/core/src/core/client/messages/Get.java @@ -0,0 +1,13 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.client.messages; + +public class Get extends Message +{ + public Get () + { + } +} diff --git a/java/core/src/core/client/messages/Message.java b/java/core/src/core/client/messages/Message.java new file mode 100644 index 0000000..38c510f --- /dev/null +++ b/java/core/src/core/client/messages/Message.java @@ -0,0 +1,11 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.client.messages; + +public class Message +{ + +} diff --git a/java/core/src/core/client/messages/Put.java b/java/core/src/core/client/messages/Put.java new file mode 100644 index 0000000..0e04cb3 --- /dev/null +++ b/java/core/src/core/client/messages/Put.java @@ -0,0 +1,21 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.client.messages; + +public class Put extends Message +{ + byte[] block; + + public Put(byte[] block) + { + this.block = block; + } + + public byte[] getBlock() + { + return block; + } +} diff --git a/java/core/src/core/client/messages/Response.java b/java/core/src/core/client/messages/Response.java new file mode 100644 index 0000000..350318e --- /dev/null +++ b/java/core/src/core/client/messages/Response.java @@ -0,0 +1,26 @@ +/** + * Author: Timothy Prepscius + * License: GPLv3 Affero + keep my name in the code! + */ + +package core.client.messages; + +public class Response extends Message +{ + byte[] block; + + public Response () + { + + } + + public Response (byte[] block) + { + this.block = block; + } + + public byte[] getBlock() + { + return block; + } +}