1 package rero.ircfw; 2 3 import rero.ircfw.interfaces.FrameworkConstants; 4 import rero.ircfw.interfaces.ChatListener; 5 6 import java.util.HashMap ; 7 import java.util.List ; 8 import java.util.LinkedList ; 9 import java.util.ListIterator ; 10 11 public class ProtocolDispatcher implements FrameworkConstants 12 { 13 protected LinkedList temporary = new LinkedList (); 14 protected LinkedList permanent = new LinkedList (); 15 16 protected ChatListener internal = null; 17 18 public void dispatchEvent(HashMap eventDescription) 19 { 20 String eventId = (String )eventDescription.get($EVENT$); 21 22 int rv = ChatListener.EVENT_DONE; 23 24 if (internal != null && internal.isChatEvent(eventId, eventDescription)) 25 { 26 rv = internal.fireChatEvent(eventDescription); 27 } 28 29 if (rv == ChatListener.EVENT_DONE) 30 { 31 rv = easyDispatch(temporary, eventId, eventDescription); 32 } 33 34 if (rv == ChatListener.EVENT_DONE) 35 { 36 rv = easyDispatch(permanent, eventId, eventDescription); 37 } 38 } 39 40 private int easyDispatch(List listeners, String eventId, HashMap eventDescription) 41 { 42 ChatListener l; 43 44 ListIterator iter = listeners.listIterator(); 45 while (iter.hasNext()) 46 { 47 l = (ChatListener)iter.next(); 48 49 if (l.isChatEvent(eventId, eventDescription)) 50 { 51 int rv = l.fireChatEvent(eventDescription); 52 53 if ((rv & (ChatListener.REMOVE_LISTENER)) == ChatListener.REMOVE_LISTENER) 54 { 55 iter.remove(); 56 } 57 58 if ((rv & (ChatListener.EVENT_HALT)) == ChatListener.EVENT_HALT) 59 { 60 return ChatListener.EVENT_HALT; 61 } 62 } 63 } 64 65 return ChatListener.EVENT_DONE; 66 } 67 68 public void addTemporaryListener(ChatListener l) 69 { 70 temporary.addFirst(l); 71 } 72 73 public void addChatListener(ChatListener l) 74 { 75 permanent.addFirst(l); 76 } 77 78 public void setInternalListener(ChatListener l) 79 { 80 internal = l; 81 } 82 } 83 84 85 | Popular Tags |