// Sensor server // // Usage: SensorServer [port] // // Implements the generation of data samples for the example // from section 1 of "Time-Recursive EM Examples", R. Perry, June 1998. import java.io.*; import java.util.Random; import java.net.InetAddress; import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.LocateRegistry; public class SensorServer extends UnicastRemoteObject implements Sensor { private Random Ran; private double p = 0.4; /* P(1) */ private double c = 2.0; /* scale factor */ private double s = 0.2; /* noise standard deviation */ public SensorServer() throws java.rmi.RemoteException { super(); Ran = new Random(); } public double get_y() throws RemoteException, ServerNotActiveException { System.out.println( "SensorServer: get_y() called from " + this.getClientHost()); int b = (Ran.nextDouble() < p) ? 1 : 0; System.out.println( "b = " + b); double y = c*b + s*Ran.nextGaussian(); System.out.println( "y = " + y); return y; } private static void createPortFile( int port) throws java.io.IOException { PrintWriter fout = new PrintWriter( new BufferedWriter( new FileWriter( "SensorServer.port"))); fout.println( InetAddress.getLocalHost().getHostName()); fout.println( port); if( fout.checkError()) { System.err.println( "SensorServer: error writing port file."); System.exit(1); } fout.close(); } public static void main(String args[]) throws Exception { int port = 31099; if (args.length == 1) port = Integer.parseInt(args[0]); // Create and install the security manager // System.setSecurityManager(new RMISecurityManager()); try { LocateRegistry.createRegistry(port); Sensor S = new SensorServer(); Naming.rebind("rmi://:" + port + "/SensorServer", S); System.err.println("SensorServer: created and bound in the registry on port " + port); } catch (Exception e) { System.err.println("SensorServer: " + e); System.exit(1); } createPortFile( port); } }