KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > util > PortRange


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.util;
19
20 import java.util.Random JavaDoc;
21
22 import org.apache.log4j.Logger;
23
24 /**
25  * @author mog
26  * @version $Id: PortRange.java,v 1.9 2004/06/01 15:40:33 mog Exp $
27  */

28 public class PortRange {
29     private static final Logger logger = Logger.getLogger(PortRange.class);
30     private int _minPort;
31     private boolean _ports[];
32     
33     Random JavaDoc rand = new Random JavaDoc();
34
35     /**
36      * Creates a default port range for port 49152 to 65535.
37      */

38     public PortRange() {
39         this(49152, 65535);
40     }
41
42     public PortRange(int minPort, int maxPort) {
43         _ports = new boolean[maxPort - minPort];
44         _minPort = minPort;
45     }
46
47     protected void finalize() throws Throwable JavaDoc {
48         super.finalize();
49         for (int i = 0; i < _ports.length; i++) {
50             if (_ports[i]) {
51                 logger.debug(_minPort + i + " not released");
52             }
53         }
54     }
55
56     /**
57      * @deprecated Doesn't check if port is used even though marked as unused.
58      */

59     public int getPort() {
60         synchronized (_ports) {
61             int initPos = rand.nextInt(_ports.length);
62             logger.debug("initPos: " + initPos);
63             int pos = initPos;
64             while (true) {
65                 if (_ports[pos] == false) {
66                     _ports[pos] = true;
67                     logger.debug("returning " + _minPort + pos);
68                     return _minPort + pos;
69                 } else {
70                     pos++;
71                     if (pos == initPos)
72                         throw new RuntimeException JavaDoc("Portrange exhausted");
73                     if (pos > _ports.length)
74                         pos = 0;
75                 }
76             }
77         }
78     }
79
80     public void releasePort(int port) {
81         synchronized (_ports) {
82             if(_ports[port - _minPort]
83                 != true) throw new RuntimeException JavaDoc("releasePort() on unused port");
84             _ports[port - _minPort] = false;
85         }
86     }
87
88 }
89
Popular Tags