Enhancing the Chat Application a Little Bit. Towards Internet-Mediated Hockey.
frilled.cs.indiana.edu%ls -ld *.java
-rw-------   1 dgerman      2299 Nov 19 06:48 ChatClient.java
-rw-------   1 dgerman      1589 Nov 19 06:51 ChatServer.java
-rw-------   1 dgerman       144 Nov 19 02:43 ClientExports.java
-rw-------   1 dgerman       468 Nov 19 04:46 Player.java
-rw-------   1 dgerman       218 Nov 19 04:42 ServerExports.java
frilled.cs.indiana.edu%cat ChatClient.java
import java.rmi.*; 
import java.rmi.server.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.*; 

public class ChatClient extends Frame implements ClientExports, 
						 KeyListener {
    static int WIDTH = 300, HEIGHT = 200, hori = 30, vert = 30; 
    Player[] players = new Player[100]; 
    int x, y, width, height, numb; 
    ServerExports server; 
    public ChatClient(int x, 
		      int y, 
		      int width, 
		      int height, 
		      ServerExports far) throws RemoteException {
	super ("You are a client..."); 
	System.out.println("Starting up client..."); numb = -1; 
	this.x = x; this.y = y; this.width= width; this.height = height;
	addKeyListener(this); server = far; 
    }
    
    public void update(int player, int x, int y) {
	if (players[player] == null) 
	    players[player] = new Player(x, y, width, height); 
	else players[player].moveTo(x, y); 
	repaint(); 
    }
    
    public void paint(Graphics g) {
	for (int i = 0; i < players.length; i++) 
	    if (players[i] != null) 
		players[i].draw(g, i == numb); 
    }

    public void keyTyped(KeyEvent e) { }

    public void keyPressed(KeyEvent e) {
	int val = e.getKeyCode(); 
	try {
	    if (val == 38) 
		server.broadcast(numb, x, --y); 
	    else if (val == 39) 
		server.broadcast(numb, ++x, y);
	    else if (val == 40) 
		server.broadcast(numb, x, ++y);
	    else if (val == 37) 
		server.broadcast(numb, --x, y);
	    else { System.out.println(val); }

	} catch (Exception oops) {
	    System.out.println("Exception in key processed: " + oops); 
	}
    }

    public void keyReleased(KeyEvent e) { }
    
    public static void main(String[] args) {
	try {

	    ServerExports far = 
		(ServerExports)Naming.lookup("rmi://" + 
					     args[0] + 
					     ":" + 
					     args[1] + 
					     "/Dirac");

	    ChatClient here = new ChatClient
		(
		 (int)(Math.random() * (WIDTH - 2 * hori) + hori), 
		 (int)(Math.random() * (HEIGHT - 2 * vert) + vert), 
		 30, 
		 30, 
		 far);

	    here.resize(WIDTH, HEIGHT);
	    here.show(); 

	    UnicastRemoteObject.exportObject(here); 
	    here.numb = far.register(here); 

	    far.broadcast(here.numb, here.x, here.y); 

	} catch (Exception e) {
	    System.out.println("Error in client..." + e); 
	    e.printStackTrace();
	}
    }
}


frilled.cs.indiana.edu%cat ChatServer.java
import java.rmi.*;
import java.rmi.server.*; 
import java.rmi.registry.*; 
import java.util.*;

public class ChatServer extends UnicastRemoteObject implements ServerExports {

    ClientExports[] clients = new ClientExports[100]; 

    Player[] players = new Player[100]; 

    int size = -1; 

    public ChatServer() throws RemoteException {
	System.out.println("Server being initialized... "); 
    }

    public int register(ClientExports remote) throws RemoteException {  
	int i = ++size; 
	clients[i] = remote;
	System.out.println("Client " + i + " has registered."); 
	return i; 
    }

    public void broadcast(int name, int x, int y) {
	if (players[name] == null) {
	    players[name] = new Player(x, y, 30, 30); 
	    for (int i = 0; i <= size; i++) {
		for (int j = 0; j <= size; j++) {
		    try {
			clients[i].update(j, players[j].x, players[j].y); 
		    } catch (Exception e) {
			System.out.println("Client unavailable..."); 
		    }
		}
	    }
	} else { 
	    players[name].moveTo(x, y); 
	    for (int i = 0; i <= size; i++) 
		try {
		    clients[i].update(name, x, y); 
		} catch (Exception e) {
		    System.out.println("Client unavailable..."); 
		}
	}
    }

    public static void main(String[] args) {
	System.setSecurityManager(new RMISecurityManager()); 
	try {

	    ChatServer pam = new ChatServer(); 
	    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 + "... "); 
	}
    }
}
frilled.cs.indiana.edu%cat Player.java
import java.awt.*; 

public class Player {
    int x, y, width, height;
    public Player(int x, int y, int width, int height) {
	this.x = x; this.y = y; this.width = width; this.height = height; 
    }
    public void moveTo(int x, int y) {
	this.x = x; this.y = y; 
    }
    public void draw(Graphics g, boolean self) {
	g.drawRect(x, y, width, height); 
	if (self) {
	    g.drawLine(x, y, x+width, y+height); 
	    g.drawLine(x+width, y, x, y+height); 
	}
    }
}
frilled.cs.indiana.edu%cat ClientExports.java
import java.rmi.*;

public interface ClientExports extends Remote {
    public void update(int player, int x, int y) throws RemoteException; 
}
frilled.cs.indiana.edu%cat ServerExports.java
import java.rmi.*;

public interface ServerExports extends Remote {
    public int register (ClientExports client) throws RemoteException; 
    public void broadcast (int name, int x, int y) throws RemoteException; 
}
frilled.cs.indiana.edu%ls -ld *.java
-rw-------   1 dgerman      2299 Nov 19 06:48 ChatClient.java
-rw-------   1 dgerman      1589 Nov 19 06:51 ChatServer.java
-rw-------   1 dgerman       144 Nov 19 02:43 ClientExports.java
-rw-------   1 dgerman       468 Nov 19 04:46 Player.java
-rw-------   1 dgerman       218 Nov 19 04:42 ServerExports.java
frilled.cs.indiana.edu%

Last updated: Nov 19, 2001 by Adrian German for A348/A548