2013년 10월 18일 금요일

RMI 채팅 예제

필요 파일
A.     RMICallbackClient.java :
B.     RMICallbackClientImpl.java
C.     RMICallbackServer.java
D.     RMICallbackServerImpl.java
 
 
import java.rmi.*;
public interface RMICallbackClient extends Remote {
  public abstract void said (String message) throws RemoteException;
}
 
 
 
import java.awt.*;
import java.rmi.*;
import java.awt.event.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class RMICallbackClientImpl extends UnicastRemoteObject implements RMICallbackClient, ActionListener {
  protected String host;
  protected Frame frame;
  protected TextField input;
  protected TextArea output;
  public RMICallbackClientImpl (String host) throws RemoteException {
    this.host = host;
    frame = new Frame ("RMICallbackClientImpl [" + host + "]");
    frame.add (output = new TextArea (), "Center");
    output.setEditable (false);
    frame.add (input = new TextField (), "South");
    input.addActionListener (this);
    frame.addWindowListener (new WindowAdapter () {
      public void windowOpened (WindowEvent ev) {
        input.requestFocus ();
      }
      public void windowClosing (WindowEvent ev) {
        try {
          stop ();
        } catch (RemoteException ex) {
          ex.printStackTrace ();
        }
      }
    });
    frame.pack ();
  }
  protected RMICallbackServer server;
  public synchronized void start () throws RemoteException, NotBoundException {
    if (server == null) {
      Registry registry = LocateRegistry.getRegistry (host);
      server = (RMICallbackServer) registry.lookup
        (RMICallbackServer.REGISTRY_NAME);
      server.register (this);
      frame.setVisible (true);
    }
  }
  public synchronized void stop () throws RemoteException {
    frame.setVisible (false);
    RMICallbackServer server = this.server;
    this.server = null;
    if (server != null)
      server.deregister (this);
  }
  public void said (String message) {
    output.append (message + "\n");
  }
  public void actionPerformed (ActionEvent ev) {
    try {
      RMICallbackServer server = this.server;
      if (server != null) {
        server.say (ev.getActionCommand ());
        input.setText ("");
      }
    } catch (RemoteException ex) {
      input.setVisible (false);
      frame.validate ();
      ex.printStackTrace ();
    }
  }
  public static void main (String[] args) throws RemoteException, NotBoundException {
    if (args.length != 1)
      throw new IllegalArgumentException
        ("Syntax: RMICallbackClientImpl <host>");
    RMICallbackClientImpl callbackClient =
      new RMICallbackClientImpl (args[0]);
    callbackClient.start ();
  }
}
 
 
 
 
import java.rmi.*;
public interface RMICallbackServer extends Remote {
  public static final String REGISTRY_NAME = "Callback Server";
//  public static final String REGISTRY_NAME = "rmi://localhost:2000/Callback Server";
  public abstract void register (RMICallbackClient client) throws RemoteException;
  public abstract void deregister (RMICallbackClient client) throws RemoteException;
  public abstract void say (String message) throws RemoteException;
}
 
 
 
import java.rmi.*;
import java.util.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class RMICallbackServerImpl extends UnicastRemoteObject implements RMICallbackServer {
  protected Vector clients;
  public RMICallbackServerImpl () throws RemoteException {
    clients = new Vector (); 
 }
  public void register (RMICallbackClient client) {
    try {
      say (getClientHost () + " has joined.");
    } catch (ServerNotActiveException ignored) {
    }
    clients.addElement (client);
  }
  public void deregister (RMICallbackClient client) {
    clients.removeElement (client);
    try {
      say (getClientHost () + " has left.");
    } catch (ServerNotActiveException ignored) {
    }
  }
  public void say (String message) {
    Vector clients = (Vector) this.clients.clone ();
    for (int i = 0; i < clients.size (); ++ i) {
      RMICallbackClient client = (RMICallbackClient) clients.elementAt (i);
      try {
        client.said (message);
      } catch (RemoteException ex) {
        this.clients.removeElement (client);
      }
    }
  }
  public static void main (String[] args) throws RemoteException {
    RMICallbackServerImpl callbackServer = new RMICallbackServerImpl ();
    Registry registry = LocateRegistry.getRegistry ();
    registry.rebind (REGISTRY_NAME, callbackServer);
  }
}
 
 

댓글 없음:

댓글 쓰기