You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

QueueHandler.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.irc.outputqueue;
  23. import com.dmdirc.parser.common.QueuePriority;
  24. import java.io.PrintWriter;
  25. import java.time.LocalDateTime;
  26. import java.time.temporal.ChronoUnit;
  27. import java.util.Comparator;
  28. import java.util.concurrent.BlockingQueue;
  29. /**
  30. * Sending queue.
  31. */
  32. public abstract class QueueHandler extends Thread implements Comparator<QueueItem> {
  33. /** Queue we are handling. */
  34. protected final BlockingQueue<QueueItem> queue;
  35. /** The output queue that owns us. */
  36. protected OutputQueue outputQueue;
  37. /** Where to send the output. */
  38. private final PrintWriter out;
  39. /**
  40. * Create a new Queue Thread.
  41. *
  42. * @param outputQueue the OutputQueue that owns us.
  43. * @param queue Queue to handle
  44. * @param out Writer to send to.
  45. */
  46. public QueueHandler(final OutputQueue outputQueue, final BlockingQueue<QueueItem> queue, final PrintWriter out) {
  47. super("IRC Parser queue handler");
  48. this.queue = queue;
  49. this.out = out;
  50. this.outputQueue = outputQueue;
  51. }
  52. /**
  53. * Send the given item.
  54. *
  55. * @param line Line to send.
  56. */
  57. public void sendLine(final String line) {
  58. out.printf("%s\r\n", line);
  59. }
  60. /**
  61. * Get a new QueueItem for the given line and priority.
  62. * By default this will just create a new QueueItem with the given
  63. * parameters, but QueueHandlers are free to override it if they need to
  64. * instead produce subclasses of QueueItem or do anything else with the
  65. * data given.
  66. *
  67. * @param line Line to send
  68. * @param priority Priority of the line.
  69. * @return A QueueItem for teh given parameters
  70. */
  71. public QueueItem getQueueItem(final String line, final QueuePriority priority) {
  72. return new QueueItem(this, line, priority);
  73. }
  74. /**
  75. * Compare two QueueItems for sorting purposes.
  76. * This is called by the default QueueItem in its compareTo method. The
  77. * calling object will be the first parameter, the object to compare it to
  78. * will be second.
  79. * This allows QueueHandlers to sort items differently if needed.
  80. *
  81. * The default implementation works as follows:
  82. * Compare based on priorty firstly, if the priorities are the same,
  83. * compare based on the order the items were added to the queue.
  84. *
  85. * If an item has been in the queue longer than 10 seconds, it will not
  86. * check its priority and soley position itself based on adding order.
  87. *
  88. * @param mainObject Main object we are comparing against.
  89. * @param otherObject Object we are comparing to.
  90. * @return A QueueItem for teh given parameters
  91. */
  92. @Override
  93. public int compare(final QueueItem mainObject, final QueueItem otherObject) {
  94. if (!isStarved(mainObject)
  95. && mainObject.getPriority().compareTo(otherObject.getPriority()) != 0) {
  96. return mainObject.getPriority().compareTo(otherObject.getPriority());
  97. }
  98. if (mainObject.getItemNumber() > otherObject.getItemNumber()) {
  99. return 1;
  100. } else if (mainObject.getItemNumber() < otherObject.getItemNumber()) {
  101. return -1;
  102. } else {
  103. // This can't happen.
  104. return 0;
  105. }
  106. }
  107. /**
  108. * Determines whether the given item has been starved (i.e., it has sat waiting in the queue
  109. * for too long due to higher priority items).
  110. *
  111. * @param item The item to check
  112. * @return True if the item has been queued for longer than 10 seconds, false otherwise
  113. */
  114. private static boolean isStarved(final QueueItem item) {
  115. return item.getTime().isBefore(LocalDateTime.now().minus(10, ChronoUnit.SECONDS));
  116. }
  117. /**
  118. * This is the main even loop of the queue.
  119. * It needs to handle pulling items out of the queue and calling
  120. * sendLine.
  121. *
  122. * It also needs to handle any delays in sending that it deems needed.
  123. */
  124. @Override
  125. public abstract void run();
  126. }