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.

WebSocketController.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_web2;
  18. import com.dmdirc.events.eventbus.EventBus;
  19. import java.io.IOException;
  20. import java.util.Collection;
  21. import java.util.concurrent.CopyOnWriteArrayList;
  22. import java.util.concurrent.atomic.AtomicBoolean;
  23. import javax.inject.Inject;
  24. import javax.inject.Singleton;
  25. import org.eclipse.jetty.websocket.api.Session;
  26. /**
  27. * Manages events raised by the {@link WebSocketHandler}.
  28. *
  29. * <p>This serves as a bridge between the {@link WebSocketHandler}, which cannot have dependencies
  30. * passed in sanely due to the framework, and the rest of the plugin/client.
  31. */
  32. @Singleton
  33. public class WebSocketController {
  34. private final Collection<Session> sessions = new CopyOnWriteArrayList<>();
  35. private final Object sessionLock = new Object();
  36. private final AtomicBoolean subscribed = new AtomicBoolean(false);
  37. private final EventBus eventBus;
  38. private final InitialStateProducer initialStateProducer;
  39. @Inject
  40. public WebSocketController(final EventBus eventBus, final InitialStateProducer initialStateProducer) {
  41. this.eventBus = eventBus;
  42. this.initialStateProducer = initialStateProducer;
  43. }
  44. /**
  45. * Handles a session connected event raised by a {@link WebSocketHandler}.
  46. *
  47. * @param session The session that is now connected.
  48. */
  49. void sessionConnected(final Session session) {
  50. synchronized (sessionLock) {
  51. if (!subscribed.getAndSet(true)) {
  52. eventBus.subscribe(this);
  53. }
  54. sessions.add(session);
  55. }
  56. sendMessage(session, initialStateProducer.getInitialState());
  57. }
  58. /**
  59. * Handles a session closed event raised by a {@link WebSocketHandler}.
  60. *
  61. * @param session The session that is now closed.
  62. * @param statusCode The status code returned.
  63. * @param reason The reason for quitting.
  64. */
  65. void sessionClosed(final Session session, final int statusCode, final String reason) {
  66. synchronized (sessionLock) {
  67. sessions.remove(session);
  68. if (sessions.isEmpty() && subscribed.getAndSet(false)) {
  69. eventBus.unsubscribe(this);
  70. }
  71. }
  72. }
  73. /**
  74. * Handles a message received event raised by a {@link WebSocketHandler}.
  75. *
  76. * @param session The session that the message is sent on.
  77. * @param message The message that was received.
  78. */
  79. void messageReceived(final Session session, final String message) {
  80. // Echo the message back for testing
  81. sendMessage(session, message);
  82. }
  83. /**
  84. * Sends a message to a specific session.
  85. *
  86. * @param session The session to send a message to.
  87. * @param message The message to be sent.
  88. */
  89. private void sendMessage(final Session session, final String message) {
  90. try {
  91. WebSocketHandler.sendMessage(session, message);
  92. } catch (IOException ex) {
  93. // TODO: Raise an error...
  94. }
  95. }
  96. /**
  97. * Sends a message to all connected sessions.
  98. *
  99. * @param message The message to be sent.
  100. */
  101. private void sendMessage(final String message) {
  102. sessions.forEach(s -> sendMessage(s, message));
  103. }
  104. }