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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.addons.ui_web2;
  23. import com.dmdirc.interfaces.EventBus;
  24. import java.io.IOException;
  25. import java.util.Collection;
  26. import java.util.concurrent.CopyOnWriteArrayList;
  27. import java.util.concurrent.atomic.AtomicBoolean;
  28. import javax.inject.Inject;
  29. import javax.inject.Singleton;
  30. import org.eclipse.jetty.websocket.api.Session;
  31. /**
  32. * Manages events raised by the {@link WebSocketHandler}.
  33. *
  34. * <p>This serves as a bridge between the {@link WebSocketHandler}, which cannot have dependencies
  35. * passed in sanely due to the framework, and the rest of the plugin/client.
  36. */
  37. @Singleton
  38. public class WebSocketController {
  39. private final Collection<Session> sessions = new CopyOnWriteArrayList<>();
  40. private final Object sessionLock = new Object();
  41. private final AtomicBoolean subscribed = new AtomicBoolean(false);
  42. private final EventBus eventBus;
  43. private final InitialStateProducer initialStateProducer;
  44. @Inject
  45. public WebSocketController(final EventBus eventBus, final InitialStateProducer initialStateProducer) {
  46. this.eventBus = eventBus;
  47. this.initialStateProducer = initialStateProducer;
  48. }
  49. /**
  50. * Handles a session connected event raised by a {@link WebSocketHandler}.
  51. *
  52. * @param session The session that is now connected.
  53. */
  54. void sessionConnected(final Session session) {
  55. synchronized (sessionLock) {
  56. if (!subscribed.getAndSet(true)) {
  57. eventBus.subscribe(this);
  58. }
  59. sessions.add(session);
  60. }
  61. sendMessage(session, initialStateProducer.getInitialState());
  62. }
  63. /**
  64. * Handles a session closed event raised by a {@link WebSocketHandler}.
  65. *
  66. * @param session The session that is now closed.
  67. * @param statusCode The status code returned.
  68. * @param reason The reason for quitting.
  69. */
  70. void sessionClosed(final Session session, final int statusCode, final String reason) {
  71. synchronized (sessionLock) {
  72. sessions.remove(session);
  73. if (sessions.isEmpty() && subscribed.getAndSet(false)) {
  74. eventBus.unsubscribe(this);
  75. }
  76. }
  77. }
  78. /**
  79. * Handles a message received event raised by a {@link WebSocketHandler}.
  80. *
  81. * @param session The session that the message is sent on.
  82. * @param message The message that was received.
  83. */
  84. void messageReceived(final Session session, final String message) {
  85. // Echo the message back for testing
  86. sendMessage(session, message);
  87. }
  88. /**
  89. * Sends a message to a specific session.
  90. *
  91. * @param session The session to send a message to.
  92. * @param message The message to be sent.
  93. */
  94. private void sendMessage(final Session session, final String message) {
  95. try {
  96. WebSocketHandler.sendMessage(session, message);
  97. } catch (IOException ex) {
  98. // TODO: Raise an error...
  99. }
  100. }
  101. /**
  102. * Sends a message to all connected sessions.
  103. *
  104. * @param message The message to be sent.
  105. */
  106. private void sendMessage(final String message) {
  107. sessions.forEach(s -> sendMessage(s, message));
  108. }
  109. }