Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.internal.node.topic;
00018
00019 import com.google.common.collect.HashMultimap;
00020 import com.google.common.collect.ImmutableList;
00021 import com.google.common.collect.Maps;
00022 import com.google.common.collect.Multimap;
00023
00024 import org.ros.namespace.GraphName;
00025 import org.ros.node.topic.Publisher;
00026 import org.ros.node.topic.Subscriber;
00027
00028 import java.util.Collection;
00029 import java.util.Map;
00030
00037 public class TopicParticipantManager {
00038
00042 private final Map<GraphName, DefaultSubscriber<?>> subscribers;
00043
00047 private final Map<GraphName, DefaultPublisher<?>> publishers;
00048
00053 private final Multimap<DefaultSubscriber<?>, PublisherIdentifier> subscriberConnections;
00054
00059 private final Multimap<DefaultPublisher<?>, SubscriberIdentifier> publisherConnections;
00060
00061
00062 private TopicParticipantManagerListener listener;
00063
00064 public TopicParticipantManager() {
00065 publishers = Maps.newConcurrentMap();
00066 subscribers = Maps.newConcurrentMap();
00067 subscriberConnections = HashMultimap.create();
00068 publisherConnections = HashMultimap.create();
00069 }
00070
00071 public void setListener(TopicParticipantManagerListener listener) {
00072 this.listener = listener;
00073 }
00074
00075 public boolean hasSubscriber(GraphName topicName) {
00076 return subscribers.containsKey(topicName);
00077 }
00078
00079 public boolean hasPublisher(GraphName topicName) {
00080 return publishers.containsKey(topicName);
00081 }
00082
00083 public DefaultPublisher<?> getPublisher(GraphName topicName) {
00084 return publishers.get(topicName);
00085 }
00086
00087 public DefaultSubscriber<?> getSubscriber(GraphName topicName) {
00088 return subscribers.get(topicName);
00089 }
00090
00091 public void addPublisher(DefaultPublisher<?> publisher) {
00092 publishers.put(publisher.getTopicName(), publisher);
00093 if (listener != null) {
00094 listener.onPublisherAdded(publisher);
00095 }
00096 }
00097
00098 public void removePublisher(DefaultPublisher<?> publisher) {
00099 publishers.remove(publisher.getTopicName());
00100 if (listener != null) {
00101 listener.onPublisherRemoved(publisher);
00102 }
00103 }
00104
00105 public void addSubscriber(DefaultSubscriber<?> subscriber) {
00106 subscribers.put(subscriber.getTopicName(), subscriber);
00107 if (listener != null) {
00108 listener.onSubscriberAdded(subscriber);
00109 }
00110 }
00111
00112 public void removeSubscriber(DefaultSubscriber<?> subscriber) {
00113 subscribers.remove(subscriber.getTopicName());
00114 if (listener != null) {
00115 listener.onSubscriberRemoved(subscriber);
00116 }
00117 }
00118
00119 public void addSubscriberConnection(DefaultSubscriber<?> subscriber,
00120 PublisherIdentifier publisherIdentifier) {
00121 subscriberConnections.put(subscriber, publisherIdentifier);
00122 }
00123
00124 public void removeSubscriberConnection(DefaultSubscriber<?> subscriber,
00125 PublisherIdentifier publisherIdentifier) {
00126 subscriberConnections.remove(subscriber, publisherIdentifier);
00127 }
00128
00129 public void addPublisherConnection(DefaultPublisher<?> publisher,
00130 SubscriberIdentifier subscriberIdentifier) {
00131 publisherConnections.put(publisher, subscriberIdentifier);
00132 }
00133
00134 public void removePublisherConnection(DefaultPublisher<?> publisher,
00135 SubscriberIdentifier subscriberIdentifier) {
00136 publisherConnections.remove(publisher, subscriberIdentifier);
00137 }
00138
00139 public Collection<DefaultSubscriber<?>> getSubscribers() {
00140 return ImmutableList.copyOf(subscribers.values());
00141 }
00142
00143 public Collection<PublisherIdentifier> getSubscriberConnections(DefaultSubscriber<?> subscriber) {
00144 return ImmutableList.copyOf(subscriberConnections.get(subscriber));
00145 }
00146
00147 public Collection<DefaultPublisher<?>> getPublishers() {
00148 return ImmutableList.copyOf(publishers.values());
00149 }
00150
00151 public Collection<SubscriberIdentifier> getPublisherConnections(DefaultPublisher<?> publisher) {
00152 return ImmutableList.copyOf(publisherConnections.get(publisher));
00153 }
00154 }