Browse Source

Merge pull request #120 from csmith/master

Use proper dates in QueueItems, fix broken logic.
pull/121/head
Greg Holmes 8 years ago
parent
commit
6c497351f1

+ 15
- 1
irc/src/com/dmdirc/parser/irc/outputqueue/QueueHandler.java View File

25
 import com.dmdirc.parser.common.QueuePriority;
25
 import com.dmdirc.parser.common.QueuePriority;
26
 
26
 
27
 import java.io.PrintWriter;
27
 import java.io.PrintWriter;
28
+import java.time.LocalDateTime;
29
+import java.time.temporal.ChronoUnit;
28
 import java.util.Comparator;
30
 import java.util.Comparator;
29
 import java.util.concurrent.BlockingQueue;
31
 import java.util.concurrent.BlockingQueue;
30
 
32
 
99
      */
101
      */
100
     @Override
102
     @Override
101
     public int compare(final QueueItem mainObject, final QueueItem otherObject) {
103
     public int compare(final QueueItem mainObject, final QueueItem otherObject) {
102
-        if (mainObject.getTime() < 10 * 1000 && mainObject.getPriority().compareTo(otherObject.getPriority()) != 0) {
104
+        if (!isStarved(mainObject)
105
+                && mainObject.getPriority().compareTo(otherObject.getPriority()) != 0) {
103
             return mainObject.getPriority().compareTo(otherObject.getPriority());
106
             return mainObject.getPriority().compareTo(otherObject.getPriority());
104
         }
107
         }
105
 
108
 
113
         }
116
         }
114
     }
117
     }
115
 
118
 
119
+    /**
120
+     * Determines whether the given item has been starved (i.e., it has sat waiting in the queue
121
+     * for too long due to higher priority items).
122
+     *
123
+     * @param item The item to check
124
+     * @return True if the item has been queued for longer than 10 seconds, false otherwise
125
+     */
126
+    private static boolean isStarved(final QueueItem item) {
127
+        return item.getTime().isBefore(LocalDateTime.now().minus(10, ChronoUnit.SECONDS));
128
+    }
129
+
116
     /**
130
     /**
117
      * This is the main even loop of the queue.
131
      * This is the main even loop of the queue.
118
      * It needs to handle pulling items out of the queue and calling
132
      * It needs to handle pulling items out of the queue and calling

+ 7
- 5
irc/src/com/dmdirc/parser/irc/outputqueue/QueueItem.java View File

24
 
24
 
25
 import com.dmdirc.parser.common.QueuePriority;
25
 import com.dmdirc.parser.common.QueuePriority;
26
 
26
 
27
+import java.time.LocalDateTime;
28
+
27
 /**
29
 /**
28
  * Queued Item.
30
  * Queued Item.
29
  */
31
  */
34
     /** Line to send. */
36
     /** Line to send. */
35
     private final String line;
37
     private final String line;
36
     /** Time this line was added. */
38
     /** Time this line was added. */
37
-    private final long time;
39
+    private final LocalDateTime time;
38
     /** Item Number. */
40
     /** Item Number. */
39
     private final long itemNumber;
41
     private final long itemNumber;
40
     /** What is the priority of this line? */
42
     /** What is the priority of this line? */
54
         this.line = line;
56
         this.line = line;
55
         this.priority = priority;
57
         this.priority = priority;
56
 
58
 
57
-        this.time = System.currentTimeMillis();
59
+        this.time = LocalDateTime.now();
58
         this.itemNumber = number++;
60
         this.itemNumber = number++;
59
     }
61
     }
60
 
62
 
68
     }
70
     }
69
 
71
 
70
     /**
72
     /**
71
-     * Get the value of time.
73
+     * Gets the time at which this item was queued.
72
      *
74
      *
73
-     * @return the value of time
75
+     * @return the time this item was queued.
74
      */
76
      */
75
-    public long getTime() {
77
+    public LocalDateTime getTime() {
76
         return time;
78
         return time;
77
     }
79
     }
78
 
80
 

Loading…
Cancel
Save