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