public class Server implements ServerExports {
int index = -1;
public int register(ClientExports client) {
clients[++index] = client;
return index;
}
ClientExports clients[] = new ClientExports [100];
public void broadcast(Update event) { }
}
|
public interface ServerExports {
public int register(ClientExports client);
public void broadcast(Update event);
} |
public class Simulation {
public static void main(String[] args) {
// on the server's host
Server server = new Server();
// on any of the clients' hosts
ServerExports far = server;
Client adrian = new Client("Adrian");
adrian.id = far.register(adrian);
adrian.server = far;
adrian.start();
Client bjorn = new Client("Bjorn");
bjorn.id = far.register(bjorn);
bjorn.server = far;
bjorn.start();
Client petchel = new Client("Tom");
petchel.id = far.register(petchel);
petchel.server = far;
petchel.start();
}
}
|
|
public class Client extends Thread implements ClientExports {
String name;
public Client(String name) { this.name = name; }
public void update(Update event) { }
public void run() {
while (true) {
try { sleep((int)(Math.random() * 6000 + 1000)); }
catch (Exception e) { }
System.out.println(this.name + " here...");
}
}
} |
public interface ClientExports {
public void update(Update event);
} |
|