KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > rmi > RandomPortSocketFactory


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.core.rmi;
32
33 import java.io.IOException JavaDoc;
34 import java.io.Serializable JavaDoc;
35 import java.net.ServerSocket JavaDoc;
36 import java.net.Socket JavaDoc;
37 import java.rmi.server.RMIClientSocketFactory JavaDoc;
38 import java.rmi.server.RMIServerSocketFactory JavaDoc;
39 import java.util.Random JavaDoc;
40
41 import org.apache.log4j.Logger;
42
43 /**
44  * This factory creates server socket with randomly choosen port number
45  * it tries 5 different ports before reporting a failure
46  */

47 public class RandomPortSocketFactory implements RMIServerSocketFactory JavaDoc, RMIClientSocketFactory JavaDoc, Serializable JavaDoc {
48
49
50     static Logger logger = Logger.getLogger(RandomPortSocketFactory.class.getName());
51
52   static protected final int MAX = 5;
53   static protected Random JavaDoc random = new Random JavaDoc();
54   //static private RMISocketFactory factory = RMISocketFactory.getDefaultSocketFactory();
55

56   protected int basePort = 35000;
57   protected int range = 5000;
58
59   public RandomPortSocketFactory() {
60     logger.debug("RandomPortSocketFactory constructor()");
61   }
62
63
64   public RandomPortSocketFactory(int basePort, int range) {
65     logger.debug("RandomPortSocketFactory constructor(2) basePort = " + basePort + " range " + range);
66     this.basePort = basePort;
67     this.range = range;
68   }
69
70
71   public ServerSocket JavaDoc createServerSocket(int port) throws IOException JavaDoc {
72     int tries = 0;
73     logger.debug("RandomPortSocketFactory: createServerSocket " + port + " requested" );
74     while (true) {
75       try {
76         int offset = random.nextInt(range);
77         // logger.debug("RandomPortSocketFactory: createServerSocket with defaultRMI trying to use port " + (basePort+offset));
78
ServerSocket JavaDoc socket = new ServerSocket JavaDoc(basePort + offset);
79         logger.debug("RandomPortSocketFactory: success for port " + (basePort + offset));
80         // logger.debug("RandomPortSocketFactory: socket says port " + socket.getLocalPort());
81
return socket;
82       } catch (IOException JavaDoc e) {
83         tries++;
84         if (tries > MAX)
85         //throw new IOException("RandomPortSocketFactory: failure to create a socket after " + tries + " attempts !!!");
86
throw new IOException JavaDoc();
87       }
88     }
89   }
90
91
92   public Socket JavaDoc createSocket(String JavaDoc host, int port) throws IOException JavaDoc {
93         logger.debug("RandomPortServerSocketFactory: createSocket to host " + host + " on port " + port);
94 // Object t = null;
95
// t.toString();
96
//try {
97
return new Socket JavaDoc(host, port);
98     //} catch (Exception e) {
99
// e.printStackTrace();
100
//}
101
//return null;
102
}
103 }
104
Popular Tags