From 1a2e2d543312fbb918324c99797c5087cc93b089 Mon Sep 17 00:00:00 2001 From: "zhao.dx" Date: Mon, 28 May 2018 16:45:53 +0800 Subject: [PATCH] fix --- fpnn-lib/src/main/java/com/fpnn/FPClient.java | 48 +++--- .../src/main/java/com/fpnn/FPProcessor.java | 12 +- fpnn-lib/src/main/java/com/fpnn/FPSocket.java | 14 +- .../java/com/fpnn/callback/CallbackData.java | 68 ++++++++ .../java/com/fpnn/callback/FPCallback.java | 111 +++++++++---- .../com/fpnn/callback/FPCallbackManager.java | 119 -------------- .../main/java/com/fpnn/event/EventData.java | 97 ++++++++++++ .../src/main/java/com/fpnn/event/FPEvent.java | 111 +++++++------ .../java/com/fpnn/event/FPEventManager.java | 97 ------------ .../src/main/java/com/fpnn/nio/NIOCore.java | 10 +- .../markdown-navigator/profiles_settings.xml | 3 + test-app/.idea/workspace.xml | 141 +++++++++++++++-- test-app/src/main/java/com/test/TestCase.java | 10 +- .../markdown-navigator/profiles_settings.xml | 3 + test-main/.idea/workspace.xml | 146 +++++------------- .../production/main/com/fpnn/FPClient$1.class | Bin 859 -> 893 bytes .../production/main/com/fpnn/FPClient$3.class | Bin 1332 -> 1366 bytes .../production/main/com/fpnn/FPClient$4.class | Bin 886 -> 926 bytes .../production/main/com/fpnn/FPClient.class | Bin 11284 -> 11261 bytes .../main/com/fpnn/FPProcessor$1.class | Bin 1482 -> 1472 bytes .../main/com/fpnn/FPProcessor.class | Bin 1548 -> 1520 bytes .../production/main/com/fpnn/FPSocket.class | Bin 5763 -> 5746 bytes .../main/com/fpnn/SyncCallbak.class | Bin 832 -> 873 bytes .../main/com/fpnn/callback/CallbackData.class | Bin 0 -> 1769 bytes .../main/com/fpnn/callback/FPCallback$1.class | Bin 0 -> 1053 bytes .../main/com/fpnn/callback/FPCallback$2.class | Bin 0 -> 1067 bytes .../fpnn/callback/FPCallback$ICallback.class | Bin 261 -> 263 bytes .../main/com/fpnn/callback/FPCallback.class | Bin 1850 -> 3754 bytes .../fpnn/callback/FPCallbackManager$1.class | Bin 1091 -> 0 bytes .../fpnn/callback/FPCallbackManager$2.class | Bin 1105 -> 0 bytes .../com/fpnn/callback/FPCallbackManager.class | Bin 3837 -> 0 bytes .../main/com/fpnn/event/EventData.class | Bin 0 -> 2402 bytes .../main/com/fpnn/event/FPEvent$1.class | Bin 0 -> 1282 bytes .../com/fpnn/event/FPEvent$IListener.class | Bin 270 -> 272 bytes .../main/com/fpnn/event/FPEvent.class | Bin 2477 -> 2585 bytes .../com/fpnn/event/FPEventManager$1.class | Bin 1346 -> 0 bytes .../main/com/fpnn/event/FPEventManager.class | Bin 2612 -> 0 bytes .../main/com/fpnn/nio/NIOCore.class | Bin 6968 -> 6951 bytes .../test-main/com/test/TestCase$1.class | Bin 1306 -> 1340 bytes .../test-main/com/test/TestCase$2.class | Bin 2026 -> 2026 bytes .../test-main/com/test/TestCase$3.class | Bin 1158 -> 1197 bytes .../test-main/com/test/TestCase.class | Bin 3689 -> 3647 bytes test-main/src/com/test/TestCase.java | 10 +- 43 files changed, 529 insertions(+), 471 deletions(-) create mode 100644 fpnn-lib/src/main/java/com/fpnn/callback/CallbackData.java delete mode 100644 fpnn-lib/src/main/java/com/fpnn/callback/FPCallbackManager.java create mode 100644 fpnn-lib/src/main/java/com/fpnn/event/EventData.java delete mode 100644 fpnn-lib/src/main/java/com/fpnn/event/FPEventManager.java create mode 100644 test-app/.idea/markdown-navigator/profiles_settings.xml create mode 100644 test-main/.idea/markdown-navigator/profiles_settings.xml create mode 100644 test-main/out/production/main/com/fpnn/callback/CallbackData.class create mode 100644 test-main/out/production/main/com/fpnn/callback/FPCallback$1.class create mode 100644 test-main/out/production/main/com/fpnn/callback/FPCallback$2.class delete mode 100644 test-main/out/production/main/com/fpnn/callback/FPCallbackManager$1.class delete mode 100644 test-main/out/production/main/com/fpnn/callback/FPCallbackManager$2.class delete mode 100644 test-main/out/production/main/com/fpnn/callback/FPCallbackManager.class create mode 100644 test-main/out/production/main/com/fpnn/event/EventData.class create mode 100644 test-main/out/production/main/com/fpnn/event/FPEvent$1.class delete mode 100644 test-main/out/production/main/com/fpnn/event/FPEventManager$1.class delete mode 100644 test-main/out/production/main/com/fpnn/event/FPEventManager.class diff --git a/fpnn-lib/src/main/java/com/fpnn/FPClient.java b/fpnn-lib/src/main/java/com/fpnn/FPClient.java index 4c9e981..6a18b4d 100644 --- a/fpnn-lib/src/main/java/com/fpnn/FPClient.java +++ b/fpnn-lib/src/main/java/com/fpnn/FPClient.java @@ -1,10 +1,10 @@ package com.fpnn; +import com.fpnn.callback.CallbackData; import com.fpnn.callback.FPCallback; -import com.fpnn.callback.FPCallbackManager; import com.fpnn.encryptor.FPEncryptor; +import com.fpnn.event.EventData; import com.fpnn.event.FPEvent; -import com.fpnn.event.FPEventManager; import com.fpnn.nio.NIOCore; import java.io.IOException; @@ -35,9 +35,9 @@ public interface IKeyData { private FPPackage _pkg = new FPPackage(); private FPEncryptor _cyr = new FPEncryptor(_pkg); - private FPEventManager _event = new FPEventManager(); + private FPEvent _event = new FPEvent(); private FPProcessor _psr = new FPProcessor(); - private FPCallbackManager _callback = new FPCallbackManager(); + private FPCallback _callback = new FPCallback(); private long _intervalID = 0; @@ -66,7 +66,7 @@ protected void init(String host, int port, boolean autoReconnect, int connection NIOCore.getInstance().getEvent().addListener("second", new FPEvent.IListener() { @Override - public void fpEvent(FPEvent event) { + public void fpEvent(EventData event) { self.onSecond(event.getTimestamp()); } @@ -84,7 +84,7 @@ public void onData(SocketChannel socket) { FPEvent.IListener listener = new FPEvent.IListener() { @Override - public void fpEvent(FPEvent event) { + public void fpEvent(EventData event) { switch (event.getType()) { case "connect": @@ -105,7 +105,7 @@ public void fpEvent(FPEvent event) { this._sock.getEvent().addListener("error", listener); } - public FPEventManager getEvent() { + public FPEvent getEvent() { return this._event; } @@ -174,9 +174,9 @@ public void sendQuest(FPData data) { this.sendQuest(data, null, 0); } - public FPCallback sendQuest(FPData data, int timeout) throws InterruptedException { + public CallbackData sendQuest(FPData data, int timeout) throws InterruptedException { - FPCallback fpcb = null; + CallbackData cbd = null; SyncCallbak syncCallbak = new SyncCallbak(); this.sendQuest(data, syncCallbak, timeout); @@ -188,10 +188,10 @@ public FPCallback sendQuest(FPData data, int timeout) throws InterruptedExceptio syncCallbak.wait(); } - fpcb = syncCallbak.getReturn(); + cbd = syncCallbak.getReturn(); } - return fpcb; + return cbd; } public void sendQuest(FPData data, FPCallback.ICallback callback) { @@ -276,9 +276,9 @@ private void sendkey() { this.sendQuest(data, new FPCallback.ICallback() { @Override - public void callback(FPCallback fpcb) { + public void callback(CallbackData cbd) { - self.onSendKey(fpcb.getData()); + self.onSendKey(cbd.getData()); } }, this._timeout); @@ -287,7 +287,7 @@ public void callback(FPCallback fpcb) { } this._intervalID = 0; - this._event.fireEvent(new FPEvent(this, "connect")); + this._event.fireEvent(new EventData(this, "connect")); } private void onSendKey(FPData data) { @@ -297,7 +297,7 @@ private void onSendKey(FPData data) { if (!data.jsonPayload().trim().equals("{}")) { this._cyr.setCryptoed(false); - this._event.fireEvent(new FPEvent(this, "error", new Exception("wrong cryptor!"))); + this._event.fireEvent(new EventData(this, "error", new Exception("wrong cryptor!"))); return; } } @@ -307,13 +307,13 @@ private void onSendKey(FPData data) { if (data.msgpackPayload().length != 1) { this._cyr.setCryptoed(false); - this._event.fireEvent(new FPEvent(this, "error", new Exception("wrong cryptor!"))); + this._event.fireEvent(new EventData(this, "error", new Exception("wrong cryptor!"))); return; } } this._intervalID = 0; - this._event.fireEvent(new FPEvent(this, "connect")); + this._event.fireEvent(new EventData(this, "connect")); } private void onConnect() { @@ -330,7 +330,7 @@ private void onClose() { this._callback.removeCallback(); this._cyr.clear(); - this._event.fireEvent(new FPEvent(this, "close")); + this._event.fireEvent(new EventData(this, "close")); if (this._autoReconnect) { @@ -452,7 +452,7 @@ private void execCallback(FPData answer) { private void onError(Exception ex) { ex.printStackTrace(); - this._event.fireEvent(new FPEvent(this, "error", ex)); + this._event.fireEvent(new EventData(this, "error", ex)); } private void onSecond(long timestamp) { @@ -506,20 +506,20 @@ private void reConnect() { class SyncCallbak implements FPCallback.ICallback { - private FPCallback _fpcb; + private CallbackData _cbd; @Override - public void callback(FPCallback fpcb) { + public void callback(CallbackData cbd) { synchronized (this) { - this._fpcb = fpcb; + this._cbd = cbd; this.notifyAll(); } } - public FPCallback getReturn() { + public CallbackData getReturn() { - return this._fpcb; + return this._cbd; } } diff --git a/fpnn-lib/src/main/java/com/fpnn/FPProcessor.java b/fpnn-lib/src/main/java/com/fpnn/FPProcessor.java index 48d0b68..3a58ade 100644 --- a/fpnn-lib/src/main/java/com/fpnn/FPProcessor.java +++ b/fpnn-lib/src/main/java/com/fpnn/FPProcessor.java @@ -1,7 +1,7 @@ package com.fpnn; +import com.fpnn.event.EventData; import com.fpnn.event.FPEvent; -import com.fpnn.event.FPEventManager; public class FPProcessor { @@ -13,14 +13,14 @@ public interface IProcessor { } private IProcessor _processor; - private FPEventManager _event; + private FPEvent _event; public FPProcessor() { - this._event = new FPEventManager(); + this._event = new FPEvent(); } - public FPEventManager getEvent() { + public FPEvent getEvent() { return this._event; } @@ -43,12 +43,12 @@ public void service(FPData data, FPClient.IAnswer answer) { if (data.getFlag() == 0) { - self._event.fireEvent(new FPEvent(this, data.getMethod(), data.jsonPayload())); + self._event.fireEvent(new EventData(this, data.getMethod(), data.jsonPayload())); } if (data.getFlag() == 1) { - self._event.fireEvent(new FPEvent(this, data.getMethod(), data.msgpackPayload())); + self._event.fireEvent(new EventData(this, data.getMethod(), data.msgpackPayload())); } } diff --git a/fpnn-lib/src/main/java/com/fpnn/FPSocket.java b/fpnn-lib/src/main/java/com/fpnn/FPSocket.java index fbc6f4e..21150de 100644 --- a/fpnn-lib/src/main/java/com/fpnn/FPSocket.java +++ b/fpnn-lib/src/main/java/com/fpnn/FPSocket.java @@ -1,7 +1,7 @@ package com.fpnn; +import com.fpnn.event.EventData; import com.fpnn.event.FPEvent; -import com.fpnn.event.FPEventManager; import com.fpnn.nio.NIOCore; import com.fpnn.nio.ThreadPool; @@ -31,7 +31,7 @@ public interface IRecvData { private long _expire = 0; private boolean _isClosed = true; - private FPEventManager _event; + private FPEvent _event; private ByteBuffer _sendBuffer = null; private List _sendQueue = new ArrayList(); @@ -41,11 +41,11 @@ public FPSocket(IRecvData recvData, String host, int port, int timeout) { this._host = host; this._port = port; this._timeout = timeout; - this._event = new FPEventManager(); + this._event = new FPEvent(); this._recvData = recvData; } - public FPEventManager getEvent() { + public FPEvent getEvent() { return this._event; } @@ -253,7 +253,7 @@ private SocketChannel initConnect() throws IOException { private void onConnect() { this._expire = 0; - this._event.fireEvent(new FPEvent(this, "connect")); + this._event.fireEvent(new EventData(this, "connect")); } private void onClose(Exception ex) { @@ -266,7 +266,7 @@ private void onClose(Exception ex) { this.onError(ex); } - this._event.fireEvent(new FPEvent(this, "close")); + this._event.fireEvent(new EventData(this, "close")); } private void onData(SocketChannel socket) { @@ -276,7 +276,7 @@ private void onData(SocketChannel socket) { private void onError(Exception ex) { - this._event.fireEvent(new FPEvent(this, "error", ex)); + this._event.fireEvent(new EventData(this, "error", ex)); } public void onSecond(long timestamp) { diff --git a/fpnn-lib/src/main/java/com/fpnn/callback/CallbackData.java b/fpnn-lib/src/main/java/com/fpnn/callback/CallbackData.java new file mode 100644 index 0000000..b561ac5 --- /dev/null +++ b/fpnn-lib/src/main/java/com/fpnn/callback/CallbackData.java @@ -0,0 +1,68 @@ +package com.fpnn.callback; + +import com.fpnn.FPData; + +import java.util.Map; + +public class CallbackData { + + private FPData _data = null; + + public FPData getData() { + + return this._data; + } + + public CallbackData(FPData data) { + + this._data = data; + } + + + private Exception _exception = null; + + public Exception getException() { + + return this._exception; + } + + public CallbackData(Exception ex) { + + this._exception = ex; + } + + + private Object _payload = null; + + public Object getPayload() { + + return this._payload; + } + + public CallbackData(Object payload) { + + this._payload = payload; + } + + + public void checkException(Map data) { + + if (this._exception == null) { + + if (data == null) { + + this._exception = new Exception("data is null!"); + } else if (data.containsKey("code") && data.containsKey("ex")) { + + this._exception = new Exception("code: ".concat(data.get("code").toString()).concat(", ex: ").concat(data.get("ex").toString())); + } + } + + if (this._exception == null) { + + this._payload = data; + } + + this._data = null; + } +} diff --git a/fpnn-lib/src/main/java/com/fpnn/callback/FPCallback.java b/fpnn-lib/src/main/java/com/fpnn/callback/FPCallback.java index dbbf3db..eae09ce 100644 --- a/fpnn-lib/src/main/java/com/fpnn/callback/FPCallback.java +++ b/fpnn-lib/src/main/java/com/fpnn/callback/FPCallback.java @@ -1,74 +1,125 @@ package com.fpnn.callback; +import com.fpnn.FPConfig; import com.fpnn.FPData; +import com.fpnn.nio.ThreadPool; -import java.util.Map; +import java.util.*; public class FPCallback { public interface ICallback { - void callback(FPCallback fpcb); + void callback(CallbackData cbd); } - private FPData _data = null; + private Map _cbMap = new HashMap(); + private Map _exMap = new HashMap(); - public FPData getData() { + public void addCallback(String key, FPCallback.ICallback callback, int timeout) { - return this._data; + synchronized (this._cbMap) { + + this._cbMap.put(key, callback); + } + + synchronized (this._exMap) { + + int ts = timeout <= 0 ? FPConfig.SEND_TIMEOUT : timeout; + long expire = ts + Calendar.getInstance().getTimeInMillis(); + this._exMap.put(key, expire); + } } - public FPCallback(FPData data) { + public void removeCallback() { + + synchronized (this._cbMap) { - this._data = data; + this._cbMap.clear(); + } } + public void execCallback(String key, FPData data) { - private Exception _exception = null; + synchronized (this._cbMap) { - public Exception getException() { + FPCallback.ICallback cb = (FPCallback.ICallback) this._cbMap.get(key); - return this._exception; - } + if (cb != null) { + + this._cbMap.remove(key); + + final FPCallback.ICallback fcb = cb; + final FPData fData = data; - public FPCallback(Exception ex) { + ThreadPool.getInstance().execute(new Runnable() { - this._exception = ex; + @Override + public void run() { + + fcb.callback(new CallbackData(fData)); + } + }); + } + } } + public void execCallback(String key, Exception ex) { - private Object _payload = null; + synchronized (this._cbMap) { - public Object getPayload() { + FPCallback.ICallback cb = (FPCallback.ICallback) this._cbMap.get(key); - return this._payload; - } + if (cb != null) { + + this._cbMap.remove(key); + + final FPCallback.ICallback fcb = cb; + final Exception fex = ex; + + ThreadPool.getInstance().execute(new Runnable() { - public FPCallback(Object payload) { + @Override + public void run() { - this._payload = payload; + fcb.callback(new CallbackData(fex)); + } + }); + } + } } + public void onSecond(long timestamp) { + + synchronized (this._exMap) { - public void checkException(Map data) { + List keys = new ArrayList(); + Iterator itor = this._exMap.entrySet().iterator(); - if (this._exception == null) { + while (itor.hasNext()) { - if (data == null) { + Map.Entry entry = (Map.Entry) itor.next(); + String key = (String) entry.getKey(); + long expire = (long) entry.getValue(); - this._exception = new Exception("data is null!"); - } else if (data.containsKey("code") && data.containsKey("ex")) { + if (expire > timestamp) { - this._exception = new Exception("code: ".concat(data.get("code").toString()).concat(", ex: ").concat(data.get("ex").toString())); + continue; + } + + keys.add(key); } - } - if (this._exception == null) { + itor = keys.iterator(); - this._payload = data; - } + while (itor.hasNext()) { - this._data = null; + String key = (String) itor.next(); + + this._exMap.remove(key); + this.execCallback(key, new Exception("timeout with expire")); + } + } } } diff --git a/fpnn-lib/src/main/java/com/fpnn/callback/FPCallbackManager.java b/fpnn-lib/src/main/java/com/fpnn/callback/FPCallbackManager.java deleted file mode 100644 index ed741c5..0000000 --- a/fpnn-lib/src/main/java/com/fpnn/callback/FPCallbackManager.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.fpnn.callback; - -import com.fpnn.FPConfig; -import com.fpnn.FPData; -import com.fpnn.nio.ThreadPool; - -import java.util.*; - -public class FPCallbackManager { - - private Map _cbMap = new HashMap(); - private Map _exMap = new HashMap(); - - public void addCallback(String key, FPCallback.ICallback callback, int timeout) { - - synchronized (this._cbMap) { - - this._cbMap.put(key, callback); - } - - synchronized (this._exMap) { - - int ts = timeout <= 0 ? FPConfig.SEND_TIMEOUT : timeout; - long expire = ts + Calendar.getInstance().getTimeInMillis(); - this._exMap.put(key, expire); - } - } - - public void removeCallback() { - - synchronized (this._cbMap) { - - this._cbMap.clear(); - } - } - - public void execCallback(String key, FPData data) { - - synchronized (this._cbMap) { - - FPCallback.ICallback cb = (FPCallback.ICallback) this._cbMap.get(key); - - if (cb != null) { - - this._cbMap.remove(key); - - final FPCallback.ICallback fcb = cb; - final FPData fData = data; - - ThreadPool.getInstance().execute(new Runnable() { - - @Override - public void run() { - - fcb.callback(new FPCallback(fData)); - } - }); - } - } - } - - public void execCallback(String key, Exception ex) { - - synchronized (this._cbMap) { - - FPCallback.ICallback cb = (FPCallback.ICallback) this._cbMap.get(key); - - if (cb != null) { - - this._cbMap.remove(key); - - final FPCallback.ICallback fcb = cb; - final Exception fex = ex; - - ThreadPool.getInstance().execute(new Runnable() { - - @Override - public void run() { - - fcb.callback(new FPCallback(fex)); - } - }); - } - } - } - - public void onSecond(long timestamp) { - - synchronized (this._exMap) { - - List keys = new ArrayList(); - Iterator itor = this._exMap.entrySet().iterator(); - - while (itor.hasNext()) { - - Map.Entry entry = (Map.Entry) itor.next(); - String key = (String) entry.getKey(); - long expire = (long) entry.getValue(); - - if (expire > timestamp) { - - continue; - } - - keys.add(key); - } - - itor = keys.iterator(); - - while (itor.hasNext()) { - - String key = (String) itor.next(); - - this._exMap.remove(key); - this.execCallback(key, new Exception("timeout with expire")); - } - } - } -} diff --git a/fpnn-lib/src/main/java/com/fpnn/event/EventData.java b/fpnn-lib/src/main/java/com/fpnn/event/EventData.java new file mode 100644 index 0000000..aa9c5cb --- /dev/null +++ b/fpnn-lib/src/main/java/com/fpnn/event/EventData.java @@ -0,0 +1,97 @@ +package com.fpnn.event; + +import com.fpnn.FPData; + +import java.nio.channels.SocketChannel; +import java.util.EventObject; + +public class EventData extends EventObject { + + private String _type; + + public String getType() { + + return this._type; + } + + public EventData(Object source, String type) { + + super(source); + this._type = type; + } + + + private FPData _data = null; + + public FPData getData() { + + return this._data; + } + + public EventData(Object source, String type, FPData data) { + + super(source); + this._type = type; + this._data = data; + } + + + private SocketChannel _socket = null; + + public SocketChannel getSocket() { + + return this._socket; + } + + public EventData(Object source, String type, SocketChannel socket) { + + super(source); + this._type = type; + this._socket = socket; + } + + + private Exception _exception = null; + + public Exception getException() { + + return this._exception; + } + + public EventData(Object source, String type, Exception ex) { + + super(source); + this._type = type; + this._exception = ex; + } + + + private long _timestamp = 0; + + public long getTimestamp() { + + return this._timestamp; + } + + public EventData(Object source, String type, long timestamp) { + + super(source); + this._type = type; + this._timestamp = timestamp; + } + + + private Object _payload; + + public Object getPayload() { + + return this._payload; + } + + public EventData(Object source, String type, Object payload) { + + super(source); + this._type = type; + this._payload = payload; + } +} diff --git a/fpnn-lib/src/main/java/com/fpnn/event/FPEvent.java b/fpnn-lib/src/main/java/com/fpnn/event/FPEvent.java index 4cbf1d4..39a10d2 100644 --- a/fpnn-lib/src/main/java/com/fpnn/event/FPEvent.java +++ b/fpnn-lib/src/main/java/com/fpnn/event/FPEvent.java @@ -1,104 +1,99 @@ package com.fpnn.event; -import com.fpnn.FPData; +import com.fpnn.nio.ThreadPool; -import java.nio.channels.SocketChannel; -import java.util.EventListener; -import java.util.EventObject; +import java.util.*; -public class FPEvent extends EventObject { +public class FPEvent { public interface IListener extends EventListener { - void fpEvent(FPEvent event); + void fpEvent(EventData event); } - private String _type; + private Map _listeners = new HashMap(); - public String getType() { + public void addListener(String type, IListener lisr) { - return this._type; - } - - public FPEvent(Object source, String type) { - - super(source); - this._type = type; - } + synchronized (this._listeners) { + List queue = (List) this._listeners.get(type); - private FPData _data = null; + if (queue == null) { - public FPData getData() { + queue = new ArrayList(); + this._listeners.put(type, queue); + } - return this._data; + queue.add(lisr); + } } - public FPEvent(Object source, String type, FPData data) { - - super(source); - this._type = type; - this._data = data; - } + public void fireEvent(EventData event) { + List queue; - private SocketChannel _socket = null; + synchronized (this._listeners) { - public SocketChannel getSocket() { + queue = (List) this._listeners.get(event.getType()); + } - return this._socket; - } + if (queue != null && queue.size() > 0) { - public FPEvent(Object source, String type, SocketChannel socket) { + final List fQueue = queue; + final EventData fEvent = event; - super(source); - this._type = type; - this._socket = socket; - } + ThreadPool.getInstance().execute(new Runnable() { + @Override + public void run() { - private Exception _exception = null; + Iterator iterator = fQueue.iterator(); - public Exception getException() { + while (iterator.hasNext()) { - return this._exception; + IListener lisr = iterator.next(); + lisr.fpEvent(fEvent); + } + } + }); + } } - public FPEvent(Object source, String type, Exception ex) { + public void removeListener() { - super(source); - this._type = type; - this._exception = ex; - } + synchronized (this._listeners) { + this._listeners.clear(); + } + } - private long _timestamp = 0; + public void removeListener(String type) { - public long getTimestamp() { + synchronized (this._listeners) { - return this._timestamp; + this._listeners.remove(type); + } } - public FPEvent(Object source, String type, long timestamp) { + public void removeListener(String type, IListener lisr) { - super(source); - this._type = type; - this._timestamp = timestamp; - } + synchronized (this._listeners) { + List queue = (List) this._listeners.get(type); - private Object _payload; + if (queue == null) { - public Object getPayload() { + return; + } - return this._payload; - } + int index = queue.indexOf(lisr); - public FPEvent(Object source, String type, Object payload) { + if (index != -1) { - super(source); - this._type = type; - this._payload = payload; + queue.remove(index); + } + } } } diff --git a/fpnn-lib/src/main/java/com/fpnn/event/FPEventManager.java b/fpnn-lib/src/main/java/com/fpnn/event/FPEventManager.java deleted file mode 100644 index 2ab7258..0000000 --- a/fpnn-lib/src/main/java/com/fpnn/event/FPEventManager.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.fpnn.event; - -import com.fpnn.nio.ThreadPool; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -public class FPEventManager { - - private Map _listeners = new HashMap(); - - public void addListener(String type, FPEvent.IListener lisr) { - - synchronized (this._listeners) { - - List queue = (List) this._listeners.get(type); - - if (queue == null) { - - queue = new ArrayList(); - this._listeners.put(type, queue); - } - - queue.add(lisr); - } - } - - public void fireEvent(FPEvent event) { - - List queue; - - synchronized (this._listeners) { - - queue = (List) this._listeners.get(event.getType()); - } - - if (queue != null && queue.size() > 0) { - - final List fQueue = queue; - final FPEvent fEvent = event; - - ThreadPool.getInstance().execute(new Runnable() { - - @Override - public void run() { - - Iterator iterator = fQueue.iterator(); - - while (iterator.hasNext()) { - - FPEvent.IListener lisr = iterator.next(); - lisr.fpEvent(fEvent); - } - } - }); - } - } - - public void removeListener() { - - synchronized (this._listeners) { - - this._listeners.clear(); - } - } - - public void removeListener(String type) { - - synchronized (this._listeners) { - - this._listeners.remove(type); - } - } - - public void removeListener(String type, FPEvent.IListener lisr) { - - synchronized (this._listeners) { - - List queue = (List) this._listeners.get(type); - - if (queue == null) { - - return; - } - - int index = queue.indexOf(lisr); - - if (index != -1) { - - queue.remove(index); - } - } - } -} diff --git a/fpnn-lib/src/main/java/com/fpnn/nio/NIOCore.java b/fpnn-lib/src/main/java/com/fpnn/nio/NIOCore.java index 8c243dd..0006346 100644 --- a/fpnn-lib/src/main/java/com/fpnn/nio/NIOCore.java +++ b/fpnn-lib/src/main/java/com/fpnn/nio/NIOCore.java @@ -1,8 +1,8 @@ package com.fpnn.nio; import com.fpnn.FPSocket; +import com.fpnn.event.EventData; import com.fpnn.event.FPEvent; -import com.fpnn.event.FPEventManager; import java.io.IOException; import java.nio.channels.SelectionKey; @@ -22,7 +22,7 @@ private NIOCore() { try { - this._event = new FPEventManager(); + this._event = new FPEvent(); this._selector = this.initSelector(); ThreadPool.getInstance().execute(this); } catch (IOException ex) { @@ -41,13 +41,13 @@ public static final NIOCore getInstance() { return Singleton.INSTANCE; } - private FPEventManager _event; + private FPEvent _event; private Selector _selector; private Map _fpsockData = new HashMap(); private List _pendingChanges = new LinkedList(); - public FPEventManager getEvent() { + public FPEvent getEvent() { return this._event; } @@ -267,7 +267,7 @@ public void checkSecond() { synchronized (this._fpsockData) { long ts = this.getTimestamp(); - this._event.fireEvent(new FPEvent(this, "second", ts)); + this._event.fireEvent(new EventData(this, "second", ts)); Iterator iterator = this._fpsockData.values().iterator(); while (iterator.hasNext()) { diff --git a/test-app/.idea/markdown-navigator/profiles_settings.xml b/test-app/.idea/markdown-navigator/profiles_settings.xml new file mode 100644 index 0000000..57927c5 --- /dev/null +++ b/test-app/.idea/markdown-navigator/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test-app/.idea/workspace.xml b/test-app/.idea/workspace.xml index 80928b8..7da7201 100644 --- a/test-app/.idea/workspace.xml +++ b/test-app/.idea/workspace.xml @@ -23,28 +23,50 @@ - + - + - - + + - - - + + + + + + + + + + + + + + + + + + + + + + fpcd + fpcb + + + + + @@ -2638,6 +2683,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -2713,10 +2812,32 @@ - + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test-app/src/main/java/com/test/TestCase.java b/test-app/src/main/java/com/test/TestCase.java index abad687..f2a302c 100644 --- a/test-app/src/main/java/com/test/TestCase.java +++ b/test-app/src/main/java/com/test/TestCase.java @@ -2,8 +2,10 @@ import com.fpnn.FPClient; import com.fpnn.FPData; +import com.fpnn.callback.CallbackData; import com.fpnn.callback.FPCallback; import com.fpnn.encryptor.FPEncryptor; +import com.fpnn.event.EventData; import com.fpnn.event.FPEvent; import com.fpnn.nio.ThreadPool; @@ -26,7 +28,7 @@ public TestCase(byte[] derKey) { FPEvent.IListener listener = new FPEvent.IListener() { @Override - public void fpEvent(FPEvent event) { + public void fpEvent(EventData event) { switch (event.getType()) { case "connect": @@ -133,16 +135,16 @@ private void onConnect() { _client.sendQuest(data, new FPCallback.ICallback() { @Override - public void callback(FPCallback fpcb) { + public void callback(CallbackData cbd) { - FPData data = fpcb.getData(); + FPData data = cbd.getData(); if (data != null) { System.out.println(data.msgpackPayload()); } else { - fpcb.getException().printStackTrace(); + cbd.getException().printStackTrace(); } } }); diff --git a/test-main/.idea/markdown-navigator/profiles_settings.xml b/test-main/.idea/markdown-navigator/profiles_settings.xml new file mode 100644 index 0000000..57927c5 --- /dev/null +++ b/test-main/.idea/markdown-navigator/profiles_settings.xml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test-main/.idea/workspace.xml b/test-main/.idea/workspace.xml index 27877de..cdf5245 100644 --- a/test-main/.idea/workspace.xml +++ b/test-main/.idea/workspace.xml @@ -15,7 +15,7 @@ - + @@ -24,11 +24,14 @@ - + - - + + + + + @@ -55,6 +58,7 @@ wakeup OP_WRITE setMtype + fpcd @@ -86,6 +90,8 @@ + + @@ -93,12 +99,21 @@ + + + + + + + + + + +