clockwerk-opensim-stable – Blame information for rev 2
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 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 OpenSimulator 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; |
||
30 | using System.Collections.Generic; |
||
31 | using System.Reflection; |
||
32 | using System.Threading; |
||
33 | using log4net; |
||
34 | using OpenMetaverse; |
||
35 | using OpenSim.Framework; |
||
36 | using OpenSim.Framework.Monitoring; |
||
37 | using OpenSim.Region.Framework.Interfaces; |
||
38 | using OpenSim.Region.ScriptEngine.Interfaces; |
||
39 | using OpenSim.Region.ScriptEngine.Shared; |
||
40 | using OpenSim.Region.ScriptEngine.Shared.Api.Plugins; |
||
41 | using Timer=OpenSim.Region.ScriptEngine.Shared.Api.Plugins.Timer; |
||
42 | |||
43 | namespace OpenSim.Region.ScriptEngine.Shared.Api |
||
44 | { |
||
45 | /// <summary> |
||
46 | /// Handles LSL commands that takes long time and returns an event, for example timers, HTTP requests, etc. |
||
47 | /// </summary> |
||
48 | public class AsyncCommandManager |
||
49 | { |
||
50 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
||
51 | |||
52 | private static int cmdHandlerThreadCycleSleepms; |
||
2 | vero | 53 | private static readonly System.Timers.Timer cmdEventTimer = new System.Timers.Timer(); |
1 | vero | 54 | |
55 | /// <summary> |
||
56 | /// Lock for reading/writing static components of AsyncCommandManager. |
||
57 | /// </summary> |
||
58 | /// <remarks> |
||
59 | /// This lock exists so that multiple threads from different engines and/or different copies of the same engine |
||
60 | /// are prevented from running non-thread safe code (e.g. read/write of lists) concurrently. |
||
61 | /// </remarks> |
||
62 | private static object staticLock = new object(); |
||
63 | |||
64 | private static List<IScriptEngine> m_ScriptEngines = |
||
65 | new List<IScriptEngine>(); |
||
66 | |||
67 | public IScriptEngine m_ScriptEngine; |
||
68 | |||
69 | private static Dictionary<IScriptEngine, Dataserver> m_Dataserver = |
||
70 | new Dictionary<IScriptEngine, Dataserver>(); |
||
71 | private static Dictionary<IScriptEngine, Timer> m_Timer = |
||
72 | new Dictionary<IScriptEngine, Timer>(); |
||
73 | private static Dictionary<IScriptEngine, Listener> m_Listener = |
||
74 | new Dictionary<IScriptEngine, Listener>(); |
||
75 | private static Dictionary<IScriptEngine, HttpRequest> m_HttpRequest = |
||
76 | new Dictionary<IScriptEngine, HttpRequest>(); |
||
77 | private static Dictionary<IScriptEngine, SensorRepeat> m_SensorRepeat = |
||
78 | new Dictionary<IScriptEngine, SensorRepeat>(); |
||
79 | private static Dictionary<IScriptEngine, XmlRequest> m_XmlRequest = |
||
80 | new Dictionary<IScriptEngine, XmlRequest>(); |
||
81 | |||
82 | public Dataserver DataserverPlugin |
||
83 | { |
||
84 | get |
||
85 | { |
||
86 | lock (staticLock) |
||
87 | return m_Dataserver[m_ScriptEngine]; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | public Timer TimerPlugin |
||
92 | { |
||
93 | get |
||
94 | { |
||
95 | lock (staticLock) |
||
96 | return m_Timer[m_ScriptEngine]; |
||
97 | } |
||
98 | } |
||
99 | |||
100 | public HttpRequest HttpRequestPlugin |
||
101 | { |
||
102 | get |
||
103 | { |
||
104 | lock (staticLock) |
||
105 | return m_HttpRequest[m_ScriptEngine]; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | public Listener ListenerPlugin |
||
110 | { |
||
111 | get |
||
112 | { |
||
113 | lock (staticLock) |
||
114 | return m_Listener[m_ScriptEngine]; |
||
115 | } |
||
116 | } |
||
117 | |||
118 | public SensorRepeat SensorRepeatPlugin |
||
119 | { |
||
120 | get |
||
121 | { |
||
122 | lock (staticLock) |
||
123 | return m_SensorRepeat[m_ScriptEngine]; |
||
124 | } |
||
125 | } |
||
126 | |||
127 | public XmlRequest XmlRequestPlugin |
||
128 | { |
||
129 | get |
||
130 | { |
||
131 | lock (staticLock) |
||
132 | return m_XmlRequest[m_ScriptEngine]; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | public IScriptEngine[] ScriptEngines |
||
137 | { |
||
138 | get |
||
139 | { |
||
140 | lock (staticLock) |
||
141 | return m_ScriptEngines.ToArray(); |
||
142 | } |
||
143 | } |
||
144 | |||
145 | public AsyncCommandManager(IScriptEngine _ScriptEngine) |
||
146 | { |
||
147 | m_ScriptEngine = _ScriptEngine; |
||
148 | |||
149 | // If there is more than one scene in the simulator or multiple script engines are used on the same region |
||
150 | // then more than one thread could arrive at this block of code simultaneously. However, it cannot be |
||
151 | // executed concurrently both because concurrent list operations are not thread-safe and because of other |
||
152 | // race conditions such as the later check of cmdHandlerThread == null. |
||
153 | lock (staticLock) |
||
154 | { |
||
155 | if (m_ScriptEngines.Count == 0) |
||
156 | ReadConfig(); |
||
157 | |||
158 | if (!m_ScriptEngines.Contains(m_ScriptEngine)) |
||
159 | m_ScriptEngines.Add(m_ScriptEngine); |
||
160 | |||
161 | // Create instances of all plugins |
||
162 | if (!m_Dataserver.ContainsKey(m_ScriptEngine)) |
||
163 | m_Dataserver[m_ScriptEngine] = new Dataserver(this); |
||
164 | if (!m_Timer.ContainsKey(m_ScriptEngine)) |
||
165 | m_Timer[m_ScriptEngine] = new Timer(this); |
||
166 | if (!m_HttpRequest.ContainsKey(m_ScriptEngine)) |
||
167 | m_HttpRequest[m_ScriptEngine] = new HttpRequest(this); |
||
168 | if (!m_Listener.ContainsKey(m_ScriptEngine)) |
||
169 | m_Listener[m_ScriptEngine] = new Listener(this); |
||
170 | if (!m_SensorRepeat.ContainsKey(m_ScriptEngine)) |
||
171 | m_SensorRepeat[m_ScriptEngine] = new SensorRepeat(this); |
||
172 | if (!m_XmlRequest.ContainsKey(m_ScriptEngine)) |
||
173 | m_XmlRequest[m_ScriptEngine] = new XmlRequest(this); |
||
174 | |||
2 | vero | 175 | if (cmdEventTimer.Enabled.Equals(true)) return; |
1 | vero | 176 | |
2 | vero | 177 | // Start the timer |
178 | cmdEventTimer.Elapsed += (sender, args) => |
||
179 | { |
||
180 | try |
||
181 | { |
||
182 | DoOneCmdHandlerPass(); |
||
183 | } |
||
184 | catch (Exception e) |
||
185 | { |
||
186 | m_log.Error("[ASYNC COMMAND MANAGER]: Exception in command handler pass: ", e); |
||
187 | } |
||
188 | }; |
||
189 | |||
190 | cmdEventTimer.Interval = cmdHandlerThreadCycleSleepms; |
||
191 | cmdEventTimer.Enabled = true; |
||
192 | |||
1 | vero | 193 | } |
194 | } |
||
195 | |||
196 | private void ReadConfig() |
||
197 | { |
||
198 | // cmdHandlerThreadCycleSleepms = m_ScriptEngine.Config.GetInt("AsyncLLCommandLoopms", 100); |
||
199 | // TODO: Make this sane again |
||
2 | vero | 200 | cmdHandlerThreadCycleSleepms = 50; |
1 | vero | 201 | } |
202 | |||
203 | ~AsyncCommandManager() |
||
204 | { |
||
205 | // Shut down thread |
||
206 | // try |
||
207 | // { |
||
208 | // if (cmdHandlerThread != null) |
||
209 | // { |
||
210 | // if (cmdHandlerThread.IsAlive == true) |
||
211 | // { |
||
212 | // cmdHandlerThread.Abort(); |
||
213 | // //cmdHandlerThread.Join(); |
||
214 | // } |
||
215 | // } |
||
216 | // } |
||
217 | // catch |
||
218 | // { |
||
219 | // } |
||
220 | } |
||
221 | |||
222 | /// <summary> |
||
223 | /// Main loop for the manager thread |
||
224 | /// </summary> |
||
225 | private static void CmdHandlerThreadLoop() |
||
226 | { |
||
227 | while (true) |
||
228 | { |
||
229 | try |
||
230 | { |
||
231 | Thread.Sleep(cmdHandlerThreadCycleSleepms); |
||
232 | |||
233 | DoOneCmdHandlerPass(); |
||
234 | |||
235 | Watchdog.UpdateThread(); |
||
236 | } |
||
237 | catch (Exception e) |
||
238 | { |
||
239 | m_log.Error("[ASYNC COMMAND MANAGER]: Exception in command handler pass: ", e); |
||
240 | } |
||
241 | } |
||
242 | } |
||
243 | |||
244 | private static void DoOneCmdHandlerPass() |
||
245 | { |
||
246 | lock (staticLock) |
||
247 | { |
||
248 | // Check HttpRequests |
||
249 | m_HttpRequest[m_ScriptEngines[0]].CheckHttpRequests(); |
||
250 | |||
251 | // Check XMLRPCRequests |
||
252 | m_XmlRequest[m_ScriptEngines[0]].CheckXMLRPCRequests(); |
||
253 | |||
254 | foreach (IScriptEngine s in m_ScriptEngines) |
||
255 | { |
||
256 | // Check Listeners |
||
257 | m_Listener[s].CheckListeners(); |
||
258 | |||
259 | // Check timers |
||
260 | m_Timer[s].CheckTimerEvents(); |
||
261 | |||
262 | // Check Sensors |
||
263 | m_SensorRepeat[s].CheckSenseRepeaterEvents(); |
||
264 | |||
265 | // Check dataserver |
||
266 | m_Dataserver[s].ExpireRequests(); |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 | |||
271 | /// <summary> |
||
272 | /// Remove a specific script (and all its pending commands) |
||
273 | /// </summary> |
||
274 | /// <param name="localID"></param> |
||
275 | /// <param name="itemID"></param> |
||
276 | public static void RemoveScript(IScriptEngine engine, uint localID, UUID itemID) |
||
277 | { |
||
278 | // m_log.DebugFormat("[ASYNC COMMAND MANAGER]: Removing facilities for script {0}", itemID); |
||
279 | |||
280 | lock (staticLock) |
||
281 | { |
||
282 | // Remove dataserver events |
||
283 | m_Dataserver[engine].RemoveEvents(localID, itemID); |
||
284 | |||
285 | // Remove from: Timers |
||
286 | m_Timer[engine].UnSetTimerEvents(localID, itemID); |
||
287 | |||
288 | // Remove from: HttpRequest |
||
289 | IHttpRequestModule iHttpReq = engine.World.RequestModuleInterface<IHttpRequestModule>(); |
||
290 | if (iHttpReq != null) |
||
291 | iHttpReq.StopHttpRequestsForScript(itemID); |
||
292 | |||
293 | IWorldComm comms = engine.World.RequestModuleInterface<IWorldComm>(); |
||
294 | if (comms != null) |
||
295 | comms.DeleteListener(itemID); |
||
296 | |||
297 | IXMLRPC xmlrpc = engine.World.RequestModuleInterface<IXMLRPC>(); |
||
298 | if (xmlrpc != null) |
||
299 | { |
||
300 | xmlrpc.DeleteChannels(itemID); |
||
301 | xmlrpc.CancelSRDRequests(itemID); |
||
302 | } |
||
303 | |||
304 | // Remove Sensors |
||
305 | m_SensorRepeat[engine].UnSetSenseRepeaterEvents(localID, itemID); |
||
306 | } |
||
307 | } |
||
308 | |||
309 | /// <summary> |
||
310 | /// Get the sensor repeat plugin for this script engine. |
||
311 | /// </summary> |
||
312 | /// <param name="engine"></param> |
||
313 | /// <returns></returns> |
||
314 | public static SensorRepeat GetSensorRepeatPlugin(IScriptEngine engine) |
||
315 | { |
||
316 | lock (staticLock) |
||
317 | { |
||
318 | if (m_SensorRepeat.ContainsKey(engine)) |
||
319 | return m_SensorRepeat[engine]; |
||
320 | else |
||
321 | return null; |
||
322 | } |
||
323 | } |
||
324 | |||
325 | /// <summary> |
||
326 | /// Get the dataserver plugin for this script engine. |
||
327 | /// </summary> |
||
328 | /// <param name="engine"></param> |
||
329 | /// <returns></returns> |
||
330 | public static Dataserver GetDataserverPlugin(IScriptEngine engine) |
||
331 | { |
||
332 | lock (staticLock) |
||
333 | { |
||
334 | if (m_Dataserver.ContainsKey(engine)) |
||
335 | return m_Dataserver[engine]; |
||
336 | else |
||
337 | return null; |
||
338 | } |
||
339 | } |
||
340 | |||
341 | /// <summary> |
||
342 | /// Get the timer plugin for this script engine. |
||
343 | /// </summary> |
||
344 | /// <param name="engine"></param> |
||
345 | /// <returns></returns> |
||
346 | public static Timer GetTimerPlugin(IScriptEngine engine) |
||
347 | { |
||
348 | lock (staticLock) |
||
349 | { |
||
350 | if (m_Timer.ContainsKey(engine)) |
||
351 | return m_Timer[engine]; |
||
352 | else |
||
353 | return null; |
||
354 | } |
||
355 | } |
||
356 | |||
357 | /// <summary> |
||
358 | /// Get the listener plugin for this script engine. |
||
359 | /// </summary> |
||
360 | /// <param name="engine"></param> |
||
361 | /// <returns></returns> |
||
362 | public static Listener GetListenerPlugin(IScriptEngine engine) |
||
363 | { |
||
364 | lock (staticLock) |
||
365 | { |
||
366 | if (m_Listener.ContainsKey(engine)) |
||
367 | return m_Listener[engine]; |
||
368 | else |
||
369 | return null; |
||
370 | } |
||
371 | } |
||
372 | |||
373 | public static Object[] GetSerializationData(IScriptEngine engine, UUID itemID) |
||
374 | { |
||
375 | List<Object> data = new List<Object>(); |
||
376 | |||
377 | lock (staticLock) |
||
378 | { |
||
379 | Object[] listeners = m_Listener[engine].GetSerializationData(itemID); |
||
380 | if (listeners.Length > 0) |
||
381 | { |
||
382 | data.Add("listener"); |
||
383 | data.Add(listeners.Length); |
||
384 | data.AddRange(listeners); |
||
385 | } |
||
386 | |||
387 | Object[] timers=m_Timer[engine].GetSerializationData(itemID); |
||
388 | if (timers.Length > 0) |
||
389 | { |
||
390 | data.Add("timer"); |
||
391 | data.Add(timers.Length); |
||
392 | data.AddRange(timers); |
||
393 | } |
||
394 | |||
395 | Object[] sensors = m_SensorRepeat[engine].GetSerializationData(itemID); |
||
396 | if (sensors.Length > 0) |
||
397 | { |
||
398 | data.Add("sensor"); |
||
399 | data.Add(sensors.Length); |
||
400 | data.AddRange(sensors); |
||
401 | } |
||
402 | } |
||
403 | |||
404 | return data.ToArray(); |
||
405 | } |
||
406 | |||
407 | public static void CreateFromData(IScriptEngine engine, uint localID, |
||
408 | UUID itemID, UUID hostID, Object[] data) |
||
409 | { |
||
410 | int idx = 0; |
||
411 | int len; |
||
412 | |||
413 | while (idx < data.Length) |
||
414 | { |
||
415 | string type = data[idx].ToString(); |
||
416 | len = (int)data[idx+1]; |
||
417 | idx+=2; |
||
418 | |||
419 | if (len > 0) |
||
420 | { |
||
421 | Object[] item = new Object[len]; |
||
422 | Array.Copy(data, idx, item, 0, len); |
||
423 | |||
424 | idx+=len; |
||
425 | |||
426 | lock (staticLock) |
||
427 | { |
||
428 | switch (type) |
||
429 | { |
||
430 | case "listener": |
||
431 | m_Listener[engine].CreateFromData(localID, itemID, |
||
432 | hostID, item); |
||
433 | break; |
||
434 | case "timer": |
||
435 | m_Timer[engine].CreateFromData(localID, itemID, |
||
436 | hostID, item); |
||
437 | break; |
||
438 | case "sensor": |
||
439 | m_SensorRepeat[engine].CreateFromData(localID, |
||
440 | itemID, hostID, item); |
||
441 | break; |
||
442 | } |
||
443 | } |
||
444 | } |
||
445 | } |
||
446 | } |
||
447 | } |
||
2 | vero | 448 | } |