*/
package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
-import com.google.common.eventbus.AsyncEventBus;
-import com.google.common.eventbus.EventBus;
-import io.netty.channel.Channel;
-import io.netty.util.internal.ConcurrentSet;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.Preconditions;
+import java.util.HashSet;
+import java.util.Iterator;
import java.util.Set;
-import java.util.concurrent.Executors;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import java.util.concurrent.ExecutionException;
+import org.checkerframework.checker.lock.qual.GuardedBy;
+import org.checkerframework.checker.lock.qual.Holding;
+import org.opendaylight.restconf.nb.rfc8040.streams.StreamSessionHandler;
+import org.opendaylight.yangtools.concepts.Registration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
* Features of subscribing part of both notifications.
*/
abstract class AbstractCommonSubscriber extends AbstractQueryParams implements BaseListenerInterface {
-
private static final Logger LOG = LoggerFactory.getLogger(AbstractCommonSubscriber.class);
- private final Set<Channel> subscribers = new ConcurrentSet<>();
- private final EventBus eventBus;
-
- @SuppressWarnings("rawtypes")
- private EventBusChangeRecorder eventBusChangeRecorder;
- @SuppressWarnings("rawtypes")
- private ListenerRegistration registration;
-
- /**
- * Creating {@link EventBus}.
- */
- protected AbstractCommonSubscriber() {
- this.eventBus = new AsyncEventBus(Executors.newSingleThreadExecutor());
- }
+ @GuardedBy("this")
+ private final Set<StreamSessionHandler> subscribers = new HashSet<>();
+ @GuardedBy("this")
+ private Registration registration;
@Override
- public final boolean hasSubscribers() {
+ public final synchronized boolean hasSubscribers() {
return !this.subscribers.isEmpty();
}
@Override
- public final Set<Channel> getSubscribers() {
- return this.subscribers;
+ public final synchronized Set<StreamSessionHandler> getSubscribers() {
+ return new HashSet<>(this.subscribers);
}
@Override
- public final void close() throws Exception {
- this.registration.close();
- this.registration = null;
-
- deleteDataInDS();
- unregister();
- }
-
- /**
- * Creates event of type {@link EventType#REGISTER}, set {@link Channel}
- * subscriber to the event and post event into event bus.
- *
- * @param subscriber
- * Channel
- */
- public void addSubscriber(final Channel subscriber) {
- if (!subscriber.isActive()) {
- LOG.debug("Channel is not active between websocket server and subscriber {}" + subscriber.remoteAddress());
+ public final synchronized void close() throws InterruptedException, ExecutionException {
+ if (this.registration != null) {
+ this.registration.close();
+ this.registration = null;
}
- final Event event = new Event(EventType.REGISTER);
- event.setSubscriber(subscriber);
- this.eventBus.post(event);
+ deleteDataInDS().get();
+ this.subscribers.clear();
}
- /**
- * Creates event of type {@link EventType#DEREGISTER}, sets {@link Channel}
- * subscriber to the event and posts event into event bus.
- *
- * @param subscriber subscriber channel
- */
- public void removeSubscriber(final Channel subscriber) {
- LOG.debug("Subscriber {} is removed.", subscriber.remoteAddress());
- final Event event = new Event(EventType.DEREGISTER);
- event.setSubscriber(subscriber);
- this.eventBus.post(event);
+ @Override
+ public synchronized void addSubscriber(final StreamSessionHandler subscriber) {
+ final boolean isConnected = subscriber.isConnected();
+ Preconditions.checkState(isConnected);
+ LOG.debug("Subscriber {} is added.", subscriber);
+ subscribers.add(subscriber);
}
- /**
- * Sets {@link ListenerRegistration} registration.
- *
- * @param registration
- * DOMDataChangeListener registration
- */
- @SuppressWarnings("rawtypes")
- public void setRegistration(final ListenerRegistration registration) {
- this.registration = registration;
+ @Override
+ public synchronized void removeSubscriber(final StreamSessionHandler subscriber) {
+ final boolean isConnected = subscriber.isConnected();
+ Preconditions.checkState(isConnected);
+ LOG.debug("Subscriber {} is removed", subscriber);
+ subscribers.remove(subscriber);
+ if (!hasSubscribers()) {
+ ListenersBroker.getInstance().removeAndCloseListener(this);
+ }
}
/**
- * Checks if {@link ListenerRegistration} registration exist.
+ * Sets {@link Registration} registration.
*
- * @return True if exist, false otherwise.
+ * @param registration a listener registration registration.
*/
- public boolean isListening() {
- return this.registration != null;
+ @Holding("this")
+ final void setRegistration(final Registration registration) {
+ this.registration = requireNonNull(registration);
}
/**
- * Creating and registering {@link EventBusChangeRecorder} of specific
- * listener on {@link EventBus}.
+ * Checks if {@link Registration} registration exists.
*
- * @param listener
- * specific listener of notifications
+ * @return {@code true} if exists, {@code false} otherwise.
*/
- @SuppressWarnings({ "unchecked", "rawtypes" })
- protected <T extends BaseListenerInterface> void register(final T listener) {
- this.eventBusChangeRecorder = new EventBusChangeRecorder(listener);
- this.eventBus.register(this.eventBusChangeRecorder);
+ @Holding("this")
+ final boolean isListening() {
+ return registration != null;
}
/**
- * Post event to event bus.
+ * Post data to subscribed SSE session handlers.
*
- * @param event
- * data of incoming notifications
+ * @param data Data of incoming notifications.
*/
- protected void post(final Event event) {
- this.eventBus.post(event);
- }
-
- /**
- * Removes all subscribers and unregisters event bus change recorder form
- * event bus.
- */
- protected void unregister() {
- this.subscribers.clear();
- this.eventBus.unregister(this.eventBusChangeRecorder);
+ synchronized void post(final String data) {
+ final Iterator<StreamSessionHandler> iterator = subscribers.iterator();
+ while (iterator.hasNext()) {
+ final StreamSessionHandler subscriber = iterator.next();
+ final boolean isConnected = subscriber.isConnected();
+ if (isConnected) {
+ subscriber.sendDataMessage(data);
+ LOG.debug("Data was sent to subscriber {} on connection {}:", this, subscriber);
+ } else {
+ // removal is probably not necessary, because it will be removed explicitly soon after invocation of
+ // onWebSocketClosed(..) in handler; but just to be sure ...
+ iterator.remove();
+ LOG.debug("Subscriber for {} was removed - web-socket session is not open.", this);
+ }
+ }
}
}