aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/AsyncCommandManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/AsyncCommandManager.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/AsyncCommandManager.cs521
1 files changed, 24 insertions, 497 deletions
diff --git a/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/AsyncCommandManager.cs b/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/AsyncCommandManager.cs
index e9539e6..0555d61 100644
--- a/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/AsyncCommandManager.cs
+++ b/OpenSim/Region/ScriptEngine/Common/ScriptEngineBase/AsyncCommandManager.cs
@@ -36,6 +36,8 @@ using OpenSim.Region.Environment.Interfaces;
36using OpenSim.Region.Environment.Modules; 36using OpenSim.Region.Environment.Modules;
37using OpenSim.Region.Environment.Scenes; 37using OpenSim.Region.Environment.Scenes;
38using OpenSim.Framework; 38using OpenSim.Framework;
39using OpenSim.Region.ScriptEngine.Common.ScriptEngineBase.AsyncCommandPlugins;
40using Timer=OpenSim.Region.ScriptEngine.Common.ScriptEngineBase.AsyncCommandPlugins.Timer;
39 41
40namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase 42namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase
41{ 43{
@@ -47,17 +49,26 @@ namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase
47 private static Thread cmdHandlerThread; 49 private static Thread cmdHandlerThread;
48 private static int cmdHandlerThreadCycleSleepms; 50 private static int cmdHandlerThreadCycleSleepms;
49 51
50 private ScriptEngine m_ScriptEngine; 52 public ScriptEngine m_ScriptEngine;
51 53
52 public Dictionary<uint, Dictionary<LLUUID, LSL_Types.list>> SenseEvents = 54 public AsyncCommandPlugins.Timer m_Timer;
53 new Dictionary<uint, Dictionary<LLUUID, LSL_Types.list>>(); 55 public AsyncCommandPlugins.HttpRequest m_HttpRequest;
54 private Object SenseLock = new Object(); 56 public AsyncCommandPlugins.Listener m_Listener;
57 public AsyncCommandPlugins.SensorRepeat m_SensorRepeat;
58 public AsyncCommandPlugins.XmlRequest m_XmlRequest;
55 59
56 public AsyncCommandManager(ScriptEngine _ScriptEngine) 60 public AsyncCommandManager(ScriptEngine _ScriptEngine)
57 { 61 {
58 m_ScriptEngine = _ScriptEngine; 62 m_ScriptEngine = _ScriptEngine;
59 ReadConfig(); 63 ReadConfig();
60 64
65 // Create instances of all plugins
66 m_Timer = new Timer(this);
67 m_HttpRequest = new HttpRequest(this);
68 m_Listener = new Listener(this);
69 m_SensorRepeat = new SensorRepeat(this);
70 m_XmlRequest = new XmlRequest(this);
71
61 StartThread(); 72 StartThread();
62 } 73 }
63 74
@@ -128,15 +139,15 @@ namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase
128 internal void DoOneCmdHandlerPass() 139 internal void DoOneCmdHandlerPass()
129 { 140 {
130 // Check timers 141 // Check timers
131 CheckTimerEvents(); 142 m_Timer.CheckTimerEvents();
132 // Check HttpRequests 143 // Check HttpRequests
133 CheckHttpRequests(); 144 m_HttpRequest.CheckHttpRequests();
134 // Check XMLRPCRequests 145 // Check XMLRPCRequests
135 CheckXMLRPCRequests(); 146 m_XmlRequest.CheckXMLRPCRequests();
136 // Check Listeners 147 // Check Listeners
137 CheckListeners(); 148 m_Listener.CheckListeners();
138 // Check Sensors 149 // Check Sensors
139 CheckSenseRepeaterEvents(); 150 m_SensorRepeat.CheckSenseRepeaterEvents();
140 } 151 }
141 152
142 /// <summary> 153 /// <summary>
@@ -149,7 +160,8 @@ namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase
149 // Remove a specific script 160 // Remove a specific script
150 161
151 // Remove from: Timers 162 // Remove from: Timers
152 UnSetTimerEvents(localID, itemID); 163 m_Timer.UnSetTimerEvents(localID, itemID);
164
153 // Remove from: HttpRequest 165 // Remove from: HttpRequest
154 IHttpRequests iHttpReq = 166 IHttpRequests iHttpReq =
155 m_ScriptEngine.World.RequestModuleInterface<IHttpRequests>(); 167 m_ScriptEngine.World.RequestModuleInterface<IHttpRequests>();
@@ -160,506 +172,21 @@ namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase
160 172
161 IXMLRPC xmlrpc = m_ScriptEngine.World.RequestModuleInterface<IXMLRPC>(); 173 IXMLRPC xmlrpc = m_ScriptEngine.World.RequestModuleInterface<IXMLRPC>();
162 xmlrpc.DeleteChannels(itemID); 174 xmlrpc.DeleteChannels(itemID);
163
164 xmlrpc.CancelSRDRequests(itemID); 175 xmlrpc.CancelSRDRequests(itemID);
165 176
166 // Remove Sensors 177 // Remove Sensors
167 UnSetSenseRepeaterEvents(localID, itemID); 178 m_SensorRepeat.UnSetSenseRepeaterEvents(localID, itemID);
168
169 }
170
171 #region TIMER
172
173 //
174 // TIMER
175 //
176 private class TimerClass
177 {
178 public uint localID;
179 public LLUUID itemID;
180 //public double interval;
181 public long interval;
182 //public DateTime next;
183 public long next;
184 }
185
186 private List<TimerClass> Timers = new List<TimerClass>();
187 private object TimerListLock = new object();
188
189 public void SetTimerEvent(uint m_localID, LLUUID m_itemID, double sec)
190 {
191 Console.WriteLine("SetTimerEvent");
192
193 // Always remove first, in case this is a re-set
194 UnSetTimerEvents(m_localID, m_itemID);
195 if (sec == 0) // Disabling timer
196 return;
197
198 // Add to timer
199 TimerClass ts = new TimerClass();
200 ts.localID = m_localID;
201 ts.itemID = m_itemID;
202 ts.interval = Convert.ToInt64(sec * 10000000); // How many 100 nanoseconds (ticks) should we wait
203 // 2193386136332921 ticks
204 // 219338613 seconds
205
206 //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
207 ts.next = DateTime.Now.Ticks + ts.interval;
208 lock (TimerListLock)
209 {
210 Timers.Add(ts);
211 }
212 }
213
214 public void UnSetTimerEvents(uint m_localID, LLUUID m_itemID)
215 {
216 // Remove from timer
217 lock (TimerListLock)
218 {
219 foreach (TimerClass ts in new ArrayList(Timers))
220 {
221 if (ts.localID == m_localID && ts.itemID == m_itemID)
222 Timers.Remove(ts);
223 }
224 }
225
226 // Old method: Create new list
227 //List<TimerClass> NewTimers = new List<TimerClass>();
228 //foreach (TimerClass ts in Timers)
229 //{
230 // if (ts.localID != m_localID && ts.itemID != m_itemID)
231 // {
232 // NewTimers.Add(ts);
233 // }
234 //}
235 //Timers.Clear();
236 //Timers = NewTimers;
237 //}
238 }
239
240 public void CheckTimerEvents()
241 {
242 // Nothing to do here?
243 if (Timers.Count == 0)
244 return;
245
246 lock (TimerListLock)
247 {
248 // Go through all timers
249 foreach (TimerClass ts in Timers)
250 {
251 // Time has passed?
252 if (ts.next < DateTime.Now.Ticks)
253 {
254// Console.WriteLine("Time has passed: Now: " + DateTime.Now.Ticks + ", Passed: " + ts.next);
255 // Add it to queue
256 m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(ts.localID, ts.itemID, "timer", EventQueueManager.llDetectNull,
257 null);
258 // set next interval
259
260 //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
261 ts.next = DateTime.Now.Ticks + ts.interval;
262 }
263 }
264 }
265 }
266
267 #endregion
268 #region SENSOR
269
270 //
271 // SenseRepeater and Sensors
272 //
273 private class SenseRepeatClass
274 {
275 public uint localID;
276 public LLUUID itemID;
277 public double interval;
278 public DateTime next;
279
280 public string name;
281 public LLUUID keyID;
282 public int type;
283 public double range;
284 public double arc;
285 public SceneObjectPart host;
286 }
287
288 private List<SenseRepeatClass> SenseRepeaters = new List<SenseRepeatClass>();
289 private object SenseRepeatListLock = new object();
290
291 public void SetSenseRepeatEvent(uint m_localID, LLUUID m_itemID,
292 string name, LLUUID keyID, int type, double range, double arc, double sec,SceneObjectPart host)
293 {
294 Console.WriteLine("SetSensorEvent");
295
296 // Always remove first, in case this is a re-set
297 UnSetSenseRepeaterEvents(m_localID, m_itemID);
298 if (sec == 0) // Disabling timer
299 return;
300
301 // Add to timer
302 SenseRepeatClass ts = new SenseRepeatClass();
303 ts.localID = m_localID;
304 ts.itemID = m_itemID;
305 ts.interval = sec;
306 ts.name = name;
307 ts.keyID = keyID;
308 ts.type = type;
309 ts.range = range;
310 ts.arc = arc;
311 ts.host = host;
312
313 ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
314 lock (SenseRepeatListLock)
315 {
316 SenseRepeaters.Add(ts);
317 }
318 }
319
320 public void UnSetSenseRepeaterEvents(uint m_localID, LLUUID m_itemID)
321 {
322 // Remove from timer
323 lock (SenseRepeatListLock)
324 {
325 List<SenseRepeatClass> NewSensors = new List<SenseRepeatClass>();
326 foreach (SenseRepeatClass ts in SenseRepeaters)
327 {
328 if (ts.localID != m_localID && ts.itemID != m_itemID)
329 {
330 NewSensors.Add(ts);
331 }
332 }
333 SenseRepeaters.Clear();
334 SenseRepeaters = NewSensors;
335 }
336 }
337
338 public void CheckSenseRepeaterEvents()
339 {
340 // Nothing to do here?
341 if (SenseRepeaters.Count == 0)
342 return;
343
344 lock (SenseRepeatListLock)
345 {
346 // Go through all timers
347 foreach (SenseRepeatClass ts in SenseRepeaters)
348 {
349 // Time has passed?
350 if (ts.next.ToUniversalTime() < DateTime.Now.ToUniversalTime())
351 {
352 SensorSweep(ts);
353 // set next interval
354 ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
355 }
356 }
357 } // lock
358 }
359
360 public void SenseOnce(uint m_localID, LLUUID m_itemID,
361 string name, LLUUID keyID, int type, double range, double arc, SceneObjectPart host)
362 {
363 // Add to timer
364 SenseRepeatClass ts = new SenseRepeatClass();
365 ts.localID = m_localID;
366 ts.itemID = m_itemID;
367 ts.interval = 0;
368 ts.name = name;
369 ts.keyID = keyID;
370 ts.type = type;
371 ts.range = range;
372 ts.arc = arc;
373 ts.host = host;
374 SensorSweep(ts);
375 }
376
377 public LSL_Types.list GetSensorList(uint m_localID, LLUUID m_itemID)
378 {
379 lock (SenseLock)
380 {
381 Dictionary<LLUUID, LSL_Types.list> Obj = null;
382 if (!SenseEvents.TryGetValue(m_localID, out Obj))
383 {
384 m_ScriptEngine.Log.Info("[AsyncLSL]: GetSensorList missing localID: " + m_localID);
385 return null;
386 }
387 lock (Obj)
388 {
389 // Get script
390 LSL_Types.list SenseList = null;
391 if (!Obj.TryGetValue(m_itemID, out SenseList))
392 {
393 m_ScriptEngine.Log.Info("[AsyncLSL]: GetSensorList missing itemID: " + m_itemID);
394 return null;
395 }
396 return SenseList;
397 }
398 }
399
400 }
401 179
402 private void SensorSweep(SenseRepeatClass ts)
403 {
404 //m_ScriptEngine.Log.Info("[AsyncLSL]:Enter SensorSweep");
405 SceneObjectPart SensePoint =ts.host;
406
407 if (SensePoint == null)
408 {
409 //m_ScriptEngine.Log.Info("[AsyncLSL]: Enter SensorSweep (SensePoint == null) for "+ts.itemID.ToString());
410 return;
411 }
412 //m_ScriptEngine.Log.Info("[AsyncLSL]: Enter SensorSweep Scan");
413
414 LLVector3 sensorPos = SensePoint.AbsolutePosition;
415 LLVector3 regionPos = new LLVector3(m_ScriptEngine.World.RegionInfo.RegionLocX * Constants.RegionSize, m_ScriptEngine.World.RegionInfo.RegionLocY * Constants.RegionSize, 0);
416 LLVector3 fromRegionPos = sensorPos + regionPos;
417
418 LLQuaternion q = SensePoint.RotationOffset;
419 LSL_Types.Quaternion r = new LSL_Types.Quaternion(q.X, q.Y, q.Z, q.W);
420 LSL_Types.Vector3 forward_dir = (new LSL_Types.Vector3(1, 0, 0) * r);
421 double mag_fwd = LSL_Types.Vector3.Mag(forward_dir);
422
423 // Here we should do some smart culling ...
424 // math seems quicker than strings so try that first
425 LSL_Types.list SensedObjects = new LSL_Types.list();
426 LSL_Types.Vector3 ZeroVector = new LSL_Types.Vector3(0, 0, 0);
427
428 foreach (EntityBase ent in m_ScriptEngine.World.Entities.Values)
429 {
430
431 LLVector3 toRegionPos = ent.AbsolutePosition + regionPos;
432 double dis = Math.Abs((double) Util.GetDistanceTo(toRegionPos, fromRegionPos));
433 if (dis <= ts.range)
434 {
435 // In Range, is it the right Type ?
436 int objtype = 0;
437
438 if (m_ScriptEngine.World.GetScenePresence(ent.UUID) != null) objtype |= 0x01; // actor
439 if (ent.Velocity.Equals(ZeroVector))
440 objtype |= 0x04; // passive non-moving
441 else
442 objtype |= 0x02; // active moving
443 if (ent is IScript) objtype |= 0x08; // Scripted. It COULD have one hidden ...
444
445 if ( ((ts.type & objtype) != 0 ) ||((ts.type & objtype) == ts.type ))
446 {
447 // docs claim AGENT|ACTIVE should find agent objects OR active objects
448 // so the bitwise AND with object type should be non-zero
449
450 // Right type too, what about the other params , key and name ?
451 bool keep = true;
452 if (ts.arc != Math.PI)
453 {
454 // not omni-directional. Can you see it ?
455 // vec forward_dir = llRot2Fwd(llGetRot())
456 // vec obj_dir = toRegionPos-fromRegionPos
457 // dot=dot(forward_dir,obj_dir)
458 // mag_fwd = mag(forward_dir)
459 // mag_obj = mag(obj_dir)
460 // ang = acos( dot /(mag_fwd*mag_obj))
461 double ang_obj = 0;
462 try
463 {
464 LLVector3 diff =toRegionPos - fromRegionPos;
465 LSL_Types.Vector3 obj_dir = new LSL_Types.Vector3(diff.X, diff.Y, diff.Z);
466 double dot = LSL_Types.Vector3.Dot(forward_dir, obj_dir);
467 double mag_obj = LSL_Types.Vector3.Mag(obj_dir);
468 ang_obj = Math.Acos(dot / (mag_fwd * mag_obj));
469 }
470 catch
471 {
472 }
473
474 if (ang_obj > ts.arc) keep = false;
475 }
476
477 if (keep && (ts.name.Length > 0) && (ts.name != ent.Name))
478 {
479 keep = false;
480 }
481
482 if (keep && (ts.keyID != null) && (ts.keyID != LLUUID.Zero) && (ts.keyID != ent.UUID))
483 {
484 keep = false;
485 }
486 if (keep==true) SensedObjects.Add(ent.UUID);
487 }
488 }
489 }
490 //m_ScriptEngine.Log.Info("[AsyncLSL]: Enter SensorSweep SenseLock");
491
492 lock (SenseLock)
493 {
494 // Create object if it doesn't exist
495 if (SenseEvents.ContainsKey(ts.localID) == false)
496 {
497 SenseEvents.Add(ts.localID, new Dictionary<LLUUID, LSL_Types.list>());
498 }
499 // clear if previous traces exist
500 Dictionary<LLUUID, LSL_Types.list> Obj;
501 SenseEvents.TryGetValue(ts.localID, out Obj);
502 if (Obj.ContainsKey(ts.itemID) == true)
503 Obj.Remove(ts.itemID);
504
505 // note list may be zero length
506 Obj.Add(ts.itemID, SensedObjects);
507
508 if (SensedObjects.Length == 0)
509 {
510 // send a "no_sensor"
511 // Add it to queue
512 m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(ts.localID, ts.itemID, "no_sensor", EventQueueManager.llDetectNull,
513 new object[] {});
514 }
515 else
516 {
517
518 m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(ts.localID, ts.itemID, "sensor", EventQueueManager.llDetectNull,
519 new object[] { SensedObjects.Length });
520 }
521 }
522 } 180 }
523 #endregion
524
525 #region HTTP REQUEST
526
527 public void CheckHttpRequests()
528 {
529 if (m_ScriptEngine.World == null)
530 return;
531 181
532 IHttpRequests iHttpReq =
533 m_ScriptEngine.World.RequestModuleInterface<IHttpRequests>();
534
535 HttpRequestClass httpInfo = null;
536
537 if (iHttpReq != null)
538 httpInfo = iHttpReq.GetNextCompletedRequest();
539
540 while (httpInfo != null)
541 {
542 //m_ScriptEngine.Log.Info("[AsyncLSL]:" + httpInfo.response_body + httpInfo.status);
543
544 // Deliver data to prim's remote_data handler
545 //
546 // TODO: Returning null for metadata, since the lsl function
547 // only returns the byte for HTTP_BODY_TRUNCATED, which is not
548 // implemented here yet anyway. Should be fixed if/when maxsize
549 // is supported
550
551 if (m_ScriptEngine.m_ScriptManager.GetScript(httpInfo.localID, httpInfo.itemID) != null)
552 {
553 iHttpReq.RemoveCompletedRequest(httpInfo.reqID);
554 object[] resobj = new object[]
555 {
556 httpInfo.reqID.ToString(), httpInfo.status, null, httpInfo.response_body
557 };
558
559 m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(
560 httpInfo.localID, httpInfo.itemID, "http_response", EventQueueManager.llDetectNull, resobj
561 );
562 //Thread.Sleep(2500);
563 }
564
565 httpInfo = iHttpReq.GetNextCompletedRequest();
566 }
567 }
568
569 #endregion
570 182
571 #region Check llRemoteData channels 183 #region Check llRemoteData channels
572 184
573 public void CheckXMLRPCRequests() 185
574 {
575 if (m_ScriptEngine.World == null)
576 return;
577
578 IXMLRPC xmlrpc = m_ScriptEngine.World.RequestModuleInterface<IXMLRPC>();
579
580 if (xmlrpc != null)
581 {
582 RPCRequestInfo rInfo = xmlrpc.GetNextCompletedRequest();
583
584 while (rInfo != null)
585 {
586 if (m_ScriptEngine.m_ScriptManager.GetScript(rInfo.GetLocalID(), rInfo.GetItemID()) != null)
587 {
588 xmlrpc.RemoveCompletedRequest(rInfo.GetMessageID());
589
590 //Deliver data to prim's remote_data handler
591 object[] resobj = new object[]
592 {
593 2, rInfo.GetChannelKey().ToString(), rInfo.GetMessageID().ToString(), String.Empty,
594 rInfo.GetIntValue(),
595 rInfo.GetStrVal()
596 };
597 m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(
598 rInfo.GetLocalID(), rInfo.GetItemID(), "remote_data", EventQueueManager.llDetectNull, resobj
599 );
600 }
601
602 rInfo = xmlrpc.GetNextCompletedRequest();
603 }
604
605 SendRemoteDataRequest srdInfo = xmlrpc.GetNextCompletedSRDRequest();
606
607 while (srdInfo != null)
608 {
609 if (m_ScriptEngine.m_ScriptManager.GetScript(srdInfo.m_localID, srdInfo.m_itemID) != null)
610 {
611 xmlrpc.RemoveCompletedSRDRequest(srdInfo.GetReqID());
612
613 //Deliver data to prim's remote_data handler
614 object[] resobj = new object[]
615 {
616 3, srdInfo.channel.ToString(), srdInfo.GetReqID().ToString(), String.Empty,
617 srdInfo.idata,
618 srdInfo.sdata
619 };
620 m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(
621 srdInfo.m_localID, srdInfo.m_itemID, "remote_data", EventQueueManager.llDetectNull, resobj
622 );
623 }
624
625 srdInfo = xmlrpc.GetNextCompletedSRDRequest();
626 }
627 }
628 }
629
630 #endregion 186 #endregion
631 187
632 #region Check llListeners 188 #region Check llListeners
633 189
634 public void CheckListeners()
635 {
636 if (m_ScriptEngine.World == null)
637 return;
638 IWorldComm comms = m_ScriptEngine.World.RequestModuleInterface<IWorldComm>();
639
640 if (comms != null)
641 {
642 while (comms.HasMessages())
643 {
644 if (m_ScriptEngine.m_ScriptManager.GetScript(
645 comms.PeekNextMessageLocalID(), comms.PeekNextMessageItemID()) != null)
646 {
647 ListenerInfo lInfo = comms.GetNextMessage();
648
649 //Deliver data to prim's listen handler
650 object[] resobj = new object[]
651 {
652 //lInfo.GetChannel(), lInfo.GetName(), lInfo.GetID().ToString(), lInfo.GetMessage()
653 lInfo.GetChannel(), lInfo.GetName(), lInfo.GetSourceItemID().ToString(), lInfo.GetMessage()
654 };
655
656 m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(
657 lInfo.GetLocalID(), lInfo.GetItemID(), "listen", EventQueueManager.llDetectNull, resobj
658 );
659 }
660 }
661 }
662 }
663 190
664 #endregion 191 #endregion
665 192