SwitchableMasterUriProvider.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 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.master.uri;
00018 
00019 import com.google.common.collect.Lists;
00020 
00021 import org.ros.exception.RosRuntimeException;
00022 
00023 import java.net.URI;
00024 import java.util.List;
00025 import java.util.concurrent.CountDownLatch;
00026 import java.util.concurrent.TimeUnit;
00027 
00036 public class SwitchableMasterUriProvider implements MasterUriProvider {
00037 
00038   private final Object mutex;
00039 
00043   private MasterUriProvider provider;
00044 
00048   private List<ProviderRequest> pending = Lists.newArrayList();
00049 
00054   public SwitchableMasterUriProvider(MasterUriProvider provider) {
00055     this.provider = provider;
00056     mutex = new Object();
00057   }
00058 
00059   @Override
00060   public URI getMasterUri() throws RosRuntimeException {
00061     MasterUriProvider providerToUse = null;
00062     ProviderRequest requestToUse = null;
00063 
00064     synchronized (mutex) {
00065       if (provider != null) {
00066         providerToUse = provider;
00067       } else {
00068         requestToUse = new ProviderRequest();
00069         pending.add(requestToUse);
00070       }
00071     }
00072 
00073     if (providerToUse != null) {
00074       return providerToUse.getMasterUri();
00075     } else {
00076       return requestToUse.getMasterUri();
00077     }
00078   }
00079 
00080   @Override
00081   public URI getMasterUri(long timeout, TimeUnit unit) {
00082     // We can't really switch providers, but people are willing to wait. It
00083     // seems appropriate to wait rather than to return immediately.
00084 
00085     MasterUriProvider providerToUse = null;
00086     synchronized (mutex) {
00087       if (provider != null) {
00088         providerToUse = provider;
00089       }
00090     }
00091 
00092     if (providerToUse != null) {
00093       return providerToUse.getMasterUri(timeout, unit);
00094     } else {
00095       try {
00096         Thread.sleep(unit.toMillis(timeout));
00097       } catch (InterruptedException e) {
00098         // Don't care
00099       }
00100 
00101       return null;
00102     }
00103   }
00104 
00111   public void switchProvider(MasterUriProviderSwitcher switcher) {
00112     synchronized (mutex) {
00113       MasterUriProvider oldProvider = provider;
00114       provider = switcher.switchProvider(oldProvider);
00115 
00116       if (oldProvider == null) {
00117         for (ProviderRequest request : pending) {
00118           request.setProvider(provider);
00119         }
00120         pending.clear();
00121       }
00122     }
00123   }
00124 
00132   public interface MasterUriProviderSwitcher {
00133 
00142     MasterUriProvider switchProvider(MasterUriProvider oldProvider);
00143   }
00144 
00148   private static class ProviderRequest {
00149 
00153     private CountDownLatch latch = new CountDownLatch(1);
00154 
00158     private MasterUriProvider provider;
00159 
00168     public URI getMasterUri() {
00169       try {
00170         latch.await();
00171         return provider.getMasterUri();
00172       } catch (InterruptedException e) {
00173         throw new RosRuntimeException("URI provider interrupted", e);
00174       }
00175     }
00176 
00183     public void setProvider(MasterUriProvider provider) {
00184       this.provider = provider;
00185       latch.countDown();
00186     }
00187   }
00188 }


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