CS 696 Emerging Technologies: Distributed Objects |
---|
import java.net.ServerSocket; import sdsu.util.ProgramProperties; import java.io.IOException; /** * This class starts the rmi registry on a random * open port. User can suggest a port using the -p flag */ public class StartRMIRegistry { public static void main( String[] args ) { try { ProgramProperties flags = new ProgramProperties( args ); int suggestedPort = flags.getInt( "p", 0); int port = getPort( suggestedPort ); String randomPortRegistry = "rmiregistry " + port; Runtime.getRuntime().exec( randomPortRegistry ); System.out.println( "rmiregistry running on port " + port ); System.exit( 0 ); } catch (IOException error ) { System.out.println( "Had trouble trying to find a port\n " + error); } } ` /** * Return an open port on current machine. Try the * suggested port first. * If suggestedPort is zero, just select a random port */ private static int getPort( int suggestedPort ) throws IOException { ServerSocket openSocket; try { openSocket = new ServerSocket( suggestedPort ); } catch (java.net.BindException portbusy) { // find random open port openSocket = new ServerSocket( 0 ); } int port = openSocket.getLocalPort(); openSocket.close(); return port; } }
import java.net.ServerSocket; import sdsu.util.ProgramProperties; import java.io.*; /** * This class starts the rmi registry on a random * open port. User can suggest a port using the -p flag */ public class StartRMIRegistry { static final String DEFAULT_DOMAIN = "sdsu.edu"; static final String PORT_KEY = "p"; static final String DOMAIN_KEY = "d"; static final String LOG_KEY = "l"; public static void main( String[] args ) { try { ProgramProperties flags = new ProgramProperties( args ); int suggestedPort = flags.getInt( PORT_KEY, 0); int port = startRMI( suggestedPort ); System.out.println( "rmiregistry running on port " + port ); Thread.sleep( 1000); //wait one second to let rmi start String domain = flags.getString( DOMAIN_KEY, DEFAULT_DOMAIN ); String host = getUnixHostName(); String fullHostName = host + "." + domain; boolean logServer; if (flags.containsKey( LOG_KEY ) ) logServer = true; else logServer = false; startUniVMRegistry( fullHostName, port, logServer ); System.out.println( "UniVMRegistry running" ); System.exit( 0 ); //don't let subprocesses keep us alive } catch (InterruptedException error ) { System.out.println( "Process interrupted " + error); System.exit( 0 ); } catch (Exception error ) { System.exit( 0 ); } } private static void startUniVMRegistry( String host, int port, boolean loggingOn ) throws IOException { try { String server = " -Djava.rmi.server.hostname=" + host.trim(); String logging = " "; //default value is false if (loggingOn) logging = " -Djava.rmi.server.logCalls=true"; String portFlag = " -p=" + port; String javaClass = " sdsu.rmi.registry.UniVMRegistry"; String command = "java" + server + logging + javaClass + portFlag + " &"; Runtime.getRuntime().exec( command ); } catch (IOException error ) { System.out.println( "Had trouble starting UniVMRegistry" + error); throw error; } } private static int startRMI( int suggestedPort ) throws IOException { try { int port = getPort( suggestedPort ); String randomPortRegistry = "rmiregistry " + port; Runtime.getRuntime().exec( randomPortRegistry ); return port; } catch (IOException error ) { System.out.println( "Had trouble trying to find a port\n " + error); throw error; } } /** * Return an open port on current machine. Try the * suggested port first. * If suggestedPort is zero, just select a random port */ private static int getPort( int suggestedPort ) throws IOException { ServerSocket openSocket; try { openSocket = new ServerSocket( suggestedPort ); } catch (java.net.BindException portbusy) { // find random open port openSocket = new ServerSocket( 0 ); } int port = openSocket.getLocalPort(); openSocket.close(); return port; } private static String getUnixHostName() throws Exception { try { Process hostName; BufferedReader answer; hostName = Runtime.getRuntime().exec( "hostname" ); answer = new BufferedReader( new InputStreamReader( hostName.getInputStream()) ); hostName.waitFor(); return answer.readLine().trim(); } catch (Exception noName) { System.out.println( "Trouble accessing host name"); throw noName; } } }