[44] | 1 | package geniusweb.partiesserver.websocket;
|
---|
| 2 |
|
---|
| 3 | import java.util.concurrent.LinkedBlockingQueue;
|
---|
| 4 | import java.util.logging.Level;
|
---|
| 5 |
|
---|
| 6 | import javax.websocket.RemoteEndpoint.Basic;
|
---|
| 7 |
|
---|
| 8 | import tudelft.utilities.logging.Reporter;
|
---|
| 9 |
|
---|
| 10 | /**
|
---|
| 11 | * A send buffer for @ServerEndpoint objects that makes websockets asynchronous.
|
---|
| 12 | * The "asynchronous" mode for websockets can not be used because it throws
|
---|
| 13 | * exceptions the moment you try to send a second datapacket into the socket
|
---|
| 14 | * while the first is not yet finished.
|
---|
| 15 | *
|
---|
| 16 | * <p>
|
---|
| 17 | * After use, {@link #stop()} must be called to stop the thread managing this
|
---|
| 18 | * queue. Failure to do this may cause memory leaks.
|
---|
| 19 | */
|
---|
| 20 | public class SendBuffer {
|
---|
| 21 |
|
---|
| 22 | private final Basic remote;
|
---|
| 23 | /**
|
---|
| 24 | * Buffer contains jackson-serialized objects, so can be "null" or "" but
|
---|
| 25 | * not null. "" (empty string) indicates buffer should be closed and
|
---|
| 26 | * terminated.
|
---|
| 27 | */
|
---|
| 28 | private final LinkedBlockingQueue<String> fifo = new LinkedBlockingQueue<String>();
|
---|
| 29 | private final Reporter log;
|
---|
| 30 |
|
---|
| 31 | public SendBuffer(final Basic basicRemote, final Reporter logger) {
|
---|
| 32 | this.remote = basicRemote;
|
---|
| 33 | this.log = logger;
|
---|
| 34 | Thread thread = new Thread(new Runnable() {
|
---|
| 35 |
|
---|
| 36 | @Override
|
---|
| 37 | public void run() {
|
---|
| 38 | String txt = "-";
|
---|
| 39 | try {
|
---|
| 40 | while (true) {
|
---|
| 41 | txt = fifo.take();
|
---|
| 42 | if (txt.isEmpty())
|
---|
| 43 | break;
|
---|
| 44 | remote.sendText(txt);
|
---|
| 45 | }
|
---|
| 46 | } catch (InterruptedException e) {
|
---|
| 47 | // normal termination
|
---|
| 48 | log.log(Level.FINE,
|
---|
| 49 | "sendbuffer is interrupted, assuming the party died.");
|
---|
| 50 | } catch (Exception e) {
|
---|
| 51 | logger.log(Level.SEVERE,
|
---|
| 52 | "Failed to write '" + txt + "' to stream", e);
|
---|
| 53 | }
|
---|
| 54 | log.log(Level.INFO, "SendBuffer is closed.");
|
---|
| 55 | }
|
---|
| 56 | }, "SendBuffer");
|
---|
| 57 | thread.start();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | *
|
---|
| 62 | * @param text (non-emptyy) text to be sent. Typically a jackson-serialized
|
---|
| 63 | * string.
|
---|
| 64 | */
|
---|
| 65 | public void send(String text) {
|
---|
| 66 | if (fifo.contains(""))
|
---|
| 67 | return;
|
---|
| 68 | if (text == null || text.isEmpty())
|
---|
| 69 | throw new IllegalArgumentException(
|
---|
| 70 | "Text must not be null or empty");
|
---|
| 71 | try {
|
---|
| 72 | fifo.put(text);
|
---|
| 73 | } catch (InterruptedException e) {
|
---|
| 74 | log.log(Level.WARNING,
|
---|
| 75 | "sender queue has been interrupted, message may have not been sent",
|
---|
| 76 | e);
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * Stop the que, disable, terminate threads
|
---|
| 82 | */
|
---|
| 83 | public synchronized void stop() {
|
---|
| 84 | if (!fifo.contains(""))
|
---|
| 85 | try {
|
---|
| 86 | fifo.put("");
|
---|
| 87 | } catch (InterruptedException e) {
|
---|
| 88 | log.log(Level.WARNING,
|
---|
| 89 | "Queue termination was interrupted, unsure about thread state");
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | }
|
---|