/** * Unsubscribe the provided Subscription * * @param subscription */ public void unsubscribe(DynSubscription subscription) { if (subscription.isConnected()) { subscription.publication.removeSubscription(subscription); subscription.publication.publisher.subscriptionDisconnected(name, subscription); subscription.set(null); } subscriptions.remove(subscription); }
/** * Subscribe and try to connect the provided Subscription. * * <p>Careful: Calling this method does NOT guarantee to have a valid connection to a publication. * It only means, that the request to subscribe was registered and it tries to connect if there is * a matching publication. * * <p>VERY Careful: If a valid connection can be established this method calls directly the * Subscribers publicationConnected() method and returns once that method returns. * * @param subscription * @return true if a successful connection to a publication could be established */ public boolean subscribe(DynSubscription subscription) { if (!subscriptions.contains(subscription)) { subscriptions.add(subscription); if (!subscription.isConnected()) { Iterator<DynPublication> i = publications.iterator(); while (i.hasNext()) { DynPublication publication = i.next(); if (subscription.equalsIdentifier(publication)) { subscription.set(publication); publication.addSubscription(subscription); subscription.subscriber.publicationConnected(name, subscription); publication.publisher.subscriptionConnected(name, subscription); return true; } } } } return false; }
/** * Publish the provided Publication * * <p>Careful: Calling this method does NOT guarantee to have a valid connection to a * subscription. It only means, that the publication is registered and it tries to connect if * there is a matching subscription. * * <p>VERY Careful: If a valid connection can be established this method calls directly the * Publishers subscriptionConnected() method and returns once that method returns. * * @param publication * @throws DynException if the Publications identifier has already been taken */ public void publish(DynPublication publication) throws DynException { // Debugger.debug(getClass(), "try to publish publication: " + publication.getIdentifier()); // First check if this pointers name has been already taken Iterator<DynPublication> p = publications.iterator(); while (p.hasNext()) { if (p.next().identifier.equals(publication.identifier)) throw new DynException("Publication name already taken: " + publication.identifier); } publications.add(publication); Iterator<DynSubscription> i = subscriptions.iterator(); while (i.hasNext()) { DynSubscription subscription = i.next(); if (!subscription.isConnected() && subscription.equalsIdentifier(publication)) { publication.addSubscription(subscription); subscription.set(publication); publication.publisher.subscriptionConnected(name, subscription); subscription.subscriber.publicationConnected(name, subscription); } } }