com.savl.ripple.client.subscriptions.SubscriptionManager Maven / Gradle / Ivy
package com.savl.ripple.client.subscriptions;
import com.savl.ripple.client.pubsub.Publisher;
import com.savl.ripple.core.coretypes.AccountID;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Collection;
import java.util.Set;
import java.util.TreeSet;
public class SubscriptionManager extends Publisher {
public void pauseEventEmissions() {
paused = true;
}
public void unpauseEventEmissions() {
paused = false;
}
public static interface events extends Publisher.Callback {}
public static interface OnSubscribed extends events {}
public static interface OnUnSubscribed extends events {}
public boolean paused = false;
public enum Stream {
server,
ledger,
transactions,
transactions_propose
}
Set streams = new TreeSet();
Set accounts = new TreeSet();
Set single(T element) {
Set set = new TreeSet();
set.add(element);
return set;
}
public void addStream(Stream s) {
streams.add(s);
subscribeStream(s);
}
public void removeStream(Stream s) {
streams.remove(s);
unsubscribeStream(s);
}
private void subscribeStream(Stream s) {
emit(OnSubscribed.class, basicSubscriptionObject(single(s), null));
}
@Override
public > int emit(Class key, A args) {
if (paused) {
return 0;
}
return super.emit(key, args);
}
private void unsubscribeStream(Stream s) {
emit(OnUnSubscribed.class, basicSubscriptionObject(single(s), null));
}
public void addAccount(AccountID a) {
accounts.add(a);
emit(OnSubscribed.class, basicSubscriptionObject(null, single(a)));
}
public void removeAccount(AccountID a) {
accounts.remove(a);
emit(OnUnSubscribed.class, basicSubscriptionObject(null, single(a)));
}
private JSONObject basicSubscriptionObject(Set streams, Set accounts) {
JSONObject subs = new JSONObject();
if (streams != null && streams.size() > 0) subs.put("streams", getJsonArray(streams));
if (accounts != null && accounts.size() > 0) subs.put("accounts", getJsonArray(accounts));
return subs;
}
private JSONArray getJsonArray(Collection> streams) {
// Yes, JSONArray has a Collection constructor, but it doesn't play
// so nicely on android.
JSONArray jsonArray = new JSONArray();
for (Object obj : streams) {
jsonArray.put(obj);
}
return jsonArray;
}
public JSONObject allSubscribed() {
return basicSubscriptionObject(streams, accounts);
}
}