public Subscription addSubscription(String destination, String subTopicId, String subscriptionId, boolean noLocal) { Subscription subscription = new Subscription(this, destination, subTopicId, subscriptionId, noLocal); Subscription present = subscriptions.putIfAbsent(subscriptionId, subscription); return (present != null ? present : subscription); }
@Override public int hashCode() { return (31 * getChannel().hashCode()) + getSubscriptionId().hashCode(); }
public void setSelector(String selector) { this.selectorText = selector; parseSelector(); }
public Message getUnsubscribeMessage() { CommandMessage unsubscribeMessage = new CommandMessage(); unsubscribeMessage.setOperation(CommandMessage.UNSUBSCRIBE_OPERATION); unsubscribeMessage.setClientId(getChannel().getId()); unsubscribeMessage.setDestination(destination); unsubscribeMessage.setHeader(AsyncMessage.SUBTOPIC_HEADER, getSubTopicId()); unsubscribeMessage.setHeader(AsyncMessage.DESTINATION_CLIENT_ID_HEADER, getSubscriptionId()); return unsubscribeMessage; }
public boolean deliver(Channel fromClient, AsyncMessage message) { if (!accept(fromClient, message)) return false; try { message.setHeader(AsyncMessage.DESTINATION_CLIENT_ID_HEADER, subscriptionId); log.debug("Channel %s deliver message to subscription %s", channel.getId(), subscriptionId); getChannel().receive(message); return true; } catch (MessageReceivingException e) { log.error(e, "Could not deliver message"); } return false; }
public void subscribe(Channel channel, String destination, String subscriptionId, String selector, boolean noLocal) { synchronized (this) { Subscription subscription = channel.addSubscription(destination, getId(), subscriptionId, noLocal); subscription.setSelector(selector); subscriptions.putIfAbsent(subscriptionId, subscription); } }
@Override public Channel removeChannel(String channelId, boolean timeout) { if (channelId == null) return null; Channel channel = (Channel)gaeCache.get(CHANNEL_PREFIX + channelId); if (channel != null) { for (Subscription subscription : channel.getSubscriptions()) { Message message = subscription.getUnsubscribeMessage(); handleMessage(channel.getFactory(), message, true); } channel.destroy(timeout); gaeCache.delete(CHANNEL_PREFIX + channelId); gaeCache.delete(GAEChannel.MSG_COUNT_PREFIX + channelId); } return channel; }
public void publish(TopicId to, Channel fromChannel, AsyncMessage msg) { int tail = to.depth()-id.depth(); switch(tail) { case 0: for (Subscription subscription : subscriptions.values()) { AsyncMessage m = msg.clone(); subscription.deliver(fromChannel, m); } break; case 1: if (wild != null) { for (Subscription subscription : wild.subscriptions.values()) { AsyncMessage m = msg.clone(); subscription.deliver(fromChannel, m); } } default: { if (wildWild != null) { for (Subscription subscription : wildWild.subscriptions.values()) { AsyncMessage m = msg.clone(); subscription.deliver(fromChannel, m); } } String next = to.getSegment(id.depth()); Topic topic = children.get(next); if (topic != null) topic.publish(to, fromChannel, msg); } } }
private void removeExpiredSubscriptions(Map<String, Subscription> subscriptions) { List<Object> channelIds = new ArrayList<Object>(subscriptions.size()); for (Subscription sub : subscriptions.values()) channelIds.add(GAEGravity.CHANNEL_PREFIX + sub.getChannel().getId()); Map<Object, Object> channels = gaeCache.getAll(channelIds); // Remove expired channel subscriptions for (Iterator<Map.Entry<String, Subscription>> ime = subscriptions.entrySet().iterator(); ime.hasNext(); ) { Map.Entry<String, Subscription> me = ime.next(); if (!channels.containsKey(GAEGravity.CHANNEL_PREFIX + me.getValue().getChannel().getId())) ime.remove(); } }
public List<Channel> getConnectedChannelsByDestination(String destination) { List<Channel> channels = new ArrayList<Channel>(); for (TimeChannel<? extends Channel> timeChannel : this.channels.values()) { if (!timeChannel.getChannel().isConnected()) continue; for (Subscription subscription : timeChannel.getChannel().getSubscriptions()) { if (destination.equals(subscription.getDestination())) { channels.add(timeChannel.getChannel()); break; } } } return channels; }
public void subscribe(Channel channel, String destination, String subscriptionId, String selector, boolean noLocal) { // How to handle cluster synchronization ??? synchronized (this) { Subscription subscription = channel.addSubscription(destination, getId(), subscriptionId, noLocal); subscription.setSelector(selector); // Handle synchronization issues ??? @SuppressWarnings("unchecked") Map<String, Subscription> subscriptions = (Map<String, Subscription>)gaeCache.get(TOPIC_PREFIX + getId()); if (subscriptions == null) subscriptions = new HashMap<String, Subscription>(); else removeExpiredSubscriptions(subscriptions); subscriptions.put(subscriptionId, subscription); gaeCache.put(TOPIC_PREFIX + getId(), subscriptions); } }
for (Subscription subscription : channel.getSubscriptions()) { try { Message message = subscription.getUnsubscribeMessage(); handleMessage(channel.getFactory(), message, true);
for (Subscription subscription : subscriptions.values()) { AsyncMessage m = msg.clone(); subscription.deliver(fromChannel, m); for (Subscription subscription : subs.values()) { AsyncMessage m = msg.clone(); subscription.deliver(fromChannel, m); for (Subscription subscription : subs.values()) { AsyncMessage m = msg.clone(); subscription.deliver(fromChannel, m);
public Set<Principal> getConnectedUsersByDestination(String destination) { Set<Principal> userPrincipals = new HashSet<Principal>(); for (TimeChannel<? extends Channel> timeChannel : this.channels.values()) { if (!timeChannel.getChannel().isConnected()) continue; for (Subscription subscription : timeChannel.getChannel().getSubscriptions()) { if (destination.equals(subscription.getDestination())) { userPrincipals.add(timeChannel.getChannel().getUserPrincipal()); break; } } } return userPrincipals; }
@Override public boolean equals(Object o) { if (!o.getClass().equals(Subscription.class)) return false; Subscription s = (Subscription)o; return getChannel().equals(s.getChannel()) && getSubscriptionId().equals(s.getSubscriptionId()); }
public Subscription addSubscription(String destination, String subTopicId, String subscriptionId, boolean noLocal) { Subscription subscription = new Subscription(this, destination, subTopicId, subscriptionId, noLocal); subscriptions.put(subscriptionId, subscription); return subscription; }
private void readObject(java.io.ObjectInputStream in) throws ClassNotFoundException, IOException { in.defaultReadObject(); if (selectorClassName != null) { try { messageSelectorConstructor = TypeUtil.getConstructor(selectorClassName, new Class<?>[] { String.class }); } catch (NoSuchMethodException e) { throw new IOException("Could not get message selector: " + selectorClassName); } } parseSelector(); }