]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/UpdateMessageBroker.java
Version 18.1, September 2015
[GpsPrune.git] / tim / prune / UpdateMessageBroker.java
index b584ed88384249eb5a639e8dfec993b6c61b4df8..16599735a80fc831ede28f72241f1310f42a3bb7 100644 (file)
@@ -6,11 +6,11 @@ package tim.prune;
  */
 public abstract class UpdateMessageBroker
 {
-       private static final int MAXIMUM_NUMBER_SUBSCRIBERS = 7;
+       private static final int MAXIMUM_NUMBER_SUBSCRIBERS = 8;
        /** Array of all subscribers */
        private static DataSubscriber[] _subscribers = new DataSubscriber[MAXIMUM_NUMBER_SUBSCRIBERS];
-       /** Counter of the number of subscribers added so far */
-       private static int _subscriberNum = 0;
+       /** Index from which to start looking for an empty slot*/
+       private static int _searchStartIndex = 0;
        /** Enable/disabled flag */
        private static boolean _enabled = true;
 
@@ -21,8 +21,33 @@ public abstract class UpdateMessageBroker
         */
        public static void addSubscriber(DataSubscriber inSub)
        {
-               _subscribers[_subscriberNum] = inSub;
-               _subscriberNum++;
+               // Loop looking for first null entry
+               for (int i=_searchStartIndex; i<MAXIMUM_NUMBER_SUBSCRIBERS; i++)
+               {
+                       if (_subscribers[i] == null)
+                       {
+                               _subscribers[i] = inSub;
+                               _searchStartIndex = i+1;
+                               break;
+                       }
+               }
+       }
+
+       /**
+        * Remove the given subscriber from the list
+        * @param inSub subscriber to remove
+        */
+       public static void removeSubscriber(DataSubscriber inSub)
+       {
+               for (int i=0; i<MAXIMUM_NUMBER_SUBSCRIBERS; i++)
+               {
+                       if (_subscribers[i] == inSub)
+                       {
+                               _subscribers[i] = null;
+                               // Could break out of the loop here but we want to make sure we remove all of them
+                       }
+               }
+               _searchStartIndex = 0; // for the next add, start from beginning to ensure all gaps are filled
        }
 
        /**