summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMikayla Ray <mikayla@livioconnect.com>2015-06-02 16:33:35 -0400
committerMikayla Ray <mikayla@livioconnect.com>2015-06-02 16:33:35 -0400
commit55ba8a3ddd26828d7bde92a1e2ebe02d513db46e (patch)
tree4a468dbb81f8a44910bda767cb3a61b9f38d0d65
parent986920c4e51a4adf9fee1130c97c9db107abcbff (diff)
downloadsdl_android-feature/dispatch_reface.tar.gz
Cleans up several minor issues in the dispatcher package classes as they relate to SdlProxyBase.feature/dispatch_reface
-rw-r--r--sdl_android_lib/src/com/smartdevicelink/Dispatcher/Dispatcher.java108
-rw-r--r--sdl_android_lib/src/com/smartdevicelink/Dispatcher/IDispatchingStrategy.java5
-rw-r--r--sdl_android_lib/src/com/smartdevicelink/Dispatcher/IncomingProtocolMessageComparitor.java14
-rw-r--r--sdl_android_lib/src/com/smartdevicelink/Dispatcher/InternalProxyMessageComparitor.java14
-rw-r--r--sdl_android_lib/src/com/smartdevicelink/Dispatcher/OutgoingProtocolMessageComparitor.java15
-rw-r--r--sdl_android_lib/src/com/smartdevicelink/Dispatcher/ProxyMessageDispatcher.java65
-rw-r--r--sdl_android_lib/src/com/smartdevicelink/proxy/SdlProxyBase.java22
7 files changed, 119 insertions, 124 deletions
diff --git a/sdl_android_lib/src/com/smartdevicelink/Dispatcher/Dispatcher.java b/sdl_android_lib/src/com/smartdevicelink/Dispatcher/Dispatcher.java
new file mode 100644
index 000000000..d39d266b7
--- /dev/null
+++ b/sdl_android_lib/src/com/smartdevicelink/Dispatcher/Dispatcher.java
@@ -0,0 +1,108 @@
+package com.smartdevicelink.dispatcher;
+
+import java.util.Comparator;
+import java.util.concurrent.PriorityBlockingQueue;
+
+import com.smartdevicelink.util.DebugTool;
+
+public class Dispatcher<T> {
+
+ private Boolean isDisposed = false;
+ private Thread dispatchThread = null;
+ private IDispatchingStrategy<T> dispatchStrategy = null;
+ private PriorityBlockingQueue<T> dispatchQueue = null;
+
+ /**
+ * Initializes a message queue and a new thread to dispatch the messages
+ * in the queue that will follow the provided dispatch strategy.
+ *
+ * @param threadName The name for the thread.
+ * @param strategy Stores the procedure for dispatching messages and how
+ * dispatch errors will be handled by the program.
+ */
+ public Dispatcher(String threadName, IDispatchingStrategy<T> strategy) {
+
+ // A FIFO ordered queue of messages to be dispatched.
+ dispatchQueue = new PriorityBlockingQueue<T>(10, new MessageComparator());
+
+ // The procedure definition for dispatching messages.
+ dispatchStrategy = strategy;
+
+ // The thread used to dispatch messages.
+ dispatchThread = new Thread(new Runnable() {
+ public void run(){
+ handleMessages();
+ }
+ });
+ dispatchThread.setName(threadName);
+ dispatchThread.setDaemon(true);
+ dispatchThread.start();
+ }
+
+ /**
+ * Interrupts the current thread process and drops the reference to that
+ * thread, creating a zombie that will eventually be cleaned up by the
+ * garbage collector.
+ */
+ public void dispose() {
+ isDisposed = true;
+
+ if(dispatchThread != null) {
+ dispatchThread.interrupt();
+ dispatchThread = null;
+ }
+ }
+
+ /**
+ * Removes the next message in the priority queue and dispatches it
+ * according to the dispatch strategy. If the message was not or could not
+ * be dispatched correctly for a reason other than a thread interruption,
+ * the error is also handled according to the dispatch strategy.
+ */
+ private void handleMessages() {
+
+ try {
+ T thisMessage;
+
+ while(isDisposed == false) {
+ // Will hold until there is a message in the queue.
+ thisMessage = dispatchQueue.take();
+ dispatchStrategy.dispatch(thisMessage);
+ }
+ } catch (InterruptedException e) {
+ // Thread was interrupted by dispose() method, no action required.
+ return;
+ } catch (Exception e) {
+ DebugTool.logError("Error occurred dispatching message.", e);
+ dispatchStrategy.handleDispatchingError("Error occurred dispatching message.", e);
+ }
+ }
+
+ /**
+ * Adds the given message to the priority queue, or if it cannot be added
+ * it handles the error according to the dispatch strategy.
+ *
+ * @param message The message to be dispatched.
+ */
+ public void queueMessage(T message) {
+ try {
+ dispatchQueue.put(message);
+ } catch(ClassCastException e) {
+ dispatchStrategy.handleQueueingError("ClassCastException encountered when queueing message.", e);
+ } catch(Exception e) {
+ dispatchStrategy.handleQueueingError("Exception encountered when queueing message.", e);
+ }
+ }
+
+ /**
+ * A private internal comparator class that will always return 0, indicating
+ * the objects being compared are the same, turning the priority queue into
+ * a FIFO queue.
+ */
+ private class MessageComparator implements Comparator<Object> {
+ @Override
+ public int compare(Object arg0, Object arg1) {
+ return 0;
+ }
+ }
+} \ No newline at end of file
diff --git a/sdl_android_lib/src/com/smartdevicelink/Dispatcher/IDispatchingStrategy.java b/sdl_android_lib/src/com/smartdevicelink/Dispatcher/IDispatchingStrategy.java
index a4cfc150b..104486659 100644
--- a/sdl_android_lib/src/com/smartdevicelink/Dispatcher/IDispatchingStrategy.java
+++ b/sdl_android_lib/src/com/smartdevicelink/Dispatcher/IDispatchingStrategy.java
@@ -1,9 +1,10 @@
-package com.smartdevicelink.Dispatcher;
+package com.smartdevicelink.dispatcher;
public interface IDispatchingStrategy<T> {
+
public void dispatch(T message);
public void handleDispatchingError(String info, Exception ex);
public void handleQueueingError(String info, Exception ex);
-}
+} \ No newline at end of file
diff --git a/sdl_android_lib/src/com/smartdevicelink/Dispatcher/IncomingProtocolMessageComparitor.java b/sdl_android_lib/src/com/smartdevicelink/Dispatcher/IncomingProtocolMessageComparitor.java
deleted file mode 100644
index fd80a5e15..000000000
--- a/sdl_android_lib/src/com/smartdevicelink/Dispatcher/IncomingProtocolMessageComparitor.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package com.smartdevicelink.Dispatcher;
-
-import java.util.Comparator;
-
-import com.smartdevicelink.protocol.ProtocolMessage;
-
-public class IncomingProtocolMessageComparitor implements Comparator<ProtocolMessage> {
-
- @Override
- public int compare(ProtocolMessage arg0, ProtocolMessage arg1) {
- // Always return 0, turning the priority queue into a FIFO queue.
- return 0;
- }
-}
diff --git a/sdl_android_lib/src/com/smartdevicelink/Dispatcher/InternalProxyMessageComparitor.java b/sdl_android_lib/src/com/smartdevicelink/Dispatcher/InternalProxyMessageComparitor.java
deleted file mode 100644
index 36e439408..000000000
--- a/sdl_android_lib/src/com/smartdevicelink/Dispatcher/InternalProxyMessageComparitor.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package com.smartdevicelink.Dispatcher;
-
-import java.util.Comparator;
-
-import com.smartdevicelink.proxy.callbacks.InternalProxyMessage;
-
-public class InternalProxyMessageComparitor implements Comparator<InternalProxyMessage> {
-
- @Override
- public int compare(InternalProxyMessage arg0, InternalProxyMessage arg1) {
- // Always return 0, turning the priority queue into a FIFO queue.
- return 0;
- }
-}
diff --git a/sdl_android_lib/src/com/smartdevicelink/Dispatcher/OutgoingProtocolMessageComparitor.java b/sdl_android_lib/src/com/smartdevicelink/Dispatcher/OutgoingProtocolMessageComparitor.java
deleted file mode 100644
index 2e3d45134..000000000
--- a/sdl_android_lib/src/com/smartdevicelink/Dispatcher/OutgoingProtocolMessageComparitor.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package com.smartdevicelink.Dispatcher;
-
-import java.util.Comparator;
-
-import com.smartdevicelink.protocol.ProtocolMessage;
-
-public class OutgoingProtocolMessageComparitor implements Comparator<ProtocolMessage> {
-
- @Override
- public int compare(ProtocolMessage arg0, ProtocolMessage arg1) {
- // Always return 0, turning the priority queue into a FIFO queue.
- return 0;
- }
-}
-
diff --git a/sdl_android_lib/src/com/smartdevicelink/Dispatcher/ProxyMessageDispatcher.java b/sdl_android_lib/src/com/smartdevicelink/Dispatcher/ProxyMessageDispatcher.java
deleted file mode 100644
index dc964fccd..000000000
--- a/sdl_android_lib/src/com/smartdevicelink/Dispatcher/ProxyMessageDispatcher.java
+++ /dev/null
@@ -1,65 +0,0 @@
-package com.smartdevicelink.Dispatcher;
-
-import java.util.Comparator;
-import java.util.concurrent.PriorityBlockingQueue;
-
-import com.smartdevicelink.util.DebugTool;
-
-public class ProxyMessageDispatcher<T> {
- PriorityBlockingQueue<T> _queue = null;
- private Thread _messageDispatchingThread = null;
- IDispatchingStrategy<T> _strategy = null;
-
- // Boolean to track if disposed
- private Boolean dispatcherDisposed = false;
-
- public ProxyMessageDispatcher(String THREAD_NAME, Comparator<T> messageComparator,
- IDispatchingStrategy<T> strategy) {
- _queue = new PriorityBlockingQueue<T>(10, messageComparator);
-
- _strategy = strategy;
-
- // Create dispatching thread
- _messageDispatchingThread = new Thread(new Runnable() {public void run(){handleMessages();}});
- _messageDispatchingThread.setName(THREAD_NAME);
- _messageDispatchingThread.setDaemon(true);
- _messageDispatchingThread.start();
- }
-
- public void dispose() {
- dispatcherDisposed = true;
-
- if(_messageDispatchingThread != null) {
- _messageDispatchingThread.interrupt();
- _messageDispatchingThread = null;
- }
- }
-
- private void handleMessages() {
-
- try {
- T thisMessage;
-
- while(dispatcherDisposed == false) {
- thisMessage = _queue.take();
- _strategy.dispatch(thisMessage);
- }
- } catch (InterruptedException e) {
- // Thread was interrupted by dispose() method, no action required
- return;
- } catch (Exception e) {
- DebugTool.logError("Error occurred dispating message.", e);
- _strategy.handleDispatchingError("Error occurred dispating message.", e);
- }
- }
-
- public void queueMessage(T message) {
- try {
- _queue.put(message);
- } catch(ClassCastException e) {
- _strategy.handleQueueingError("ClassCastException encountered when queueing message.", e);
- } catch(Exception e) {
- _strategy.handleQueueingError("Exception encountered when queueing message.", e);
- }
- }
-}
diff --git a/sdl_android_lib/src/com/smartdevicelink/proxy/SdlProxyBase.java b/sdl_android_lib/src/com/smartdevicelink/proxy/SdlProxyBase.java
index 8053d870d..2ec2047e1 100644
--- a/sdl_android_lib/src/com/smartdevicelink/proxy/SdlProxyBase.java
+++ b/sdl_android_lib/src/com/smartdevicelink/proxy/SdlProxyBase.java
@@ -35,14 +35,11 @@ import android.util.Log;
import com.smartdevicelink.proxy.RPCRequestFactory;
import com.smartdevicelink.proxy.rpc.PutFile;
-import com.smartdevicelink.Dispatcher.IDispatchingStrategy;
-import com.smartdevicelink.Dispatcher.IncomingProtocolMessageComparitor;
-import com.smartdevicelink.Dispatcher.InternalProxyMessageComparitor;
-import com.smartdevicelink.Dispatcher.OutgoingProtocolMessageComparitor;
-import com.smartdevicelink.Dispatcher.ProxyMessageDispatcher;
import com.smartdevicelink.SdlConnection.ISdlConnectionListener;
import com.smartdevicelink.SdlConnection.SdlConnection;
import com.smartdevicelink.SdlConnection.SdlSession;
+import com.smartdevicelink.dispatcher.Dispatcher;
+import com.smartdevicelink.dispatcher.IDispatchingStrategy;
import com.smartdevicelink.exception.SdlException;
import com.smartdevicelink.exception.SdlExceptionCause;
import com.smartdevicelink.marshal.JsonRPCMarshaller;
@@ -130,9 +127,9 @@ public abstract class SdlProxyBase<proxyListenerType extends IProxyListenerBase>
private TraceDeviceInfo _traceDeviceInterrogator = null;
// Declare Queuing Threads
- private ProxyMessageDispatcher<ProtocolMessage> _incomingProxyMessageDispatcher;
- private ProxyMessageDispatcher<ProtocolMessage> _outgoingProxyMessageDispatcher;
- private ProxyMessageDispatcher<InternalProxyMessage> _internalProxyMessageDispatcher;
+ private Dispatcher<ProtocolMessage> _incomingProxyMessageDispatcher;
+ private Dispatcher<ProtocolMessage> _outgoingProxyMessageDispatcher;
+ private Dispatcher<InternalProxyMessage> _internalProxyMessageDispatcher;
// Flag indicating if callbacks should be called from UIThread
private Boolean _callbackToUIThread = false;
@@ -456,8 +453,7 @@ public abstract class SdlProxyBase<proxyListenerType extends IProxyListenerBase>
_internalProxyMessageDispatcher = null;
}
- _internalProxyMessageDispatcher = new ProxyMessageDispatcher<InternalProxyMessage>("INTERNAL_MESSAGE_DISPATCHER",
- new InternalProxyMessageComparitor(),
+ _internalProxyMessageDispatcher = new Dispatcher<InternalProxyMessage>("INTERNAL_MESSAGE_DISPATCHER",
new IDispatchingStrategy<InternalProxyMessage>() {
@Override
@@ -485,8 +481,7 @@ public abstract class SdlProxyBase<proxyListenerType extends IProxyListenerBase>
_incomingProxyMessageDispatcher = null;
}
- _incomingProxyMessageDispatcher = new ProxyMessageDispatcher<ProtocolMessage>("INCOMING_MESSAGE_DISPATCHER",
- new IncomingProtocolMessageComparitor(),
+ _incomingProxyMessageDispatcher = new Dispatcher<ProtocolMessage>("INCOMING_MESSAGE_DISPATCHER",
new IDispatchingStrategy<ProtocolMessage>() {
@Override
public void dispatch(ProtocolMessage message) {
@@ -513,8 +508,7 @@ public abstract class SdlProxyBase<proxyListenerType extends IProxyListenerBase>
_outgoingProxyMessageDispatcher = null;
}
- _outgoingProxyMessageDispatcher = new ProxyMessageDispatcher<ProtocolMessage>("OUTGOING_MESSAGE_DISPATCHER",
- new OutgoingProtocolMessageComparitor(),
+ _outgoingProxyMessageDispatcher = new Dispatcher<ProtocolMessage>("OUTGOING_MESSAGE_DISPATCHER",
new IDispatchingStrategy<ProtocolMessage>() {
@Override
public void dispatch(ProtocolMessage message) {