22
33import org .apache .commons .logging .Log ;
44import org .apache .commons .logging .LogFactory ;
5-
65import org .ros .concurrent .ListenerGroup ;
76import org .ros .concurrent .SignalRunnable ;
87import org .ros .internal .node .server .NodeIdentifier ;
2423import java .util .concurrent .TimeUnit ;
2524
2625/**
27- * Default implementation of a {@link Subscriber}.
26+ * Default implementation of a {@link Subscriber}.<br/>
27+ * Primary players are knownPublishers, which is a Set of PublisherIdentifiers,<br/>
28+ * and TcpClientManager, which has the NamedChannelHandlers.<br/>
29+ * Here, we also maintain the incomingMessageQueue, which contains MessageListeners of the type this
30+ * class is parameterized with.<br/>
31+ *
2832 *
2933 * @author jg
3034 */
3135public class DefaultSubscriber <T > extends DefaultTopicParticipant implements Subscriber <T > {
32-
33- private static final Log log = LogFactory .getLog (DefaultSubscriber .class );
36+ private static boolean DEBUG = true ;
37+ private static final Log log = LogFactory .getLog (DefaultSubscriber .class );
3438
3539 /**
3640 * The maximum delay before shutdown will begin even if all
@@ -43,27 +47,35 @@ public class DefaultSubscriber<T> extends DefaultTopicParticipant implements Sub
4347 private final NodeIdentifier nodeIdentifier ;
4448 private final ScheduledExecutorService executorService ;
4549 private final IncomingMessageQueue <T > incomingMessageQueue ;
46- private final Set <PublisherIdentifier > knownPublishers ;
50+ // private final Set<PublisherIdentifier> knownPublishers;
4751 private final TcpClientManager tcpClientManager ;
52+ private final TopicParticipantManager topicParticipantManager ;
4853 private final Object mutex ;
4954
5055 /**
5156 * Manages the {@link SubscriberListener}s for this {@link Subscriber}.
5257 */
5358 private final ListenerGroup <SubscriberListener <T >> subscriberListeners ;
5459
60+ //public static <S> DefaultSubscriber<S> newDefault(NodeIdentifier nodeIdentifier,
61+ // TopicDeclaration description, ScheduledExecutorService executorService) throws IOException {
62+ // return new DefaultSubscriber<S>(nodeIdentifier, description, executorService);
63+ //}
5564 public static <S > DefaultSubscriber <S > newDefault (NodeIdentifier nodeIdentifier ,
56- TopicDeclaration description , ScheduledExecutorService executorService ) throws IOException {
57- return new DefaultSubscriber <S >(nodeIdentifier , description , executorService );
65+ TopicDeclaration description ,
66+ TopicParticipantManager topicParticipantManager ,
67+ ScheduledExecutorService executorService ) throws IOException {
68+ return new DefaultSubscriber <S >(nodeIdentifier , description , topicParticipantManager , executorService );
5869 }
59-
60- private DefaultSubscriber ( NodeIdentifier nodeIdentifier , TopicDeclaration topicDeclaration , ScheduledExecutorService executorService ) throws IOException {
70+ private DefaultSubscriber ( NodeIdentifier nodeIdentifier , TopicDeclaration topicDeclaration ,
71+ TopicParticipantManager topicParticipantManager , ScheduledExecutorService executorService ) throws IOException {
6172 super (topicDeclaration );
6273 this .nodeIdentifier = nodeIdentifier ;
6374 this .executorService = executorService ;
6475 incomingMessageQueue = new IncomingMessageQueue <T >(executorService );
65- knownPublishers = new HashSet <PublisherIdentifier >();
76+ // knownPublishers = new HashSet<PublisherIdentifier>();
6677 tcpClientManager = TcpClientManager .getInstance (executorService );
78+ this .topicParticipantManager = topicParticipantManager ;
6779 mutex = new Object ();
6880 SubscriberHandshakeHandler <T > subscriberHandshakeHandler =
6981 new SubscriberHandshakeHandler <T >(toDeclaration ().toConnectionHeader (),
@@ -73,22 +85,26 @@ private DefaultSubscriber(NodeIdentifier nodeIdentifier, TopicDeclaration topicD
7385 subscriberListeners .add (new DefaultSubscriberListener <T >() {
7486 @ Override
7587 public void onMasterRegistrationSuccess (Subscriber <T > registrant ) {
76- log .info ("Subscriber registered: " + DefaultSubscriber .this );
88+ if (DEBUG )
89+ log .info ("Subscriber registered: " + DefaultSubscriber .this );
7790 }
7891
7992 @ Override
8093 public void onMasterRegistrationFailure (Subscriber <T > registrant ) {
81- log .info ("Subscriber registration failed: " + DefaultSubscriber .this );
94+ if (DEBUG )
95+ log .info ("Subscriber registration failed: " + DefaultSubscriber .this );
8296 }
8397
8498 @ Override
8599 public void onMasterUnregistrationSuccess (Subscriber <T > registrant ) {
86- log .info ("Subscriber unregistered: " + DefaultSubscriber .this );
100+ if (DEBUG )
101+ log .info ("Subscriber unregistered: " + DefaultSubscriber .this );
87102 }
88103
89104 @ Override
90105 public void onMasterUnregistrationFailure (Subscriber <T > registrant ) {
91- log .info ("Subscriber unregistration failed: " + DefaultSubscriber .this );
106+ if (DEBUG )
107+ log .info ("Subscriber unregistration failed: " + DefaultSubscriber .this );
92108 }
93109 });
94110 }
@@ -119,27 +135,44 @@ public void addMessageListener(MessageListener<T> messageListener, int limit) {
119135 public void addMessageListener (MessageListener <T > messageListener ) {
120136 addMessageListener (messageListener , 1 );
121137 }
122-
123-
138+ /**
139+ * When the SlaveClient requests a topic from the publisher in UpdatePublisherRunnable, as
140+ * happens when the method updatePublishers is called here, this method is called back on reply from master.
141+ * TcpClientManager calls connect to the passed InetSocketAddress. After that, all the SubscriberListeners are
142+ * signaled with the new Publisher.
143+ * @param publisherIdentifier
144+ * @param address
145+ * @throws Exception
146+ */
124147 public void addPublisher (PublisherIdentifier publisherIdentifier , InetSocketAddress address ) throws Exception {
125148 synchronized (mutex ) {
126149 // TODO(damonkohler): If the connection is dropped, knownPublishers should
127150 // be updated.
128- if (knownPublishers .contains (publisherIdentifier )) {
129- return ;
130- }
131- tcpClientManager .connect (toString (), address );
151+ //if (knownPublishers.contains(publisherIdentifier)) {
152+ // return;
153+ //}
154+ Collection <PublisherIdentifier > pubs = topicParticipantManager .getSubscriberConnections (this );
155+ if (pubs != null && pubs .contains (publisherIdentifier )) {
156+ log .info ("Defaultsubscriber addPublisher topicParticipantManager CONTAINS " +publisherIdentifier +" at " +address );
157+ } else {
158+ log .info ("Defaultsubscriber addPublisher topicParticipantManager DOES NOT CONTAIN " +publisherIdentifier +" at " +address );
159+ topicParticipantManager .addSubscriberConnection (this , publisherIdentifier );
160+ }
161+ tcpClientManager .connect (toString (), address );
132162 // TODO(damonkohler): knownPublishers is duplicate information that is
133163 // already available to the TopicParticipantManager.
134- knownPublishers .add (publisherIdentifier );
164+ // knownPublishers.add(publisherIdentifier);
135165 signalOnNewPublisher (publisherIdentifier );
136166 }
137167 }
138168
139169 /**
140170 * Updates the list of {@link Publisher}s for the topic that this
141- * {@link Subscriber} is interested in.
142- *
171+ * {@link Subscriber} is interested in.<p/>
172+ * Creates UpdatePublisherRunnable of this classes generic type for each PublisherIdentifier.
173+ * Using executorService, spin the runnable which creates SlaveClient of type SlaveRpcEndpoint.
174+ * This is invoked from client Registrar when the onSubscriberAdded event occurs, and
175+ * from the SlaveServer when publisherUpdate is called.<br/>
143176 * @param publisherIdentifiers
144177 * {@link Collection} of {@link PublisherIdentifier}s for the
145178 * subscribed topic
@@ -279,4 +312,5 @@ public void run(SubscriberListener<T> listener) {
279312 public String toString () {
280313 return "Subscriber<" + getTopicDeclaration () + ">" ;
281314 }
315+
282316}
0 commit comments