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.

OutputQueue.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.OutputStream;
  25. import java.io.PrintWriter;
  26. import java.util.concurrent.BlockingQueue;
  27. import java.util.concurrent.PriorityBlockingQueue;
  28. /**
  29. * This class handles the Parser output Queue.
  30. *
  31. * @author shane
  32. */
  33. public class OutputQueue {
  34. /** PrintWriter for sending output. */
  35. private PrintWriter out = null;
  36. /** Is queueing enabled? */
  37. private boolean queueEnabled = true;
  38. /** The output queue! */
  39. private BlockingQueue<QueueItem> queue = new PriorityBlockingQueue<QueueItem>();
  40. /** Thread for the sending queue. */
  41. private QueueHandler queueHandler;
  42. /** The QueueFactory for this OutputQueue. */
  43. private QueueFactory queueFactory = PriorityQueueHandler.getFactory();
  44. // private QueueFactory queueFactory = SimpleRateLimitedQueueHandler.getFactory();
  45. /**
  46. * Create a new OutputQueue
  47. */
  48. public OutputQueue() { }
  49. /**
  50. * Set the output stream for this queue.
  51. *
  52. * @param outputStream Output Stream to use.
  53. */
  54. public void setOutputStream(final OutputStream outputStream) {
  55. this.out = new PrintWriter(outputStream, true);
  56. }
  57. /**
  58. * Is output queueing enabled?
  59. *
  60. * @return true if output queueing is enabled.
  61. */
  62. public boolean isQueueEnabled() {
  63. return queueEnabled;
  64. }
  65. /**
  66. * Set the QueueFactory.
  67. * Changing this will not change an existing QueueHandler unless queueing is
  68. * disabled and reenabled.
  69. * If this is called before the first lien of output is queued then there is
  70. * no need to disable and reenable the queue.
  71. *
  72. * @param manager New QueueFactory to use.
  73. */
  74. public void setQueueManager(final QueueFactory manager) {
  75. queueFactory = manager;
  76. }
  77. /**
  78. * Get the QueueFactory.
  79. * @return The current QueueFactory.
  80. */
  81. public QueueFactory getQueueManager() {
  82. return queueFactory;
  83. }
  84. /**
  85. * Set if queueing is enabled.
  86. * if this is changed from enabled to disabled, all currently queued items
  87. * will be sent immediately!
  88. *
  89. * @param queueEnabled new value for queueEnabled
  90. */
  91. public void setQueueEnabled(final boolean queueEnabled) {
  92. if (out == null) {
  93. throw new NullPointerException("No output stream has been set.");
  94. }
  95. final boolean old = this.queueEnabled;
  96. this.queueEnabled = queueEnabled;
  97. if (old != queueEnabled && old) {
  98. queueHandler.interrupt();
  99. queueHandler = null;
  100. while (!queue.isEmpty()) {
  101. try {
  102. out.printf("%s\r\n", queue.take().getLine());
  103. } catch (InterruptedException ex) {
  104. // Do nothing, we'll try again.
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * Clear the queue and stop the thread that is sending stuff.
  111. */
  112. public void clearQueue() {
  113. this.queueEnabled = false;
  114. if (queueHandler != null) {
  115. queueHandler.interrupt();
  116. queueHandler = null;
  117. }
  118. queue.clear();
  119. }
  120. /**
  121. * Get the number of items currently in the queue.
  122. *
  123. * @return Number of items in the queue.
  124. */
  125. public int queueCount() {
  126. return queue.size();
  127. }
  128. /**
  129. * Send the given line.
  130. * If queueing is enabled, this will queue it, else it will send it
  131. * immediately.
  132. *
  133. * @param line Line to send
  134. */
  135. public void sendLine(final String line) {
  136. sendLine(line, QueuePriority.NORMAL);
  137. }
  138. /**
  139. * Send the given line.
  140. * If queueing is enabled, this will queue it, else it will send it
  141. * immediately.
  142. *
  143. * @param line Line to send
  144. * @param priority Priority of item (ignored if queue is disabled)
  145. */
  146. public void sendLine(final String line, final QueuePriority priority) {
  147. if (out == null) {
  148. throw new NullPointerException("No output stream has been set.");
  149. }
  150. if (queueEnabled) {
  151. if (queueHandler == null || !queueHandler.isAlive()) {
  152. queueHandler = queueFactory.getQueueHandler(this, queue, out);
  153. queueHandler.start();
  154. }
  155. queue.add(queueHandler.getQueueItem(line, priority));
  156. } else {
  157. out.printf("%s\r\n", line);
  158. }
  159. }
  160. }