FileDocCategorySizeDatePackage
EventTransceiver.javaAPI DocExample4525Tue Jan 20 21:43:38 GMT 1998dcj.util.message

EventTransceiver

public class EventTransceiver extends Thread implements EventHandler
Source code from "Java Distributed Computing", by Jim Farley. Class: EventTransceiver Example: 6-13 Description: One end of an event-based network communication scheme.

Fields Summary
private Hashtable
handlers
private Vector
globalHandlers
InputStream
evIn
OutputStream
evOut
Constructors Summary
public EventTransceiver(String host, int port)


       
    try {
      InetAddress a = InetAddress.getByName(host);
      connect(a, port);
    }
    catch (Exception e) {}
  
public EventTransceiver(InetAddress a, int port)

    connect(a, port);
  
public EventTransceiver(InputStream in, OutputStream out)

    setStreams(in, out);
  
Methods Summary
public voidaddHandler(EventHandler eh)

    if (!globalHandlers.contains(eh)) {
      globalHandlers.addElement(eh);
    }
  
public voidaddHandler(EventHandler eh, java.util.EventObject e)

    Class eClass = e.getClass();
    addHandler(eh, eClass);
  
public voidaddHandler(EventHandler eh, java.lang.Class ec)

    Vector evHandlers = (Vector)handlers.get(ec);
    if (evHandlers == null) {
      evHandlers = new Vector();
      handlers.put(ec, evHandlers);
    }
    if (!evHandlers.contains(eh)) {
      evHandlers.addElement(eh);
    }
  
voidconnect(java.net.InetAddress a, int port)

    try {
      Socket s = new Socket(a, port);
      evIn = s.getInputStream();
      evOut = s.getOutputStream();
    }
    catch (Exception e) {
      evIn = null;
      evOut = null;
    }
  
voiddistributeEvent(java.util.EventObject ev)

    // Send event to all "global" handlers
    Enumeration e = globalHandlers.elements();
    while (e.hasMoreElements()){
      EventHandler h = (EventHandler)e.nextElement();
      h.handleEvent(ev);
    }

    // Send event to handlers targetting the event's class
    Class evClass = ev.getClass();
    Vector evHandlers = (Vector)handlers.get(evClass);
    e = evHandlers.elements();
    while (e.hasMoreElements()) {
      EventHandler h = (EventHandler)e.nextElement();
      h.handleEvent(ev);
    }
  
public voidhandleEvent(java.util.EventObject e)

java.util.EventObjectreceiveEvent()

    ObjectInputStream oin = new ObjectInputStream(evIn);
    EventObject ev = null;
    try {
      ev = (EventObject)oin.readObject();
    }
    catch (ClassCastException e) {
      System.out.println("Non-event object sent to EventTransceiver");
    }
    catch (ClassNotFoundException e2) {
      System.out.println("Unresolvable object type sent to EventTransceiver");
    }

    return ev;
  
public voidremoveHandler(EventHandler eh)

    globalHandlers.removeElement(eh);
    Enumeration ecList = handlers.keys();
    while (ecList.hasMoreElements()) {
      Vector evHandlers =
        (Vector)handlers.get(ecList.nextElement());
      if (evHandlers != null) {
        evHandlers.removeElement(eh);
      }
    }
  
public voidremoveHandler(EventHandler eh, java.util.EventObject e)

    removeHandler(eh, e.getClass());
  
public voidremoveHandler(EventHandler eh, java.lang.Class ec)

    Vector evHandlers = (Vector)handlers.get(ec);
    if (evHandlers != null) {
      evHandlers.removeElement(eh);
    }
  
public voidrun()

    try {
      while (true) {
        EventObject e = receiveEvent();
        if (e != null)
          distributeEvent(e);
      }
    }
    // Treat an IOException as termination of the event
    // input stream, and let this handler thread die
    catch (IOException e) {}
  
public voidsendEvent(java.util.EventObject ev)

    ObjectOutputStream oout = new ObjectOutputStream(evOut);
    oout.writeObject(ev);
  
voidsetStreams(java.io.InputStream in, java.io.OutputStream out)

    evIn = in;
    evOut = out;