org/objectweb/proactive/core/rmi/RandomPortSocketFactory.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.rmi;
00032 
00033 import java.io.IOException;
00034 import java.io.Serializable;
00035 import java.net.ServerSocket;
00036 import java.net.Socket;
00037 import java.rmi.server.RMIClientSocketFactory;
00038 import java.rmi.server.RMIServerSocketFactory;
00039 import java.util.Random;
00040 
00041 import org.apache.log4j.Logger;
00042 import org.objectweb.proactive.core.util.log.Loggers;
00043 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00044 
00045 
00050 public class RandomPortSocketFactory implements RMIServerSocketFactory,
00051     RMIClientSocketFactory, Serializable {
00052     static Logger logger = ProActiveLogger.getLogger(Loggers.RMI);
00053     static protected final int MAX = 5;
00054     static protected Random random = new Random();
00055     protected int basePort = 35000;
00056     protected int range = 5000;
00057 
00058     public RandomPortSocketFactory() {
00059         logger.debug("RandomPortSocketFactory constructor()");
00060     }
00061 
00062     public RandomPortSocketFactory(int basePort, int range) {
00063         logger.debug("RandomPortSocketFactory constructor(2) basePort = " +
00064             basePort + " range " + range);
00065         this.basePort = basePort;
00066         this.range = range;
00067     }
00068 
00069     public ServerSocket createServerSocket(int port) throws IOException {
00070         int tries = 0;
00071         logger.debug("RandomPortSocketFactory: createServerSocket " + port +
00072             " requested");
00073         while (true) {
00074             try {
00075                 int offset = random.nextInt(range);
00076                 ServerSocket socket = new ServerSocket(basePort + offset);
00077                 logger.debug("RandomPortSocketFactory: success for port " +
00078                     (basePort + offset));
00079                 return socket;
00080             } catch (IOException e) {
00081                 tries++;
00082                 if (tries > MAX) {
00083                     throw new IOException();
00084                 }
00085             }
00086         }
00087     }
00088 
00089     public Socket createSocket(String host, int port) throws IOException {
00090         logger.debug("RandomPortServerSocketFactory: createSocket to host " +
00091             host + " on port " + port);
00092         return new Socket(host, port);
00093     }
00094 }

Generated on Mon Jan 22 15:16:09 2007 for ProActive by  doxygen 1.5.1