KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > slave > PortRangeServerSocketFactory


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.slave;
19
20 import java.io.IOException JavaDoc;
21 import java.net.ServerSocket JavaDoc;
22 import java.net.Socket JavaDoc;
23 import java.rmi.server.RMISocketFactory JavaDoc;
24 import java.util.Random JavaDoc;
25
26 import org.apache.log4j.Logger;
27
28 /**
29  * @author mog
30  * @version $Id: PortRangeServerSocketFactory.java,v 1.5 2004/02/10 00:03:31 mog Exp $
31  * @deprecated
32  */

33 public class PortRangeServerSocketFactory extends RMISocketFactory JavaDoc {
34     private static final Logger logger =
35         Logger.getLogger(PortRangeServerSocketFactory.class);
36
37     int _portfrom;
38     int _portto;
39
40     public PortRangeServerSocketFactory(int portfrom, int portto) {
41         if(portfrom > portfrom) throw new IllegalArgumentException JavaDoc("from cannot be higher than to");
42         _portfrom = portfrom;
43         _portto = portto;
44     }
45
46
47     public ServerSocket JavaDoc createServerSocket(int port) throws IOException JavaDoc {
48         if(port != 0) logger.error("Returning anonymous port to non-anonymous request", new Throwable JavaDoc());
49         //getting a random number between portrangelow and portrangehigh,
50
//attempt to listen on port, if it fails: increase port
51
//if port is over portrangehigh, set to portrangelow, if port == random port: return
52
//repeat
53
int randport = new Random JavaDoc().nextInt(_portto - _portfrom) + _portfrom;
54         int myport = randport;
55         while (true) {
56             //wrap
57
if (myport > _portto)
58                 myport = _portfrom;
59             try {
60                 return new ServerSocket JavaDoc(myport, 1);
61             } catch (IOException JavaDoc e) {
62                 logger.warn("Failed to listen, will try next port", e);
63                 myport += 1;
64                 //fail
65
if (myport == randport)
66                     throw (IOException JavaDoc) new IOException JavaDoc(
67                         "No available ports to listen on").initCause(
68                         e);
69             }
70         }
71     }
72
73     /* (non-Javadoc)
74      * @see java.rmi.server.RMISocketFactory#createSocket(java.lang.String, int)
75      */

76     public Socket JavaDoc createSocket(String JavaDoc host, int port) throws IOException JavaDoc {
77         return new Socket JavaDoc(host, port);
78     }
79     /* (non-Javadoc)
80      * @see java.lang.Object#equals(java.lang.Object)
81      */

82     public boolean equals(Object JavaDoc obj) {
83         if(obj instanceof PortRangeServerSocketFactory) {
84             PortRangeServerSocketFactory factory = (PortRangeServerSocketFactory)obj;
85             return (factory._portfrom == _portfrom && factory._portto == _portto);
86         }
87         return false;
88     }
89
90 }
91
Popular Tags