PublisherFactory.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
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 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49