HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Direct Appplet to Applet Communication with RMI"
Volume: 3 Issue: 8 p. 38

	

Listing 1.
 
import java.rmi.*; 
public interface Talker extends Remote { 
  public String getName() throws RemoteException; 
  public void recvMsg(String from, String msg) throws RemoteException; 
  public void add(Talker talker) throws RemoteException; 
  public void remove(Talker talker) throws RemoteException; 
} 

Listing 2.
 
import java.rmi.*; 
public interface TalkerServer extends Remote { 

  public void register(Talker talker) throws RemoteException; 
  public void unRegister(Talker talker) throws RemoteException; 
  public Talker[] lookup() throws RemoteException; 
  public void shutdown() throws RemoteException; 
} 

Listing 3.
 
import java.rmi.*; 
import java.rmi.server.UnicastRemoteObject; 
import java.rmi.registry.LocateRegistry; 
import java.util.*; 

public class TalkerServerImpl extends UnicastRemoteObject implements TalkerServer { 

  private Vector v = new Vector(); 

  public TalkerServerImpl() throws RemoteException { 
    super(); 
  } 

  public synchronized void register(Talker talker) throws RemoteException { 
    if (!v.contains(talker)) { 
      v.addElement(talker); 
      for (int i = 0; i < v.size(); i++) 
        ((Talker)v.elementAt(i)).add(talker); 
    } 
  } 

  public synchronized void unRegister(Talker talker) throws RemoteException { 
    for (int i = 0; i < v.size(); i++) 
      ((Talker)v.elementAt(i)).remove(talker); 
    v.removeElement(talker); 
  } 

  public synchronized Talker[] lookup() throws RemoteException { 
    Talker[] talkers = new Talker[v.size()]; 
    v.copyInto(talkers); 
    return talkers; 
  } 

  public void shutdown() throws RemoteException { 
    System.exit(0); 
  } 

  public static void main(String args[]) { 

    System.setSecurityManager(new RMISecurityManager()); 

    try { 
      System.out.println("TalkerServer.main: creating registry"); 
      LocateRegistry.createRegistry(2006); 

      System.out.println("TalkerServer.main: creating server"); 
      TalkerServerImpl talkerServer = new TalkerServerImpl(); 

      System.out.println("TalkerServer.main: binding server"); 
      Naming.rebind("//:2006/TalkerServer", talkerServer); 

      System.out.println("TalkerServer.main: done"); 
    } catch (Exception ex) { 
      System.err.println("TalkerServer.main: exception: " + ex.getMessage()); 
      ex.printStackTrace(); 
    } 

  } 

} 

Listing 4.
 
import java.rmi.*; 
import java.rmi.server.UnicastRemoteObject; 

class TalkerImpl extends UnicastRemoteObject implements Talker { 

  private TalkerApplet applet; 
  private String name; 

  public TalkerImpl(TalkerApplet applet, String name) throws RemoteException { 
    this.applet = applet; 
    this.name = name; 
  } 

  public String getName() throws RemoteException { 
    return name; 
  } 

  public void recvMsg(String from, String msg) throws RemoteException { 
    applet.addToMsg(from, msg); 
  } 

  public void add(Talker talker) throws RemoteException { 
    applet.addTalker(talker, talker.getName()); 
  } 

  public void remove(Talker talker) throws RemoteException { 
    applet.removeTalker(talker, talker.getName()); 
  } 

} 

Listing 5.
 
import java.applet.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.net.*; 
import java.rmi.*; 
import java.util.*; 

public class TalkerApplet extends Applet { 

  class StartListener implements ActionListener { 
    TalkerApplet applet; 
    public StartListener(TalkerApplet applet) { 
      this.applet = applet; 
    } 
    public void actionPerformed(ActionEvent ev) { 
      field.setEditable(false); 
      start.setEnabled(false); 
      stop.setEnabled(true); 
      try { 
        initList(); 
        talker = new TalkerImpl(applet, field.getText()); 
        server.register(talker); 
      } catch (Exception ex) { 
      } 
    } 
  } 

  class StopListener implements ActionListener { 
    public void actionPerformed(ActionEvent ev) { 
      field.setEditable(true); 
      start.setEnabled(true); 
      stop.setEnabled(false); 
      try { 
        server.unRegister(talker); 
        talker = null; 
      } catch (Exception ex) { 
      } 
    } 
  } 

  class ShutdownListener implements ActionListener { 
    public void actionPerformed(ActionEvent ev) { 
      shutdown.setEnabled(false); 
      try { 
        server.shutdown(); 
      } catch (Exception ex) { 
      } 
    } 
  } 

  class TextAreaListener extends KeyAdapter { 
    public void keyReleased(KeyEvent ev) { 
      String s; 
      String s1; 
      String s2; 
      int i1; 
      int i2; 
      if (talker != null && ev.getKeyCode() == KeyEvent.VK_ENTER) { 

        // send the last line of text 
        s1 = senderText.getText(); 
        i1 = s1.lastIndexOf('\n'); 
        if (i1 == 0) s = s1; 
        else { 
          s2 = s1.substring(0, i1); 
          i2 = s2.lastIndexOf('\n'); 
          if (i2 == -1) s = s1; 
          else s = s2.substring(i2+1, s2.length()) + '\n'; 
        } 

        // and send it!! 
        String[] names = list.getSelectedItems(); 
        for (int i = 0; i < names.length; i++) 
          for (int j = 0; j < talkersNames.size(); j++) 
            if (names[i].equals((String)talkersNames.elementAt(j))) { 
              try { 
                ((Talker)talkers.elementAt(j)).recvMsg(field.getText(), s); 
              } catch (RemoteException ex) { 
              } 
            } 
      } 
    } 
  } 

  Button start, stop, shutdown; 
  TextField field; 
  List list; 
  TextArea receiverText; 
  TextArea senderText; 
  TalkerImpl talker; 
  TalkerServer server; 
  java.util.Vector talkers = new java.util.Vector(); 
  java.util.Vector talkersNames = new java.util.Vector(); 

  public void init() { 

    try { 
      server = (TalkerServer)Naming.lookup("//pluto:2006/TalkerServer"); 
    } catch (Exception ex) { 
      ex.printStackTrace(); 
      return; 
    } 

    setSize(550, 450); 

    GridBagLayout layout = new GridBagLayout(); 
    GridBagConstraints c = new GridBagConstraints(); 
    c.anchor = GridBagConstraints.NORTHWEST; 
    c.insets = new Insets(4, 4, 4, 4); 

    Panel p1 = new Panel(); 
    p1.setLayout(layout); 

    c.gridx = 0; c.gridy = 0; 
    Label label1 = new Label("Name:"); 
    layout.setConstraints(label1, c); 
    p1.add(label1); 

    c.gridx = 1; 
    field = new TextField(15); 
    layout.setConstraints(field, c); 
    p1.add(field); 

    c.gridx = 0; c.gridy = 1; 
    Label label2 = new Label("Talk to:"); 
    layout.setConstraints(label2, c); 
    p1.add(label2); 

    c.gridx = 1; 
    list = new List(5, false); 
    list.setMultipleMode(true); 
    layout.setConstraints(list, c); 
    p1.add(list); 

    c.gridx = 0; c.gridy = 2; 
    Label sender = new Label("Send message:"); 
    layout.setConstraints(sender, c); 
    p1.add(sender); 

    c.gridx = 1; 
    senderText = new TextArea(5, 50); 
    senderText.addKeyListener(new TextAreaListener()); 
    layout.setConstraints(senderText, c); 
    p1.add(senderText); 

    c.gridx = 0; c.gridy = 3; 
    Label receiver = new Label("Receive message:"); 
    layout.setConstraints(receiver, c); 
    p1.add(receiver); 

    c.gridx = 1; 
    receiverText = new TextArea(5, 50); 
    layout.setConstraints(receiverText, c); 
    p1.add(receiverText); 

    Panel p2 = new Panel(); 
    p2.setLayout(new FlowLayout(FlowLayout.CENTER)); 
    start = new Button("Start"); 
    start.addActionListener(new StartListener(this)); 
    p2.add(start); 
    stop = new Button("Stop"); 
    stop.setEnabled(false); 
    stop.addActionListener(new StopListener()); 
    p2.add(stop); 
    shutdown = new Button("Shutdown Server"); 
    shutdown.addActionListener(new ShutdownListener()); 
    p2.add(shutdown); 

    add(p1); 
    add(p2); 
    initList(); 

  } 

  void initList() { 
    Talker[] talkersArray; 
    try { 
      talkersArray = server.lookup(); 
    } catch (RemoteException ex) { 
      ex.printStackTrace(); 
      return; 
    } 
    list.removeAll(); 
    talkers.removeAllElements(); 
    talkersNames.removeAllElements(); 
    for (int i = 0; i < talkersArray.length; i++) { 
      try { 
        list.add(talkersArray[i].getName()); 
        talkers.addElement(talkersArray[i]); 
        talkersNames.addElement(talkersArray[i].getName()); 
      } catch (RemoteException ex) { 
      } 
    } 
  } 

  public void addToMsg(String from, String msg) { 
    receiverText.append(from + ": " + msg); 
  } 

  public void addTalker(Talker talker, String name) { 
    list.add(name); 
    talkers.addElement(talker); 
    talkersNames.addElement(name); 
  } 

  public void removeTalker(Talker talker, String name) { 
    list.remove(name); 
    talkers.removeElement(talker); 
  } 

} 
  
      
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.