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.

ChildEventBusManager.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2006-2014 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.util;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.events.DMDircEvent;
  25. import net.engio.mbassy.listener.Handler;
  26. import static com.google.common.base.Preconditions.checkNotNull;
  27. /**
  28. * Utility for creating and managing instances of {@link DMDircMBassador} that are slaved to a
  29. * parent bus.
  30. * <p>
  31. * Any events sent on the child bus will be propagated up to the parent bus.
  32. */
  33. public class ChildEventBusManager {
  34. private final DMDircMBassador parent;
  35. private final DMDircMBassador child;
  36. private final EventPropagator propagator;
  37. public ChildEventBusManager(final DMDircMBassador parent) {
  38. this.parent = checkNotNull(parent);
  39. this.child = new DMDircMBassador();
  40. this.propagator = new EventPropagator();
  41. }
  42. /**
  43. * Connects the child event bus to the parent.
  44. * <p>
  45. * After this method is called, all events sent to the child bus will be passed to the parent.
  46. */
  47. public void connect() {
  48. child.subscribe(propagator);
  49. }
  50. /**
  51. * Disconnects the child event bus from the parent.
  52. * <p>
  53. * The child will be disconnected asynchronously, to allow any pending async events to be
  54. * dispatched. After the bus is disconnected, no future events will be passed to the parent.
  55. */
  56. public void disconnect() {
  57. child.publishAsync(new ChildEventBusDisconnectingEvent());
  58. }
  59. /**
  60. * Gets the child bus.
  61. *
  62. * @return The child bus belonging to this manager.
  63. */
  64. public DMDircMBassador getChildBus() {
  65. return child;
  66. }
  67. private class EventPropagator {
  68. @Handler
  69. public void handleEvent(final DMDircEvent event) {
  70. if (!(event instanceof ChildEventBusDisconnectingEvent)) {
  71. // Don't propagate our private event
  72. parent.publish(event);
  73. }
  74. }
  75. // Allow all other handlers on the child bus to process this first
  76. @Handler(priority = EventUtils.PRIORITY_LOWEST)
  77. public void handleDisconnect(final ChildEventBusDisconnectingEvent event) {
  78. child.unsubscribe(propagator);
  79. }
  80. }
  81. private static class ChildEventBusDisconnectingEvent extends DMDircEvent {
  82. }
  83. }