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 org.ros.internal.node.server.NodeIdentifier;
00020 import org.ros.message.MessageFactory;
00021 import org.ros.message.MessageSerializer;
00022 import org.ros.namespace.GraphName;
00023 import org.ros.node.topic.DefaultPublisherListener;
00024 import org.ros.node.topic.Publisher;
00025
00026 import java.util.concurrent.ScheduledExecutorService;
00027
00033 public class PublisherFactory {
00034
00035 private final TopicParticipantManager topicParticipantManager;
00036 private final MessageFactory messageFactory;
00037 private final ScheduledExecutorService executorService;
00038 private final NodeIdentifier nodeIdentifier;
00039 private final Object mutex;
00040
00041 public PublisherFactory(NodeIdentifier nodeIdentifier,
00042 TopicParticipantManager topicParticipantManager, MessageFactory messageFactory,
00043 ScheduledExecutorService executorService) {
00044 this.nodeIdentifier = nodeIdentifier;
00045 this.topicParticipantManager = topicParticipantManager;
00046 this.messageFactory = messageFactory;
00047 this.executorService = executorService;
00048 mutex = new Object();
00049 }
00050
00064 @SuppressWarnings("unchecked")
00065 public <T> Publisher<T> newOrExisting(TopicDeclaration topicDeclaration,
00066 MessageSerializer<T> messageSerializer) {
00067 GraphName topicName = topicDeclaration.getName();
00068 synchronized (mutex) {
00069 if (topicParticipantManager.hasPublisher(topicName)) {
00070 return (DefaultPublisher<T>) topicParticipantManager.getPublisher(topicName);
00071 } else {
00072 DefaultPublisher<T> publisher =
00073 new DefaultPublisher<T>(nodeIdentifier, topicDeclaration, messageSerializer,
00074 messageFactory, executorService);
00075 publisher.addListener(new DefaultPublisherListener<T>() {
00076 @Override
00077 public void onNewSubscriber(Publisher<T> publisher,
00078 SubscriberIdentifier subscriberIdentifier) {
00079 topicParticipantManager.addPublisherConnection((DefaultPublisher<T>) publisher,
00080 subscriberIdentifier);
00081 }
00082
00083 @Override
00084 public void onShutdown(Publisher<T> publisher) {
00085 topicParticipantManager.removePublisher((DefaultPublisher<T>) publisher);
00086 }
00087 });
00088 topicParticipantManager.addPublisher(publisher);
00089 return publisher;
00090 }
00091 }
00092 }
00093 }