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.annotations.Parameter; 023import org.apache.reef.wake.remote.ports.parameters.TcpPortRangeBegin; 024import org.apache.reef.wake.remote.ports.parameters.TcpPortRangeCount; 025import org.apache.reef.wake.remote.ports.parameters.TcpPortRangeTryCount; 026 027import javax.inject.Inject; 028import java.util.Iterator; 029import java.util.logging.Level; 030import java.util.logging.Logger; 031 032/** 033 * A TcpPortProvider which gives out random ports in a range. 034 */ 035public final class RangeTcpPortProvider implements TcpPortProvider { 036 private final int portRangeBegin; 037 private final int portRangeCount; 038 private final int portRangeTryCount; 039 private static final Logger LOG = Logger.getLogger(RangeTcpPortProvider.class.getName()); 040 041 @Inject 042 public RangeTcpPortProvider(@Parameter(TcpPortRangeBegin.class) final int portRangeBegin, 043 @Parameter(TcpPortRangeCount.class) final int portRangeCount, 044 @Parameter(TcpPortRangeTryCount.class) final int portRangeTryCount) { 045 this.portRangeBegin = portRangeBegin; 046 this.portRangeCount = portRangeCount; 047 this.portRangeTryCount = portRangeTryCount; 048 LOG.log(Level.FINE, "Instantiating " + this); 049 } 050 051 /** 052 * Returns an iterator over a set of tcp ports. 053 * 054 * @return an Iterator. 055 */ 056 @Override 057 public Iterator<Integer> iterator() { 058 return new RandomRangeIterator(portRangeBegin, portRangeCount, portRangeTryCount); 059 } 060 061 /** 062 * @deprecated in 0.12 have an instance injected instead. 063 */ 064 @Deprecated 065 @SuppressWarnings("checkstyle:constantname") 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 @Override 072 public String toString() { 073 return "RangeTcpPortProvider{" + 074 "portRangeBegin=" + portRangeBegin + 075 ", portRangeCount=" + portRangeCount + 076 ", portRangeTryCount=" + portRangeTryCount + 077 '}'; 078 } 079}