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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 factory New QueueFactory to use.
  73. */
  74. public void setQueueFactory(final QueueFactory factory) {
  75. queueFactory = factory;
  76. }
  77. /**
  78. * Get the QueueFactory.
  79. *
  80. * @return The current QueueFactory.
  81. */
  82. public QueueFactory getQueueManager() {
  83. return queueFactory;
  84. }
  85. /**
  86. * Get the QueueHandler.
  87. *
  88. * @return The current QueueHandler if there is one, else null.
  89. */
  90. public QueueHandler getQueueHandler() {
  91. return queueHandler;
  92. }
  93. /**
  94. * Set if queueing is enabled.
  95. * if this is changed from enabled to disabled, all currently queued items
  96. * will be sent immediately!
  97. *
  98. * @param queueEnabled new value for queueEnabled
  99. */
  100. public void setQueueEnabled(final boolean queueEnabled) {
  101. if (out == null) {
  102. throw new NullPointerException("No output stream has been set.");
  103. }
  104. final boolean old = this.queueEnabled;
  105. this.queueEnabled = queueEnabled;
  106. if (old != queueEnabled && old) {
  107. queueHandler.interrupt();
  108. queueHandler = null;
  109. while (!queue.isEmpty()) {
  110. try {
  111. out.printf("%s\r\n", queue.take().getLine());
  112. } catch (InterruptedException ex) {
  113. // Do nothing, we'll try again.
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. * Clear the queue and stop the thread that is sending stuff.
  120. */
  121. public void clearQueue() {
  122. this.queueEnabled = false;
  123. if (queueHandler != null) {
  124. queueHandler.interrupt();
  125. queueHandler = null;
  126. }
  127. queue.clear();
  128. }
  129. /**
  130. * Get the number of items currently in the queue.
  131. *
  132. * @return Number of items in the queue.
  133. */
  134. public int queueCount() {
  135. return queue.size();
  136. }
  137. /**
  138. * Send the given line.
  139. * If queueing is enabled, this will queue it, else it will send it
  140. * immediately.
  141. *
  142. * @param line Line to send
  143. */
  144. public void sendLine(final String line) {
  145. sendLine(line, QueuePriority.NORMAL);
  146. }
  147. /**
  148. * Send the given line.
  149. * If queueing is enabled, this will queue it, else it will send it
  150. * immediately.
  151. *
  152. * @param line Line to send
  153. * @param priority Priority of item (ignored if queue is disabled)
  154. */
  155. public void sendLine(final String line, final QueuePriority priority) {
  156. if (out == null) {
  157. throw new NullPointerException("No output stream has been set.");
  158. }
  159. if (queueEnabled) {
  160. if (queueHandler == null || !queueHandler.isAlive()) {
  161. queueHandler = queueFactory.getQueueHandler(this, queue, out);
  162. queueHandler.start();
  163. }
  164. queue.add(queueHandler.getQueueItem(line, priority));
  165. } else {
  166. out.printf("%s\r\n", line);
  167. }
  168. }
  169. }