aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/DotNetEngine/EventQueueManager.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/ScriptEngine/DotNetEngine/EventQueueManager.cs136
1 files changed, 136 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/EventQueueManager.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/EventQueueManager.cs
new file mode 100644
index 0000000..59f669b
--- /dev/null
+++ b/OpenSim/Region/ScriptEngine/DotNetEngine/EventQueueManager.cs
@@ -0,0 +1,136 @@
1/*
2* Copyright (c) Contributors, http://www.openmetaverse.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/* Original code: Tedd Hansen */
29using System;
30using System.Collections.Generic;
31using System.Text;
32using System.Threading;
33using System.Reflection;
34
35namespace OpenSim.Region.ScriptEngine.DotNetEngine
36{
37 class EventQueueManager
38 {
39 private Thread EventQueueThread;
40 private int NothingToDoSleepms = 200;
41 private Queue<QueueItemStruct> EventQueue = new Queue<QueueItemStruct>();
42 private struct QueueItemStruct
43 {
44 public string ObjectID;
45 public string ScriptID;
46 public string FunctionName;
47 public object[] param;
48 }
49
50 private ScriptEngine myScriptEngine;
51 public EventQueueManager(ScriptEngine _ScriptEngine)
52 {
53 myScriptEngine = _ScriptEngine;
54 Common.SendToDebug("EventQueueManager Start");
55 // Start worker thread
56 EventQueueThread = new Thread(EventQueueThreadLoop);
57 EventQueueThread.IsBackground = true;
58 EventQueueThread.Name = "EventQueueManagerThread";
59 EventQueueThread.Start();
60 }
61 ~EventQueueManager()
62 {
63 // Kill worker thread
64 if (EventQueueThread != null && EventQueueThread.IsAlive == true)
65 {
66 try
67 {
68 EventQueueThread.Abort();
69 EventQueueThread.Join();
70 }
71 catch (Exception e)
72 {
73 Common.SendToDebug("EventQueueManager Exception killing worker thread: " + e.ToString());
74 }
75 }
76 // Todo: Clean up our queues
77
78 }
79
80 private void EventQueueThreadLoop()
81 {
82 Common.SendToDebug("EventQueueManager Worker thread spawned");
83 try
84 {
85 while (true)
86 {
87 if (EventQueue.Count == 0)
88 {
89 // Nothing to do? Sleep a bit waiting for something to do
90 Thread.Sleep(NothingToDoSleepms);
91 }
92 else
93 {
94 // Something in queue, process
95 QueueItemStruct QIS = EventQueue.Dequeue();
96 Common.SendToDebug("Processing event for ObjectID: " + QIS.ObjectID + ", ScriptID: " + QIS.ScriptID + ", FunctionName: " + QIS.FunctionName);
97 // TODO: Execute function
98 myScriptEngine.myScriptManager.ExecuteFunction(QIS.ObjectID, QIS.ScriptID, QIS.FunctionName, QIS.param);
99 }
100 }
101 }
102 catch (ThreadAbortException tae)
103 {
104 Common.SendToDebug("EventQueueManager Worker thread killed: " + tae.Message);
105 }
106 }
107
108 public void AddToObjectQueue(string ObjectID, string FunctionName, object[] param)
109 {
110 // Determine all scripts in Object and add to their queue
111 Common.SendToDebug("EventQueueManager Adding ObjectID: " + ObjectID + ", FunctionName: " + FunctionName);
112
113 foreach (string ScriptID in myScriptEngine.myScriptManager.GetScriptKeys(ObjectID))
114 {
115 // Add to each script in that object
116 // TODO: Some scripts may not subscribe to this event. Should we NOT add it? Does it matter?
117
118 // Create a structure and add data
119 QueueItemStruct QIS = new QueueItemStruct();
120 QIS.ObjectID = ObjectID;
121 QIS.ScriptID = ScriptID;
122 QIS.FunctionName = FunctionName;
123 QIS.param = param;
124
125 // Add it to queue
126 EventQueue.Enqueue(QIS);
127
128 }
129 }
130 //public void AddToScriptQueue(string ObjectID, string FunctionName, object[] param)
131 //{
132 // // Add to script queue
133 //}
134
135 }
136}