add files

This commit is contained in:
Timothy Prepscius 2013-07-14 14:43:20 -04:00
parent e822c224af
commit e332e79f6d
28 changed files with 1108 additions and 0 deletions

View File

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

View File

@ -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<Callback> array = new ArrayList<Callback>();
Callback i = head;
while (i!=null)
{
array.add(i);
i = i.callback;
}
return array.toArray(new Callback[0]);
}
}

View File

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

View File

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

View File

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

View File

@ -0,0 +1,65 @@
/**
* Author: Timothy Prepscius
* License: GPLv3 Affero + keep my name in the code!
*/
package core.callback;
public class CallbackSync<T>
{
CallbackChain chain;
Object[] results;
public CallbackSync(Callback callback)
{
chain = callback.addCallback(setResults_());
}
public CallbackSync<T> 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;
}
}

View File

@ -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> T V(int i)
{
return (T)v[i];
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<String, Object> memory = new HashMap<String, Object>();
@SuppressWarnings("unchecked")
public <T> 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<v.length; ++i)
{
memory.put((String)v[i], arguments[i]);
}
next(arguments);
}
};
}
public Callback restore_(Object...keys)
{
return new CallbackDefault(keys) {
public void onSuccess(Object...arguments) {
Object[] keys = v;
Object[] args = new Object[keys.length];
int j=0;
for (Object i : keys)
{
if (i instanceof Integer)
args[j++] = arguments[(Integer)i];
else
args[j++] = memory.get((String)i);
}
next(args);
}
};
}
}

View File

@ -0,0 +1,43 @@
/**
* Author: Timothy Prepscius
* License: GPLv3 Affero + keep my name in the code!
*/
package core.callbacks;
import core.callback.Callback;
import core.callback.CallbackDefault;
public class SaveArguments extends CallbackDefault{
Object[] saved;
@Override
public void onSuccess(Object... save) throws Exception {
this.saved = save;
next(save);
}
public Callback restore_(int...indexes)
{
return new CallbackDefault (new Object[] {indexes})
{
public void onSuccess(Object...arguments) {
int[] indexes = V(0);
Object[] args = new Object[indexes.length];
int j=0;
for (int i : indexes)
{
if (i < 100)
args[j++] = arguments[i];
else
args[j++] = saved[i-100];
}
next(args);
}
};
}
}

View File

@ -0,0 +1,15 @@
/**
* Author: Timothy Prepscius
* License: GPLv3 Affero + keep my name in the code!
*/
package core.callbacks;
import core.callback.Callback;
public class Single extends CountDown
{
public Single(Callback onFinished) {
super(1, onFinished);
}
}

View File

@ -0,0 +1,26 @@
/**
* Author: Timothy Prepscius
* License: GPLv3 Affero + keep my name in the code!
*/
package core.callbacks;
import core.callback.Callback;
public class Split extends Callback {
Callback split;
public Split(Callback split)
{
this.split = split;
}
@Override
public void invoke(Object... arguments) {
split.invoke(arguments);
next(arguments);
}
}

View File

@ -0,0 +1,42 @@
/**
* 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 SuccessFailure extends CallbackDefault
{
LogNull log = new LogNull(SuccessFailure.class);
Callback succeed, fail;
public SuccessFailure (Callback succeed, Callback fail)
{
this.succeed = succeed;
this.fail = fail;
}
@Override
public void onSuccess(Object... arguments) throws Exception
{
log.debug("onSuccess");
succeed.setReturn(callback);
succeed.invoke(arguments);
}
@Override
public void onFailure(Exception e)
{
log.debug("onFailure, transmogrifying to string",e);
fail.setReturn(callback);
fail.invoke(e.getMessage());
}
}

View File

@ -0,0 +1,74 @@
/**
* Author: Timothy Prepscius
* License: GPLv3 Affero + keep my name in the code!
*/
package core.client;
import com.jordanzimmerman.SRPVerifier;
import core.callback.Callback;
import core.callbacks.IoSend;
import core.callbacks.JSONSerialize;
import core.constants.ConstantsVersion;
import core.crypt.Cryptor;
import core.crypt.KeyPairFromPassword;
import core.io.IoChain;
import core.srp.SRPPackets;
import core.srp.client.SRPClient;
import core.util.SimpleSerializer;
public class ClientCreateSession extends IoChain
{
static { SRPPackets.register(); }
Cryptor cryptor;
Callback callback;
String user;
KeyPairFromPassword keyPair;
byte[] extra;
public ClientCreateSession (Cryptor cryptor, String user, KeyPairFromPassword keyPair, byte[] extra, Callback callback, IoChain session)
{
super(session);
this.user = user;
this.keyPair = keyPair;
this.callback = callback;
this.extra = extra;
this.cryptor = cryptor;
}
@Override
public void open () throws Exception
{
SRPVerifier verifier = SRPClient.createVerifierFromKeyPair(keyPair);
SRPPackets.PacketInit_ClientPreAutheticationInitialization init =
new SRPPackets.PacketInit_ClientPreAutheticationInitialization(
ConstantsVersion.LOGIN, user, verifier.verifier_v.toByteArray(), verifier.salt_s.toByteArray()
);
init.extra = extra;
new JSONSerialize()
.addCallback(cryptor.encrypt_())
.addCallback(SRPPackets.PacketInit_EncryptedPacket.wrap_())
.addCallback(new JSONSerialize())
.addCallback(new IoSend(sender))
.invoke(init);
}
@Override
public void onReceive (byte[] bytes) throws Exception
{
callback.invoke(this, bytes);
}
@Override
public void onException(Exception e)
{
callback.invoke(this, e);
}
}

View File

@ -0,0 +1,61 @@
/**
* Author: Timothy Prepscius
* License: GPLv3 Affero + keep my name in the code!
*/
package core.client;
import core.callback.Callback;
import core.callbacks.IoSend;
import core.callbacks.JSONSerialize;
import core.constants.ConstantsVersion;
import core.crypt.Cryptor;
import core.io.IoChain;
import core.srp.SRPPackets;
import core.srp.SRPPackets.PacketInit_ClientTestCreate;
import core.util.SimpleSerializer;
public class ClientTestCreateSession extends IoChain
{
static { SRPPackets.register(); }
Cryptor cryptor;
Callback callback;
String user;
public ClientTestCreateSession (Cryptor cryptor, String user, Callback callback, IoChain session)
{
super(session);
this.user = user;
this.callback = callback;
this.cryptor = cryptor;
}
@Override
public void open () throws Exception
{
SRPPackets.PacketInit_ClientTestCreate test =
new PacketInit_ClientTestCreate(ConstantsVersion.LOGIN, user);
new JSONSerialize()
.addCallback(cryptor.encrypt_())
.addCallback(SRPPackets.PacketInit_EncryptedPacket.wrap_())
.addCallback(new JSONSerialize())
.addCallback(new IoSend(sender))
.invoke(test);
}
@Override
public void onReceive (byte[] bytes) throws Exception
{
callback.invoke(this, bytes);
}
@Override
public void onException(Exception e)
{
callback.invoke(this, e);
}
}

View File

@ -0,0 +1,49 @@
/**
* Author: Timothy Prepscius
* License: GPLv3 Affero + keep my name in the code!
*/
package core.client;
import core.callback.Callback;
import core.client.messages.ClientMessagesSerializerJSON;
import core.client.messages.Message;
import core.client.messages.Response;
import core.io.IoChain;
import core.srp.SRPPackets;
import core.util.SimpleSerializer;
public class ClientUserSession extends IoChain
{
static { SRPPackets.register(); ClientMessagesSerializerJSON.register(); }
Callback callback;
Message message;
public ClientUserSession (Message message, Callback callback, IoChain sender)
{
super(sender);
this.message = message;
this.callback = callback;
}
@Override
public void open () throws Exception
{
sender.send(SimpleSerializer.serialize(message));
}
@Override
public void onReceive (byte[] bytes) throws Exception
{
Response message = SimpleSerializer.deserialize(bytes);
callback.invoke(this, message.getBlock());
}
@Override
public void onException(Exception e)
{
callback.invoke(this, e);
}
}

View File

@ -0,0 +1,107 @@
/**
* Author: Timothy Prepscius
* License: GPLv3 Affero + keep my name in the code!
*/
package core.client.messages;
import org.json.JSONObject;
import core.util.Base64;
import core.util.CallSingle;
import core.util.JSONRegistry;
public class ClientMessagesSerializerJSON
{
static public void register () {}
static {
JSONRegistry.register (
"core.client.messages.Get",
new CallSingle<JSONObject,Object> () {
@Override
public JSONObject invoke(Object v) throws Exception
{
Get get = (Get)v;
JSONObject object = new JSONObject();
return object;
}
},
new CallSingle<Object,JSONObject> () {
@Override
public Object invoke(JSONObject v) throws Exception
{
return new Get();
}
}
);
JSONRegistry.register (
"core.client.messages.Delete",
new CallSingle<JSONObject,Object> () {
@Override
public JSONObject invoke(Object v) throws Exception
{
JSONObject object = new JSONObject();
return object;
}
},
new CallSingle<Object,JSONObject> () {
@Override
public Object invoke(JSONObject v) throws Exception
{
return new Delete();
}
}
);
JSONRegistry.register (
"core.client.messages.Put",
new CallSingle<JSONObject,Object> () {
@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<Object,JSONObject> () {
@Override
public Object invoke(JSONObject v) throws Exception
{
return new Put(Base64.decode(v.getString("block")));
}
}
);
JSONRegistry.register (
"core.client.messages.Response",
new CallSingle<JSONObject,Object> () {
@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<Object,JSONObject> () {
@Override
public Object invoke(JSONObject v) throws Exception
{
if (v.has("block"))
return new Response(Base64.decode(v.getString("block")));
else
return new Response();
}
}
);
}
}

View File

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

View File

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

View File

@ -0,0 +1,11 @@
/**
* Author: Timothy Prepscius
* License: GPLv3 Affero + keep my name in the code!
*/
package core.client.messages;
public class Message
{
}

View File

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

View File

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