Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

OutputQueue.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.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. public class OutputQueue {
  32. /** PrintWriter for sending output. */
  33. private PrintWriter out;
  34. /** Is queueing enabled? */
  35. private boolean queueEnabled = true;
  36. /** Are we discarding all futher input? */
  37. private boolean discarding = false;
  38. /** The output queue! */
  39. private final BlockingQueue<QueueItem> queue = new PriorityBlockingQueue<>();
  40. /** Object for synchronising access to the {@link #queueHandler}. */
  41. private final Object queueHandlerLock = new Object();
  42. /** Thread for the sending queue. */
  43. private QueueHandler queueHandler;
  44. /** The QueueHandlerFactory for this OutputQueue. */
  45. private final QueueHandlerFactory queueHandlerFactory;
  46. /**
  47. * Creates a new output queue using the default handler.
  48. */
  49. public OutputQueue() {
  50. this(PriorityQueueHandler.getFactory());
  51. }
  52. /**
  53. * Creates a new output queue using a handler from the given factory.
  54. *
  55. * @param queueHandlerFactory The factory to use to create {@link QueueHandler}s.
  56. */
  57. public OutputQueue(final QueueHandlerFactory queueHandlerFactory) {
  58. this.queueHandlerFactory = queueHandlerFactory;
  59. }
  60. /**
  61. * Set the output stream for this queue.
  62. *
  63. * @param outputStream Output Stream to use.
  64. */
  65. public void setOutputStream(final OutputStream outputStream) {
  66. this.out = new PrintWriter(outputStream, true);
  67. }
  68. /**
  69. * Is output queueing enabled?
  70. *
  71. * @return true if output queueing is enabled.
  72. */
  73. public boolean isQueueEnabled() {
  74. return queueEnabled;
  75. }
  76. /**
  77. * Get the QueueHandler.
  78. *
  79. * @return The current QueueHandler if there is one, else null.
  80. */
  81. public QueueHandler getQueueHandler() {
  82. return queueHandler;
  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 the new value is not the same as the old one, and we used to be enabled
  98. // then flush the queue.
  99. if (old != queueEnabled && old) {
  100. synchronized (queueHandlerLock) {
  101. if (queueHandler != null) {
  102. queueHandler.interrupt();
  103. queueHandler = null;
  104. }
  105. }
  106. while (!queue.isEmpty()) {
  107. try {
  108. out.printf("%s\r\n", queue.take().getLine());
  109. } catch (InterruptedException ex) {
  110. // Do nothing, we'll try again.
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. * Direct access to the queue of items waiting to be sent.
  117. *
  118. * @return This queue's backing queue.
  119. */
  120. public BlockingQueue<QueueItem> getQueue() {
  121. return queue;
  122. }
  123. /**
  124. * Should we be discarding?
  125. *
  126. * @param newValue true to enable discarding.
  127. */
  128. public void setDiscarding(final boolean newValue) {
  129. discarding = newValue;
  130. }
  131. /**
  132. * Are we discarding?
  133. *
  134. * @return true if discarding
  135. */
  136. public boolean isDiscarding() {
  137. return discarding;
  138. }
  139. /**
  140. * Clear the queue and stop the thread that is sending stuff.
  141. */
  142. public void clearQueue() {
  143. this.queueEnabled = false;
  144. synchronized (queueHandlerLock) {
  145. if (queueHandler != null) {
  146. queueHandler.interrupt();
  147. queueHandler = null;
  148. }
  149. }
  150. queue.clear();
  151. }
  152. /**
  153. * Get the number of items currently in the queue.
  154. *
  155. * @return Number of items in the queue.
  156. */
  157. public int queueCount() {
  158. return queue.size();
  159. }
  160. /**
  161. * Send the given line.
  162. * If queueing is enabled, this will queue it, else it will send it
  163. * immediately.
  164. *
  165. * @param line Line to send
  166. */
  167. public void sendLine(final String line) {
  168. sendLine(line, QueuePriority.NORMAL);
  169. }
  170. /**
  171. * Send the given line.
  172. * If queueing is enabled, this will queue it, else it will send it
  173. * immediately.
  174. *
  175. * @param line Line to send
  176. * @param priority Priority of item (ignored if queue is disabled)
  177. */
  178. public void sendLine(final String line, final QueuePriority priority) {
  179. if (discarding) { return; }
  180. if (out == null) {
  181. throw new NullPointerException("No output stream has been set.");
  182. }
  183. if (queueEnabled && priority != QueuePriority.IMMEDIATE) {
  184. synchronized (queueHandlerLock) {
  185. if (queueHandler == null || !queueHandler.isAlive()) {
  186. queueHandler = queueHandlerFactory.getQueueHandler(this, out);
  187. queueHandler.start();
  188. }
  189. queue.add(queueHandler.getQueueItem(line, priority));
  190. }
  191. } else {
  192. out.printf("%s\r\n", line);
  193. }
  194. }
  195. }