opensim – Blame information for rev 6
?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 int cmdHandlerThreadCycleSleepms; |
||
4 | eva | 53 | private static readonly System.Timers.Timer cmdEventTimer = new System.Timers.Timer(); |
1 | eva | 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)) |
||
5 | eva | 173 | m_XmlRequest[m_ScriptEngine] = new XmlRequest(this); |
174 | |||
175 | if (cmdEventTimer.Enabled.Equals(true)) return; |
||
176 | // Start the timer |
||
177 | cmdEventTimer.Elapsed += (sender, args) => |
||
178 | { |
||
179 | try |
||
180 | { |
||
181 | DoOneCmdHandlerPass(); |
||
182 | } |
||
183 | catch (Exception e) |
||
184 | { |
||
185 | m_log.Error("[ASYNC COMMAND MANAGER]: Exception in command handler pass: ", e); |
||
186 | } |
||
187 | }; |
||
188 | cmdEventTimer.Interval = cmdHandlerThreadCycleSleepms; |
||
189 | cmdEventTimer.Enabled = true; |
||
1 | eva | 190 | } |
191 | } |
||
192 | |||
193 | private void ReadConfig() |
||
194 | { |
||
195 | // cmdHandlerThreadCycleSleepms = m_ScriptEngine.Config.GetInt("AsyncLLCommandLoopms", 100); |
||
196 | // TODO: Make this sane again |
||
6 | vero | 197 | cmdHandlerThreadCycleSleepms = 50; |
1 | eva | 198 | } |
199 | |||
200 | ~AsyncCommandManager() |
||
201 | { |
||
202 | // Shut down thread |
||
203 | // try |
||
204 | // { |
||
205 | // if (cmdHandlerThread != null) |
||
206 | // { |
||
207 | // if (cmdHandlerThread.IsAlive == true) |
||
208 | // { |
||
209 | // cmdHandlerThread.Abort(); |
||
210 | // //cmdHandlerThread.Join(); |
||
211 | // } |
||
212 | // } |
||
213 | // } |
||
214 | // catch |
||
215 | // { |
||
216 | // } |
||
217 | } |
||
218 | |||
219 | /// <summary> |
||
220 | /// Main loop for the manager thread |
||
221 | /// </summary> |
||
222 | private static void CmdHandlerThreadLoop() |
||
223 | { |
||
224 | while (true) |
||
225 | { |
||
226 | try |
||
227 | { |
||
4 | eva | 228 | Thread.Sleep(cmdHandlerThreadCycleSleepms); |
1 | eva | 229 | |
230 | DoOneCmdHandlerPass(); |
||
4 | eva | 231 | |
1 | eva | 232 | Watchdog.UpdateThread(); |
233 | } |
||
234 | catch (Exception e) |
||
235 | { |
||
236 | m_log.Error("[ASYNC COMMAND MANAGER]: Exception in command handler pass: ", e); |
||
237 | } |
||
238 | } |
||
239 | } |
||
240 | |||
241 | private static void DoOneCmdHandlerPass() |
||
242 | { |
||
243 | lock (staticLock) |
||
244 | { |
||
245 | // Check HttpRequests |
||
246 | m_HttpRequest[m_ScriptEngines[0]].CheckHttpRequests(); |
||
247 | |||
248 | // Check XMLRPCRequests |
||
249 | m_XmlRequest[m_ScriptEngines[0]].CheckXMLRPCRequests(); |
||
250 | |||
251 | foreach (IScriptEngine s in m_ScriptEngines) |
||
252 | { |
||
253 | // Check Listeners |
||
254 | m_Listener[s].CheckListeners(); |
||
255 | |||
256 | // Check timers |
||
257 | m_Timer[s].CheckTimerEvents(); |
||
258 | |||
259 | // Check Sensors |
||
260 | m_SensorRepeat[s].CheckSenseRepeaterEvents(); |
||
261 | |||
262 | // Check dataserver |
||
263 | m_Dataserver[s].ExpireRequests(); |
||
264 | } |
||
265 | } |
||
266 | } |
||
267 | |||
268 | /// <summary> |
||
269 | /// Remove a specific script (and all its pending commands) |
||
270 | /// </summary> |
||
271 | /// <param name="localID"></param> |
||
272 | /// <param name="itemID"></param> |
||
273 | public static void RemoveScript(IScriptEngine engine, uint localID, UUID itemID) |
||
274 | { |
||
275 | // m_log.DebugFormat("[ASYNC COMMAND MANAGER]: Removing facilities for script {0}", itemID); |
||
276 | |||
277 | lock (staticLock) |
||
278 | { |
||
279 | // Remove dataserver events |
||
280 | m_Dataserver[engine].RemoveEvents(localID, itemID); |
||
281 | |||
282 | // Remove from: Timers |
||
283 | m_Timer[engine].UnSetTimerEvents(localID, itemID); |
||
284 | |||
285 | // Remove from: HttpRequest |
||
286 | IHttpRequestModule iHttpReq = engine.World.RequestModuleInterface<IHttpRequestModule>(); |
||
287 | if (iHttpReq != null) |
||
288 | iHttpReq.StopHttpRequestsForScript(itemID); |
||
289 | |||
290 | IWorldComm comms = engine.World.RequestModuleInterface<IWorldComm>(); |
||
291 | if (comms != null) |
||
292 | comms.DeleteListener(itemID); |
||
293 | |||
294 | IXMLRPC xmlrpc = engine.World.RequestModuleInterface<IXMLRPC>(); |
||
295 | if (xmlrpc != null) |
||
296 | { |
||
297 | xmlrpc.DeleteChannels(itemID); |
||
298 | xmlrpc.CancelSRDRequests(itemID); |
||
299 | } |
||
300 | |||
301 | // Remove Sensors |
||
302 | m_SensorRepeat[engine].UnSetSenseRepeaterEvents(localID, itemID); |
||
303 | } |
||
304 | } |
||
305 | |||
306 | /// <summary> |
||
307 | /// Get the sensor repeat plugin for this script engine. |
||
308 | /// </summary> |
||
309 | /// <param name="engine"></param> |
||
310 | /// <returns></returns> |
||
311 | public static SensorRepeat GetSensorRepeatPlugin(IScriptEngine engine) |
||
312 | { |
||
313 | lock (staticLock) |
||
314 | { |
||
315 | if (m_SensorRepeat.ContainsKey(engine)) |
||
316 | return m_SensorRepeat[engine]; |
||
317 | else |
||
318 | return null; |
||
319 | } |
||
320 | } |
||
321 | |||
322 | /// <summary> |
||
323 | /// Get the dataserver plugin for this script engine. |
||
324 | /// </summary> |
||
325 | /// <param name="engine"></param> |
||
326 | /// <returns></returns> |
||
327 | public static Dataserver GetDataserverPlugin(IScriptEngine engine) |
||
328 | { |
||
329 | lock (staticLock) |
||
330 | { |
||
331 | if (m_Dataserver.ContainsKey(engine)) |
||
332 | return m_Dataserver[engine]; |
||
333 | else |
||
334 | return null; |
||
335 | } |
||
336 | } |
||
337 | |||
338 | /// <summary> |
||
339 | /// Get the timer plugin for this script engine. |
||
340 | /// </summary> |
||
341 | /// <param name="engine"></param> |
||
342 | /// <returns></returns> |
||
343 | public static Timer GetTimerPlugin(IScriptEngine engine) |
||
344 | { |
||
345 | lock (staticLock) |
||
346 | { |
||
347 | if (m_Timer.ContainsKey(engine)) |
||
348 | return m_Timer[engine]; |
||
349 | else |
||
350 | return null; |
||
351 | } |
||
352 | } |
||
353 | |||
354 | /// <summary> |
||
355 | /// Get the listener plugin for this script engine. |
||
356 | /// </summary> |
||
357 | /// <param name="engine"></param> |
||
358 | /// <returns></returns> |
||
359 | public static Listener GetListenerPlugin(IScriptEngine engine) |
||
360 | { |
||
361 | lock (staticLock) |
||
362 | { |
||
363 | if (m_Listener.ContainsKey(engine)) |
||
364 | return m_Listener[engine]; |
||
365 | else |
||
366 | return null; |
||
367 | } |
||
368 | } |
||
369 | |||
370 | public static Object[] GetSerializationData(IScriptEngine engine, UUID itemID) |
||
371 | { |
||
372 | List<Object> data = new List<Object>(); |
||
373 | |||
374 | lock (staticLock) |
||
375 | { |
||
376 | Object[] listeners = m_Listener[engine].GetSerializationData(itemID); |
||
377 | if (listeners.Length > 0) |
||
378 | { |
||
379 | data.Add("listener"); |
||
380 | data.Add(listeners.Length); |
||
381 | data.AddRange(listeners); |
||
382 | } |
||
383 | |||
384 | Object[] timers=m_Timer[engine].GetSerializationData(itemID); |
||
385 | if (timers.Length > 0) |
||
386 | { |
||
387 | data.Add("timer"); |
||
388 | data.Add(timers.Length); |
||
389 | data.AddRange(timers); |
||
390 | } |
||
391 | |||
392 | Object[] sensors = m_SensorRepeat[engine].GetSerializationData(itemID); |
||
393 | if (sensors.Length > 0) |
||
394 | { |
||
395 | data.Add("sensor"); |
||
396 | data.Add(sensors.Length); |
||
397 | data.AddRange(sensors); |
||
398 | } |
||
399 | } |
||
400 | |||
401 | return data.ToArray(); |
||
402 | } |
||
403 | |||
404 | public static void CreateFromData(IScriptEngine engine, uint localID, |
||
405 | UUID itemID, UUID hostID, Object[] data) |
||
406 | { |
||
407 | int idx = 0; |
||
408 | int len; |
||
409 | |||
410 | while (idx < data.Length) |
||
411 | { |
||
412 | string type = data[idx].ToString(); |
||
413 | len = (int)data[idx+1]; |
||
414 | idx+=2; |
||
415 | |||
416 | if (len > 0) |
||
417 | { |
||
418 | Object[] item = new Object[len]; |
||
419 | Array.Copy(data, idx, item, 0, len); |
||
420 | |||
421 | idx+=len; |
||
422 | |||
423 | lock (staticLock) |
||
424 | { |
||
425 | switch (type) |
||
426 | { |
||
427 | case "listener": |
||
428 | m_Listener[engine].CreateFromData(localID, itemID, |
||
429 | hostID, item); |
||
430 | break; |
||
431 | case "timer": |
||
432 | m_Timer[engine].CreateFromData(localID, itemID, |
||
433 | hostID, item); |
||
434 | break; |
||
435 | case "sensor": |
||
436 | m_SensorRepeat[engine].CreateFromData(localID, |
||
437 | itemID, hostID, item); |
||
438 | break; |
||
439 | } |
||
440 | } |
||
441 | } |
||
442 | } |
||
443 | } |
||
444 | } |
||
4 | eva | 445 | } |