|
| 1 | +//package rpc.framework.server; |
| 2 | +// |
| 3 | +//import java.util.ArrayList; |
| 4 | +//import java.util.HashSet; |
| 5 | +//import java.util.LinkedList; |
| 6 | +//import java.util.concurrent.ConcurrentHashMap; |
| 7 | +// |
| 8 | +//import org.json.JSONObject; |
| 9 | +// |
| 10 | +//import rpc.framework.RpcInvoker; |
| 11 | +//import rpc.framework.RpcServerInvokerHandler; |
| 12 | +//import rpc.json.message.RpcNotification; |
| 13 | +//import rpc.util.IDGenerator; |
| 14 | +//import rpc.util.RpcLog; |
| 15 | +// |
| 16 | +///** |
| 17 | +// * @author sunshyran |
| 18 | +// * |
| 19 | +// */ |
| 20 | +//public class EventCenter implements EventListenerInterface { |
| 21 | +// private static final String TAG="EventCenter"; |
| 22 | +// private RpcServerInvokerHandler mHandler = null; |
| 23 | +// private IDGenerator mSIDGen = new IDGenerator(); // TODO:: reuse id generator temporally |
| 24 | +// public static final long INVALID_SID = -1; |
| 25 | +// private HashSet<EventSourceInterface> mSources = new HashSet<EventSourceInterface>(); |
| 26 | +// |
| 27 | +// private class EventListener implements EventListenerInterface { |
| 28 | +// private long mSID = -1; |
| 29 | +// private EventListenerInterface mListener = null; |
| 30 | +// public EventListener(long SID, EventListenerInterface listener) { |
| 31 | +// mSID = SID; |
| 32 | +// mListener = listener; |
| 33 | +// } |
| 34 | +// @Override |
| 35 | +// public void onNotify(Event event, Object data) { |
| 36 | +// mListener.onNotify(event, data); |
| 37 | +// } |
| 38 | +// |
| 39 | +// } |
| 40 | +// private ConcurrentHashMap<Event, LinkedList<EventListener>> mListeners = new ConcurrentHashMap<Event, LinkedList<EventListener>>(); |
| 41 | +// |
| 42 | +// /**add event source into event center |
| 43 | +// * @param source |
| 44 | +// */ |
| 45 | +// public void addEventSource(EventSourceInterface source) { |
| 46 | +// synchronized (mSources) { |
| 47 | +// if (mSources.contains(source)) { |
| 48 | +// RpcLog.w(TAG, "source " + source + " is already added into event center!"); |
| 49 | +// } else { |
| 50 | +// RpcLog.i(TAG, "add source " + source + " into event center"); |
| 51 | +// mSources.add(source); |
| 52 | +// } |
| 53 | +// } |
| 54 | +// } |
| 55 | +// |
| 56 | +// /**remove source from event center |
| 57 | +// * @param source |
| 58 | +// */ |
| 59 | +// public void removeEventSource(EventSourceInterface source) { |
| 60 | +// synchronized (mSources) { |
| 61 | +// if (!mSources.contains(source)) { |
| 62 | +// RpcLog.w(TAG, "source " + source + " is not in event center!"); |
| 63 | +// } else { |
| 64 | +// RpcLog.i(TAG, "remove source " + source + " from event center"); |
| 65 | +// mSources.remove(source); |
| 66 | +// } |
| 67 | +// } |
| 68 | +// } |
| 69 | +// |
| 70 | +// public long addEventListener(Event event, EventListener listener) { |
| 71 | +// boolean isAcceptable = false; |
| 72 | +// synchronized (mSources) { |
| 73 | +// for (EventSourceInterface source:mSources) { |
| 74 | +// if (source.acceptEvent(event)) { |
| 75 | +// if (source.subscribe(event, this)) { |
| 76 | +// isAcceptable = true; |
| 77 | +// } else { |
| 78 | +// RpcLog.e(TAG, "subscribe event of source failed: " + event + ", " + source); |
| 79 | +// } |
| 80 | +// } |
| 81 | +// } |
| 82 | +// } |
| 83 | +// |
| 84 | +// if (!isAcceptable) { |
| 85 | +// RpcLog.e(TAG, "no source accept event " + event); |
| 86 | +// return INVALID_SID; |
| 87 | +// } |
| 88 | +// |
| 89 | +// long sid = aquireSID(); |
| 90 | +// LinkedList<EventListener> listeners = mListeners.getOrDefault(event, new LinkedList<EventListener>()); |
| 91 | +// synchronized (listeners) { // FIXME:: thread safe is enough? |
| 92 | +// listeners.add(new EventListener(sid, listener)); |
| 93 | +// mListeners.put(event, listeners); |
| 94 | +// } |
| 95 | +// return sid; |
| 96 | +// } |
| 97 | +// |
| 98 | +// public void removeEventListener(Event event, EventListener listener) { |
| 99 | +// boolean isAcceptable = false; |
| 100 | +// synchronized (mSources) { |
| 101 | +// for (EventSourceInterface source:mSources) { |
| 102 | +// if (source.acceptEvent(event)) { |
| 103 | +// source.unsubscribe(event, this); |
| 104 | +// } |
| 105 | +// } |
| 106 | +// } |
| 107 | +// |
| 108 | +// |
| 109 | +// } |
| 110 | +// |
| 111 | +// |
| 112 | +// |
| 113 | +// private long aquireSID() { |
| 114 | +// return mSIDGen.getRequestId(); |
| 115 | +// } |
| 116 | +// |
| 117 | +// |
| 118 | +// @Override |
| 119 | +// public void onNotify(Event event, Object data) { |
| 120 | +// System.out.println("onNotify"); |
| 121 | +// HashSet<EventListener> listeners = mListeners.get(event); |
| 122 | +// if (listeners == null) { return; } |
| 123 | +// ArrayList<EventListener> listeners = new ArrayList<EventListener>(listeners.); |
| 124 | +// |
| 125 | +// RpcNotification notification = new RpcNotification(); |
| 126 | +// notification.setMethod(event.method); |
| 127 | +// JSONObject params = new JSONObject(); |
| 128 | +// params.put("SID", sid); |
| 129 | +// params.put("data", data); |
| 130 | +// notification.setParams(params); |
| 131 | +// try { |
| 132 | +// notify(notification); |
| 133 | +// } catch (InterruptedException e) { |
| 134 | +// // TODO Auto-generated catch block |
| 135 | +// e.printStackTrace(); |
| 136 | +// } |
| 137 | +// } |
| 138 | +// |
| 139 | +// private long getRemoteEventListener(Event event) { |
| 140 | +// returen 0 |
| 141 | +// } |
| 142 | +// |
| 143 | +// private void notify(RpcNotification notification) throws InterruptedException { |
| 144 | +// mHandler.invoke(new RpcInvoker(notification, null, null)); |
| 145 | +// } |
| 146 | +//} |
0 commit comments