/** * Stop listening for messages of the given type with the given listener. * * @param m specify message type and template we're listening for * @param listener destination for received messages */ public void deregisterListener(Message template, MessageListener listener) { Integer amType = new Integer(template.amType()); Vector vec = (Vector) templateTbl.get(amType); if (vec == null) { throw new IllegalArgumentException( "No listeners registered for message type " + template.getClass().getName() + " (AM type " + template.amType() + ")"); } msgTemplate mt = new msgTemplate(template, listener); // Remove all occurrences while (vec.removeElement(mt)) ; if (vec.size() == 0) templateTbl.remove(amType); }
/** * Register a particular listener for a particular message type. More than one listener can be * registered for each message type. * * @param m specify message type and template we're listening for * @param listener destination for received messages */ public void registerListener(Message template, MessageListener listener) { Integer amType = new Integer(template.amType()); Vector vec = (Vector) templateTbl.get(amType); if (vec == null) { vec = new Vector(); } vec.addElement(new msgTemplate(template, listener)); templateTbl.put(amType, vec); }
public static void main(String[] args) throws Exception { String source = null; Vector v = new Vector(); if (args.length > 0) { for (int i = 0; i < args.length; i++) { if (args[i].equals("-comm")) { source = args[++i]; } else { String className = args[i]; try { Class c = Class.forName(className); Object packet = c.newInstance(); Message msg = (Message) packet; if (msg.amType() < 0) { System.err.println(className + " does not have an AM type - ignored"); } else { v.addElement(msg); } } catch (Exception e) { System.err.println(e); } } } } else if (args.length != 0) { usage(); System.exit(1); } MsgReader mr = new MsgReader(source); Enumeration msgs = v.elements(); while (msgs.hasMoreElements()) { Message m = (Message) msgs.nextElement(); mr.addMsgType(m); } mr.start(); }