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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /** Comparator to use to sort queue items. */
  38. private final Comparator<QueueItem> comparator;
  39. /** Where to send the output. */
  40. private final PrintWriter out;
  41. /**
  42. * Create a new Queue Thread.
  43. *
  44. * @param outputQueue the OutputQueue that owns us.
  45. * @param queue Queue to handle.
  46. * @param comparator Comparator to use to sort items in the queue.
  47. * @param out Writer to send to.
  48. */
  49. public QueueHandler(
  50. final OutputQueue outputQueue,
  51. final BlockingQueue<QueueItem> queue,
  52. final Comparator<QueueItem> comparator,
  53. final PrintWriter out) {
  54. super("IRC Parser queue handler");
  55. this.queue = queue;
  56. this.comparator = comparator;
  57. this.out = out;
  58. this.outputQueue = outputQueue;
  59. }
  60. /**
  61. * Send the given item.
  62. *
  63. * @param line Line to send.
  64. */
  65. public void sendLine(final String line) {
  66. out.printf("%s\r\n", line);
  67. }
  68. /**
  69. * Get a new QueueItem for the given line and priority.
  70. * By default this will just create a new QueueItem with the given
  71. * parameters, but QueueHandlers are free to override it if they need to
  72. * instead produce subclasses of QueueItem or do anything else with the
  73. * data given.
  74. *
  75. * @param line Line to send
  76. * @param priority Priority of the line.
  77. * @return A QueueItem for teh given parameters
  78. */
  79. public QueueItem getQueueItem(final String line, final QueuePriority priority) {
  80. return new QueueItem(this, line, priority);
  81. }
  82. @Override
  83. public int compare(final QueueItem mainObject, final QueueItem otherObject) {
  84. return comparator.compare(mainObject, otherObject);
  85. }
  86. /**
  87. * Determines whether the given item has been starved (i.e., it has sat waiting in the queue
  88. * for too long due to higher priority items).
  89. *
  90. * @param item The item to check
  91. * @return True if the item has been queued for longer than 10 seconds, false otherwise
  92. */
  93. private static boolean isStarved(final QueueItem item) {
  94. return item.getTime().isBefore(LocalDateTime.now().minus(10, ChronoUnit.SECONDS));
  95. }
  96. /**
  97. * This is the main even loop of the queue.
  98. * It needs to handle pulling items out of the queue and calling
  99. * sendLine.
  100. *
  101. * It also needs to handle any delays in sending that it deems needed.
  102. */
  103. @Override
  104. public abstract void run();
  105. }