diff options
author | Tedd Hansen | 2008-09-17 16:46:23 +0000 |
---|---|---|
committer | Tedd Hansen | 2008-09-17 16:46:23 +0000 |
commit | e94d6f12eeb3bd84a752fc401847b4c25d39cdac (patch) | |
tree | a4d4f1d91df2f2dd2d6fc715c7b8e9bb241f5784 /OpenSim/Grid/ScriptEngine/DotNetEngine/LSLLongCmdHandler.cs | |
parent | * SSL Documentation update from Lexa (diff) | |
download | opensim-SC_OLD-e94d6f12eeb3bd84a752fc401847b4c25d39cdac.zip opensim-SC_OLD-e94d6f12eeb3bd84a752fc401847b4c25d39cdac.tar.gz opensim-SC_OLD-e94d6f12eeb3bd84a752fc401847b4c25d39cdac.tar.bz2 opensim-SC_OLD-e94d6f12eeb3bd84a752fc401847b4c25d39cdac.tar.xz |
More ScriptEngine cleanup
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Grid/ScriptEngine/DotNetEngine/LSLLongCmdHandler.cs | 354 |
1 files changed, 0 insertions, 354 deletions
diff --git a/OpenSim/Grid/ScriptEngine/DotNetEngine/LSLLongCmdHandler.cs b/OpenSim/Grid/ScriptEngine/DotNetEngine/LSLLongCmdHandler.cs deleted file mode 100644 index d054a2c..0000000 --- a/OpenSim/Grid/ScriptEngine/DotNetEngine/LSLLongCmdHandler.cs +++ /dev/null | |||
@@ -1,354 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSim Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Threading; | ||
31 | using libsecondlife; | ||
32 | using OpenSim.Region.Environment.Interfaces; | ||
33 | using OpenSim.Region.Environment.Modules; | ||
34 | |||
35 | namespace OpenSim.Grid.ScriptEngine.DotNetEngine | ||
36 | { | ||
37 | /// <summary> | ||
38 | /// Handles LSL commands that takes long time and returns an event, for example timers, HTTP requests, etc. | ||
39 | /// </summary> | ||
40 | internal class LSLLongCmdHandler | ||
41 | { | ||
42 | private Thread cmdHandlerThread; | ||
43 | private int cmdHandlerThreadCycleSleepms = 100; | ||
44 | |||
45 | private ScriptEngine m_ScriptEngine; | ||
46 | |||
47 | public LSLLongCmdHandler(ScriptEngine _ScriptEngine) | ||
48 | { | ||
49 | m_ScriptEngine = _ScriptEngine; | ||
50 | |||
51 | // Start the thread that will be doing the work | ||
52 | cmdHandlerThread = new Thread(CmdHandlerThreadLoop); | ||
53 | cmdHandlerThread.Name = "CmdHandlerThread"; | ||
54 | cmdHandlerThread.Priority = ThreadPriority.BelowNormal; | ||
55 | cmdHandlerThread.IsBackground = true; | ||
56 | cmdHandlerThread.Start(); | ||
57 | } | ||
58 | |||
59 | ~LSLLongCmdHandler() | ||
60 | { | ||
61 | // Shut down thread | ||
62 | try | ||
63 | { | ||
64 | if (cmdHandlerThread != null) | ||
65 | { | ||
66 | if (cmdHandlerThread.IsAlive == true) | ||
67 | { | ||
68 | cmdHandlerThread.Abort(); | ||
69 | cmdHandlerThread.Join(); | ||
70 | } | ||
71 | } | ||
72 | } | ||
73 | catch | ||
74 | { | ||
75 | } | ||
76 | } | ||
77 | |||
78 | private void CmdHandlerThreadLoop() | ||
79 | { | ||
80 | while (true) | ||
81 | { | ||
82 | // Check timers | ||
83 | CheckTimerEvents(); | ||
84 | Thread.Sleep(25); | ||
85 | // Check HttpRequests | ||
86 | CheckHttpRequests(); | ||
87 | Thread.Sleep(25); | ||
88 | // Check XMLRPCRequests | ||
89 | CheckXMLRPCRequests(); | ||
90 | Thread.Sleep(25); | ||
91 | // Check Listeners | ||
92 | CheckListeners(); | ||
93 | Thread.Sleep(25); | ||
94 | |||
95 | // Sleep before next cycle | ||
96 | //Thread.Sleep(cmdHandlerThreadCycleSleepms); | ||
97 | } | ||
98 | } | ||
99 | |||
100 | /// <summary> | ||
101 | /// Remove a specific script (and all its pending commands) | ||
102 | /// </summary> | ||
103 | /// <param name="m_localID"></param> | ||
104 | /// <param name="m_itemID"></param> | ||
105 | public void RemoveScript(uint localID, LLUUID itemID) | ||
106 | { | ||
107 | // Remove a specific script | ||
108 | |||
109 | // Remove from: Timers | ||
110 | UnSetTimerEvents(localID, itemID); | ||
111 | // Remove from: HttpRequest | ||
112 | StopHttpRequest(localID, itemID); | ||
113 | } | ||
114 | |||
115 | #region TIMER | ||
116 | |||
117 | // | ||
118 | // TIMER | ||
119 | // | ||
120 | private class TimerClass | ||
121 | { | ||
122 | public uint localID; | ||
123 | public LLUUID itemID; | ||
124 | public double interval; | ||
125 | public DateTime next; | ||
126 | } | ||
127 | |||
128 | private List<TimerClass> Timers = new List<TimerClass>(); | ||
129 | private object TimerListLock = new object(); | ||
130 | |||
131 | public void SetTimerEvent(uint m_localID, LLUUID m_itemID, double sec) | ||
132 | { | ||
133 | Console.WriteLine("SetTimerEvent"); | ||
134 | |||
135 | // Always remove first, in case this is a re-set | ||
136 | UnSetTimerEvents(m_localID, m_itemID); | ||
137 | if (sec == 0) // Disabling timer | ||
138 | return; | ||
139 | |||
140 | // Add to timer | ||
141 | TimerClass ts = new TimerClass(); | ||
142 | ts.localID = m_localID; | ||
143 | ts.itemID = m_itemID; | ||
144 | ts.interval = sec; | ||
145 | ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval); | ||
146 | lock (TimerListLock) | ||
147 | { | ||
148 | Timers.Add(ts); | ||
149 | } | ||
150 | } | ||
151 | |||
152 | public void UnSetTimerEvents(uint m_localID, LLUUID m_itemID) | ||
153 | { | ||
154 | // Remove from timer | ||
155 | lock (TimerListLock) | ||
156 | { | ||
157 | List<TimerClass> NewTimers = new List<TimerClass>(); | ||
158 | foreach (TimerClass ts in Timers) | ||
159 | { | ||
160 | if (ts.localID != m_localID && ts.itemID != m_itemID) | ||
161 | { | ||
162 | NewTimers.Add(ts); | ||
163 | } | ||
164 | } | ||
165 | Timers.Clear(); | ||
166 | Timers = NewTimers; | ||
167 | } | ||
168 | } | ||
169 | |||
170 | public void CheckTimerEvents() | ||
171 | { | ||
172 | // Nothing to do here? | ||
173 | if (Timers.Count == 0) | ||
174 | return; | ||
175 | |||
176 | lock (TimerListLock) | ||
177 | { | ||
178 | // Go through all timers | ||
179 | foreach (TimerClass ts in Timers) | ||
180 | { | ||
181 | // Time has passed? | ||
182 | if (ts.next.ToUniversalTime() < DateTime.Now.ToUniversalTime()) | ||
183 | { | ||
184 | // Add it to queue | ||
185 | m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(ts.localID, ts.itemID, "timer", | ||
186 | new object[] {}); | ||
187 | // set next interval | ||
188 | |||
189 | |||
190 | ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval); | ||
191 | } | ||
192 | } | ||
193 | } | ||
194 | } | ||
195 | |||
196 | #endregion | ||
197 | |||
198 | #region HTTP REQUEST | ||
199 | |||
200 | // | ||
201 | // HTTP REAQUEST | ||
202 | // | ||
203 | private class HttpClass | ||
204 | { | ||
205 | public uint localID; | ||
206 | public LLUUID itemID; | ||
207 | public string url; | ||
208 | public List<string> parameters; | ||
209 | public string body; | ||
210 | public DateTime next; | ||
211 | |||
212 | public string response_request_id; | ||
213 | public int response_status; | ||
214 | public List<string> response_metadata; | ||
215 | public string response_body; | ||
216 | |||
217 | public void SendRequest() | ||
218 | { | ||
219 | // TODO: SEND REQUEST!!! | ||
220 | } | ||
221 | |||
222 | public void Stop() | ||
223 | { | ||
224 | // TODO: Cancel any ongoing request | ||
225 | } | ||
226 | |||
227 | public bool CheckResponse() | ||
228 | { | ||
229 | // TODO: Check if we got a response yet, return true if so -- false if not | ||
230 | return true; | ||
231 | |||
232 | // TODO: If we got a response, set the following then return true | ||
233 | //response_request_id | ||
234 | //response_status | ||
235 | //response_metadata | ||
236 | //response_body | ||
237 | } | ||
238 | } | ||
239 | |||
240 | private List<HttpClass> HttpRequests = new List<HttpClass>(); | ||
241 | private object HttpListLock = new object(); | ||
242 | |||
243 | public void StartHttpRequest(uint localID, LLUUID itemID, string url, List<string> parameters, string body) | ||
244 | { | ||
245 | Console.WriteLine("StartHttpRequest"); | ||
246 | |||
247 | HttpClass htc = new HttpClass(); | ||
248 | htc.localID = localID; | ||
249 | htc.itemID = itemID; | ||
250 | htc.url = url; | ||
251 | htc.parameters = parameters; | ||
252 | htc.body = body; | ||
253 | lock (HttpListLock) | ||
254 | { | ||
255 | //ADD REQUEST | ||
256 | HttpRequests.Add(htc); | ||
257 | } | ||
258 | } | ||
259 | |||
260 | public void StopHttpRequest(uint m_localID, LLUUID m_itemID) | ||
261 | { | ||
262 | // Remove from list | ||
263 | lock (HttpListLock) | ||
264 | { | ||
265 | List<HttpClass> NewHttpList = new List<HttpClass>(); | ||
266 | foreach (HttpClass ts in HttpRequests) | ||
267 | { | ||
268 | if (ts.localID != m_localID && ts.itemID != m_itemID) | ||
269 | { | ||
270 | // Keeping this one | ||
271 | NewHttpList.Add(ts); | ||
272 | } | ||
273 | else | ||
274 | { | ||
275 | // Shutting this one down | ||
276 | ts.Stop(); | ||
277 | } | ||
278 | } | ||
279 | HttpRequests.Clear(); | ||
280 | HttpRequests = NewHttpList; | ||
281 | } | ||
282 | } | ||
283 | |||
284 | public void CheckHttpRequests() | ||
285 | { | ||
286 | // Nothing to do here? | ||
287 | if (HttpRequests.Count == 0) | ||
288 | return; | ||
289 | |||
290 | lock (HttpListLock) | ||
291 | { | ||
292 | foreach (HttpClass ts in HttpRequests) | ||
293 | { | ||
294 | if (ts.CheckResponse() == true) | ||
295 | { | ||
296 | // Add it to event queue | ||
297 | //key request_id, integer status, list metadata, string body | ||
298 | object[] resobj = | ||
299 | new object[] | ||
300 | {ts.response_request_id, ts.response_status, ts.response_metadata, ts.response_body}; | ||
301 | m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(ts.localID, ts.itemID, "http_response", | ||
302 | resobj); | ||
303 | // Now stop it | ||
304 | StopHttpRequest(ts.localID, ts.itemID); | ||
305 | } | ||
306 | } | ||
307 | } | ||
308 | } | ||
309 | |||
310 | #endregion | ||
311 | |||
312 | public void CheckXMLRPCRequests() | ||
313 | { | ||
314 | IXMLRPC xmlrpc = m_ScriptEngine.World.RequestModuleInterface<IXMLRPC>(); | ||
315 | |||
316 | while (xmlrpc.hasRequests()) | ||
317 | { | ||
318 | RPCRequestInfo rInfo = xmlrpc.GetNextRequest(); | ||
319 | Console.WriteLine("PICKED REQUEST"); | ||
320 | |||
321 | //Deliver data to prim's remote_data handler | ||
322 | object[] resobj = new object[] | ||
323 | { | ||
324 | 2, rInfo.GetChannelKey().ToString(), rInfo.GetMessageID().ToString(), "", rInfo.GetIntValue(), | ||
325 | rInfo.GetStrVal() | ||
326 | }; | ||
327 | m_ScriptEngine.m_EventQueueManager.AddToScriptQueue( | ||
328 | rInfo.GetLocalID(), rInfo.GetItemID(), "remote_data", resobj | ||
329 | ); | ||
330 | } | ||
331 | } | ||
332 | |||
333 | public void CheckListeners() | ||
334 | { | ||
335 | IWorldComm comms = m_ScriptEngine.World.RequestModuleInterface<IWorldComm>(); | ||
336 | |||
337 | while (comms.HasMessages()) | ||
338 | { | ||
339 | ListenerInfo lInfo = comms.GetNextMessage(); | ||
340 | Console.WriteLine("PICKED LISTENER"); | ||
341 | |||
342 | //Deliver data to prim's listen handler | ||
343 | object[] resobj = new object[] | ||
344 | { | ||
345 | lInfo.GetChannel(), lInfo.GetName(), lInfo.GetID().ToString(), lInfo.GetMessage() | ||
346 | }; | ||
347 | |||
348 | m_ScriptEngine.m_EventQueueManager.AddToScriptQueue( | ||
349 | lInfo.GetLocalID(), lInfo.GetItemID(), "listen", resobj | ||
350 | ); | ||
351 | } | ||
352 | } | ||
353 | } | ||
354 | } | ||