KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > bridges > event > EventBridge


1 package rero.bridges.event;
2  
3 import java.util.*;
4 import java.io.*;
5
6 import sleep.engine.*;
7 import sleep.interfaces.*;
8 import sleep.runtime.*;
9
10 import sleep.engine.atoms.*;
11
12 import rero.ircfw.*;
13 import rero.ircfw.interfaces.*;
14
15 /** Man, I feel bad for the mantainer of this class. Oh wait thats me. Actually its not that bad.
16     However this class isn't just as straight forward as registering any kind of event and going
17     with it one way. Nope, there are 3 ways events can be handled.
18
19     <pre>
20     1. normal chat event i.e. on JOIN for a channel join
21        this is handled by adding the code to a collapsed listener (i.e. one real listener attached to
22        the framework for the JOIN event). If the collapsed listener does not exist it is created and
23        registered with the irc framework.
24
25     2. temporary chat event i.e. on PART for a channel part
26        temporary chat events are handled by just creating a real listener and attaching it to the framework
27        as a temporary listener. No muss, no fuss.
28
29     3. "registered" listeners i.e. on WINDOW
30        registered listeners are listeners that aren't really IRC events. They have there own framework
31        for dealing with themselves. There is a super class ScriptedEventListener that provides the interface
32        the event bridge works with. Other frameworks have a listener class that extends ScriptedEventListener
33        and know to fire dispatchEvent when the listener is fired. This should be more efficient as it keeps
34        overhead low on some high occurence events (i.e. on WINDOW) and keeps the framework from being mucked
35        up with tons of events. All valuable things right.
36     </pre>
37 **/

38
39 public class EventBridge implements Loadable, Environment, PredicateEnvironment, FilterEnvironment
40 {
41 // public ScriptEnvironment environment;
42
protected HashMap listeners;
43     protected ChatFramework framework;
44
45     protected HashMap registeredEvents;
46
47     protected HashMap unloadEvents;
48
49     public EventBridge()
50     {
51         listeners = new HashMap();
52         registeredEvents = new HashMap();
53         unloadEvents = new HashMap();
54     }
55   
56     public void announceFramework(ChatFramework f)
57     {
58         framework = f;
59     }
60
61     public boolean scriptUnloaded (ScriptInstance si)
62     {
63         if (unloadEvents.get(si) != null)
64         {
65            Block code = (Block)unloadEvents.get(si);
66            unloadEvents.remove(si);
67
68            synchronized (si.getScriptEnvironment().getScriptVariables())
69            {
70               si.getScriptEnvironment().getScriptVariables().pushLocalLevel();
71               SleepUtils.runCode(code, si.getScriptEnvironment());
72               si.getScriptEnvironment().getScriptVariables().popLocalLevel();
73            }
74         }
75
76         //
77
// listeners check themselves to make sure the associated script is still loaded.
78
// If its not they automatically request to be removed. Theres one way around that
79
// unload problem.
80
//
81
return true;
82     }
83
84     public boolean scriptLoaded (ScriptInstance si)
85     {
86         Hashtable _env = si.getScriptEnvironment().getEnvironment();
87         _env.put("on", this); // since we implement both interfaces this should be okay.
88
_env.put("wait", this);
89
90 // environment = si.getScriptEnvironment();
91

92         return true;
93     }
94
95     /** adds another type of "event" for the event bridge to manage. By default events are just registered with the irc
96         framework. ScriptedEventListener allows any sort of event to be incorporated into the client scripting */

97     public void registerEvent(String JavaDoc name, ScriptedEventListener listener)
98     {
99         registeredEvents.put(name.toUpperCase(), listener);
100     }
101
102     protected EventChatListener getNewListenerFor(String JavaDoc event_name)
103     {
104         if (event_name.equals("PUBLIC"))
105         {
106            return new PublicChatListener("PRIVMSG");
107         }
108
109         if (event_name.equals("PUBLIC_ACTION"))
110         {
111            return new PublicChatListener("ACTION");
112         }
113
114         if (event_name.equals("PRIVATE_ACTION"))
115         {
116            return new PrivateChatListener("ACTION");
117         }
118
119         if (event_name.equals("MSG"))
120         {
121            return new PrivateChatListener("PRIVMSG");
122         }
123
124         if (event_name.length() > 6 && event_name.substring(0, 5).equals("REPL_"))
125         {
126            return new GeneralChatListener(event_name.substring(5, event_name.length()));
127         }
128
129         return new GeneralChatListener(event_name);
130     }
131
132     //
133
// returns a "collapsed" listener. Just trying to reduce the overhead of firing an event.
134
//
135
protected EventChatListener getListenerFor(String JavaDoc event_name)
136     {
137         if (listeners.get(event_name) == null)
138         {
139            listeners.put(event_name, getNewListenerFor(event_name));
140            framework.addChatListener((ChatListener)listeners.get(event_name));
141         }
142
143         return (EventChatListener)listeners.get(event_name);
144     }
145
146     public void bindFilteredFunction(ScriptInstance si, String JavaDoc typeKeyword, String JavaDoc keyword, String JavaDoc filter, Block functionBody)
147     {
148         FilterChatListener temp = new FilterChatListener(si.getScriptEnvironment(), keyword, filter, new CodeSnippet(functionBody, si.getScriptEnvironment()));
149
150         if (typeKeyword.equals("on"))
151         {
152            framework.addChatListener((ChatListener)temp);
153         }
154
155         if (typeKeyword.equals("wait"))
156         {
157            temp.setFlags(ChatListener.REMOVE_LISTENER);
158            framework.addTemporaryListener(temp);
159         }
160     }
161
162     public void bindPredicate(ScriptInstance si, String JavaDoc type, Check pred, Block code)
163     {
164         PredicateChatListener temp = new PredicateChatListener(si.getScriptEnvironment(), pred, new CodeSnippet(code, si.getScriptEnvironment()));
165
166         if (type.equals("on"))
167         {
168            framework.addChatListener((ChatListener)temp);
169         }
170
171         if (type.equals("wait"))
172         {
173            temp.setFlags(ChatListener.REMOVE_LISTENER); // tells it to flag all temp events as removeable.
174
framework.addTemporaryListener(temp);
175         }
176     }
177
178     public void bindFunction(ScriptInstance si, String JavaDoc type, String JavaDoc name, Block code)
179     {
180         name = name.toUpperCase();
181
182         if (type.equals("on") && name.equals("UNLOAD"))
183         {
184            unloadEvents.put(si, code);
185         }
186         else if (registeredEvents.get(name) != null)
187         {
188            ScriptedEventListener temp = (ScriptedEventListener)registeredEvents.get(name);
189            if (!temp.isSetup())
190            {
191               temp.setupListener();
192               temp.setRegistered();
193            }
194            
195            if (type.equals("on"))
196            {
197               temp.addListener(new CodeSnippet(code, si.getScriptEnvironment()));
198            }
199
200            if (type.equals("wait"))
201            {
202               temp.addTemporaryListener(new CodeSnippet(code, si.getScriptEnvironment()));
203            }
204
205            return;
206         }
207         else if (type.equals("on"))
208         {
209            // when the listener is created it is added to the umm thingy.
210
EventChatListener temp = getListenerFor(name);
211            temp.addListener(new CodeSnippet(code, si.getScriptEnvironment()));
212         }
213         else if (type.equals("wait"))
214         {
215            EventChatListener temp = getNewListenerFor(name);
216            temp.addListener(new CodeSnippet(code, si.getScriptEnvironment()));
217            temp.setFlags(ChatListener.REMOVE_LISTENER); // tells it to flag all temp events as removeable.
218

219            framework.addTemporaryListener(temp);
220         }
221     }
222 }
223
224
Popular Tags