Up Previous Next (end of slides)

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;

public class Server extends UnicastRemoteObject implements ServerExports {
  int index = -1;

  synchronized public int register(ClientExports client) throws RemoteException { 
    clients[++index] = client;
    return index;
  }

  ClientExports clients[] = new ClientExports[100];
  synchronized public void broadcast(Update event) throws RemoteException {
    for (int i = 0; i <= index; i++)
      clients[i].update(event);
  }
  public Server() throws RemoteException {
    System.out.println("Server being initialized... ");
  }
  public static void main(String[] args) {
    System.setSecurityManager(new RMISecurityManager());
    try {
      Server pam = new Server();
      Registry cat = LocateRegistry.createRegistry(Integer.parseInt(args[0]));
      cat.bind("Dirac", pam);
      System.out.println("Server is ready... ");
    } catch (Exception e) {
      System.out.println("Server error: " + e + "... ");
    }
  }
}

import java.rmi.*;

public interface ServerExports extends Remote {
  public int register(ClientExports client) throws RemoteException ;
  public void broadcast(Update event) throws RemoteException ;
}
import java.rmi.*;
import java.rmi.server.*;

public class Client extends Thread implements ClientExports {
  String name;
  int id;
  ServerExports server;
  public Client(String name) { this.name = name; }
  public void update(Update event) throws RemoteException {
    System.out.println(this.name + " receives: ***(" + event + ")*** ");
  }
  public void run() {
    while (true) {
      try { sleep((int)(Math.random() * 6000 + 10000)); }
      catch (Exception e) { }
      server.broadcast(new Update(this.name + " says: Howdy!"));
    }
  }
  public static void main(String[] args) {
    try {
      ServerExports far = (ServerExports)Naming.lookup("rmi://" + args[0] + ":" + args[1] + "/Dirac");
      Client here = new Client( args[2] );
      UnicastRemoteObject.exportObject(here);
      here.id = far.register(here);
      here.server = far;
      here.start();
    } catch (Exception e) {
      System.out.println("Error in client... " + e);
      e.printStackTrace();
    }
  }}
}

import java.rmi.*;

public interface ClientExports extends Remote {
  public void update(Update event) throws RemoteException ;
}

import java.io.*;

public class Update implements Serializable {
  Update(String message) {
    this.message = message;
  }
  String message;
  public String toString() {
    return message;
  }
}
Which ends the transformation. Hopefully these last few slides gave a better idea how the code changes.

This is the end of this set of presenter's notes: Sat Sep 22 23:01:24 EDT 2007

List of necessary changes to this material should result in a new set of notes. Discussion forthcoming.

Add perform method to Penguin, use threads to power them up like in this last example, see it working both ways (as expected).

Easiest: develop a n-to-m penguin football game, with ice blocks instead of footballs. Random motion, show score, etc.


Up Previous Next