CountDownPublisherListener.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.node.topic;
00018 
00019 import org.ros.internal.node.topic.SubscriberIdentifier;
00020 
00021 import org.ros.internal.node.CountDownRegistrantListener;
00022 
00023 import java.util.concurrent.CountDownLatch;
00024 import java.util.concurrent.TimeUnit;
00025 
00032 public class CountDownPublisherListener<T> extends CountDownRegistrantListener<Publisher<T>>
00033     implements PublisherListener<T> {
00034 
00035   private final CountDownLatch shutdownLatch;
00036   private final CountDownLatch newSubscriberLatch;
00037 
00038   public static <T> CountDownPublisherListener<T> newDefault() {
00039     return newFromCounts(1, 1, 1, 1, 1);
00040   }
00041 
00054   public static <T> CountDownPublisherListener<T> newFromCounts(int masterRegistrationSuccessCount,
00055       int masterRegistrationFailureCount, int masterUnregistrationSuccessCount,
00056       int masterUnregistrationFailureCount, int newSubscriberCount) {
00057     return new CountDownPublisherListener<T>(new CountDownLatch(masterRegistrationSuccessCount),
00058         new CountDownLatch(masterRegistrationFailureCount), new CountDownLatch(
00059             masterUnregistrationSuccessCount),
00060         new CountDownLatch(masterUnregistrationFailureCount),
00061         new CountDownLatch(newSubscriberCount));
00062   }
00063 
00064   private CountDownPublisherListener(CountDownLatch masterRegistrationSuccessLatch,
00065       CountDownLatch masterRegistrationFailureLatch,
00066       CountDownLatch masterUnregistrationSuccessLatch,
00067       CountDownLatch masterUnregistrationFailureLatch, CountDownLatch newSubscriberLatch) {
00068     super(masterRegistrationSuccessLatch, masterRegistrationFailureLatch,
00069         masterUnregistrationSuccessLatch, masterUnregistrationFailureLatch);
00070     this.newSubscriberLatch = newSubscriberLatch;
00071     shutdownLatch = new CountDownLatch(1);
00072   }
00073 
00074   @Override
00075   public void onNewSubscriber(Publisher<T> publisher, SubscriberIdentifier subscriberIdentifier) {
00076     newSubscriberLatch.countDown();
00077   }
00078 
00079   @Override
00080   public void onShutdown(Publisher<T> publisher) {
00081     shutdownLatch.countDown();
00082   }
00083 
00089   public void awaitNewSubscriber() throws InterruptedException {
00090     newSubscriberLatch.await();
00091   }
00092 
00105   public boolean awaitNewSubscriber(long timeout, TimeUnit unit) throws InterruptedException {
00106     return newSubscriberLatch.await(timeout, unit);
00107   }
00108 
00114   public void awaitShutdown() throws InterruptedException {
00115     shutdownLatch.await();
00116   }
00117 
00129   public boolean awaitShutdown(long timeout, TimeUnit unit) throws InterruptedException {
00130     return shutdownLatch.await(timeout, unit);
00131   }
00132 }


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