// EM server // // Usage: EMServer [port] // // Implements the EM algorithm for the example from section 1 of // "Time-Recursive EM Examples", R. Perry, June 1998. // Uses buffer size K = 1 (i.e. no buffering or pipelining). import java.io.*; import java.net.InetAddress; import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.LocateRegistry; public class EMServer extends UnicastRemoteObject implements EM { private static double c = 0; private static int n1 = 0; public EMServer() throws java.rmi.RemoteException { super(); } public int get_b( double yK) throws RemoteException, ServerNotActiveException { System.out.println( "EMServer: get_bK(" + yK + ") called from " + this.getClientHost()); int bK = 1; /* initial estimate */ double d = c; for( int i = 0; i < 3; ++i) { /* 3 iterations of EM */ /* E-step */ d = c; if( bK == 1) d += (yK - c)/(n1 + 1); /* M-step */ bK = (d > 0) ? ((yK > d/2) ? 1 : 0) : ((yK < d/2) ? 1 : 0); } if( bK == 1) { ++n1; c = d; } System.out.println( "EMServer: c = " + c); return bK; } private static void createPortFile( int port) throws java.io.IOException { PrintWriter fout = new PrintWriter( new BufferedWriter( new FileWriter( "EMServer.port"))); fout.println( InetAddress.getLocalHost().getHostName()); fout.println( port); if( fout.checkError()) { System.err.println( "EMServer: error writing port file."); System.exit(1); } fout.close(); } public static void main(String args[]) throws Exception { int port = 41099; if (args.length == 1) port = Integer.parseInt(args[0]); // Create and install the security manager // System.setSecurityManager(new RMISecurityManager()); try { LocateRegistry.createRegistry(port); EM S = new EMServer(); Naming.rebind("rmi://:" + port + "/EMServer", S); System.err.println("EMServer: created and bound in the registry on port " + port); } catch (Exception e) { System.err.println("EMServer: " + e); System.exit(1); } createPortFile( port); } }