001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * <p/> 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * <p/> 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.wake.remote.ports; 020 021 022import org.apache.reef.tang.Configuration; 023import org.apache.reef.tang.ConfigurationProvider; 024import org.apache.reef.tang.Tang; 025import org.apache.reef.tang.annotations.Parameter; 026import org.apache.reef.wake.remote.ports.parameters.TcpPortRangeBegin; 027import org.apache.reef.wake.remote.ports.parameters.TcpPortRangeCount; 028import org.apache.reef.wake.remote.ports.parameters.TcpPortRangeTryCount; 029 030import javax.inject.Inject; 031import java.util.Iterator; 032import java.util.logging.Logger; 033 034/** 035 * A TcpPortProvider which gives out random ports in a range 036 */ 037public final class RangeTcpPortProvider implements TcpPortProvider, ConfigurationProvider { 038 private final int portRangeBegin; 039 private final int portRangeCount; 040 private final int portRangeTryCount; 041 private static final Logger LOG = Logger.getLogger(RangeTcpPortProvider.class.getName()); 042 043 @Inject 044 public RangeTcpPortProvider(final @Parameter(TcpPortRangeBegin.class) int portRangeBegin, 045 final @Parameter(TcpPortRangeCount.class) int portRangeCount, 046 final @Parameter(TcpPortRangeTryCount.class) int portRangeTryCount) { 047 this.portRangeBegin = portRangeBegin; 048 this.portRangeCount = portRangeCount; 049 this.portRangeTryCount = portRangeTryCount; 050 } 051 052 /** 053 * Returns an iterator over a set of tcp ports 054 * 055 * @return an Iterator. 056 */ 057 @Override 058 public Iterator<Integer> iterator() { 059 return new RandomRangeIterator(portRangeBegin, portRangeCount, portRangeTryCount); 060 } 061 062 /** 063 * @deprecated have an instance injected instead. 064 */ 065 @Deprecated 066 public static final RangeTcpPortProvider Default = new RangeTcpPortProvider( 067 Integer.parseInt(TcpPortRangeBegin.default_value), 068 Integer.parseInt(TcpPortRangeCount.default_value), 069 Integer.parseInt(TcpPortRangeTryCount.default_value)); 070 071 072 @Override 073 public Configuration getConfiguration() { 074 return Tang.Factory.getTang().newConfigurationBuilder() 075 .bindNamedParameter(TcpPortRangeBegin.class, String.valueOf(portRangeBegin)) 076 .bindNamedParameter(TcpPortRangeCount.class, String.valueOf(portRangeCount)) 077 .bindNamedParameter(TcpPortRangeTryCount.class, String.valueOf(portRangeTryCount)) 078 .bindImplementation(TcpPortProvider.class, RangeTcpPortProvider.class) 079 .build(); 080 } 081 082 @Override 083 public String toString() { 084 return "RangeTcpPortProvider{" + 085 "portRangeBegin=" + portRangeBegin + 086 ", portRangeCount=" + portRangeCount + 087 ", portRangeTryCount=" + portRangeTryCount + 088 '}'; 089 } 090}