aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/ScriptEngine/Components/DotNetEngine/Scheduler/ScriptManager.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/ScriptEngine/Components/DotNetEngine/Scheduler/ScriptManager.cs176
1 files changed, 176 insertions, 0 deletions
diff --git a/OpenSim/ScriptEngine/Components/DotNetEngine/Scheduler/ScriptManager.cs b/OpenSim/ScriptEngine/Components/DotNetEngine/Scheduler/ScriptManager.cs
new file mode 100644
index 0000000..3452b03
--- /dev/null
+++ b/OpenSim/ScriptEngine/Components/DotNetEngine/Scheduler/ScriptManager.cs
@@ -0,0 +1,176 @@
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Reflection;
5using System.Text;
6using System.Threading;
7using log4net;
8using OpenMetaverse;
9using OpenSim.Region.ScriptEngine.Shared;
10using OpenSim.ScriptEngine.Shared;
11using EventParams=OpenSim.ScriptEngine.Shared.EventParams;
12
13namespace OpenSim.ScriptEngine.Components.DotNetEngine.Scheduler
14{
15 public partial class ScriptManager: IScriptExecutor
16 {
17 private const int NoWorkSleepMs = 50;
18 private const int NoWorkSleepMsInc = 1; // How much time to increase wait with on every iteration
19 private const int NoWorkSleepMsIncMax = 300; // Max time to wait
20
21 internal static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
22 public string Name { get { return "SECS.DotNetEngine.ScriptManager"; } }
23 private static Thread ScriptLoadUnloadThread;
24 public Dictionary<uint, Dictionary<UUID, ScriptStructure>> Scripts = new Dictionary<uint, Dictionary<UUID, ScriptStructure>>();
25
26 private RegionInfoStructure CurrentRegion;
27 public void Initialize(RegionInfoStructure currentRegion)
28 {
29 CurrentRegion = currentRegion;
30 }
31
32 public ScriptManager()
33 {
34 ScriptLoadUnloadThread = new Thread(LoadUnloadLoop);
35 ScriptLoadUnloadThread.Name = "ScriptLoadUnloadThread";
36 ScriptLoadUnloadThread.IsBackground = true;
37 ScriptLoadUnloadThread.Start();
38 }
39 public void Close() { }
40
41 private void LoadUnloadLoop ()
42 {
43 int _NoWorkSleepMsInc = 0;
44 while (true)
45 {
46 if (DoScriptLoadUnload())
47 {
48 // We found work, reset counter
49 _NoWorkSleepMsInc = NoWorkSleepMs;
50 } else
51 {
52 // We didn't find work
53 // Sleep
54 Thread.Sleep(NoWorkSleepMs + NoWorkSleepMsInc);
55 // Increase sleep delay
56 _NoWorkSleepMsInc += NoWorkSleepMsInc;
57 // Make sure we don't exceed max
58 if (_NoWorkSleepMsInc > NoWorkSleepMsIncMax)
59 _NoWorkSleepMsInc = NoWorkSleepMsIncMax;
60 }
61 }
62 }
63
64 #region Add/Remove/Find script functions for our Script memory structure
65 private void MemAddScript(ScriptStructure script)
66 {
67 lock (scriptLock)
68 {
69 // Create object if it doesn't exist
70 if (!Scripts.ContainsKey(script.LocalID))
71 Scripts.Add(script.LocalID, new Dictionary<UUID, ScriptStructure>());
72
73 // Delete script if it exists
74 Dictionary<UUID, ScriptStructure> Obj;
75 if (Scripts.TryGetValue(script.LocalID, out Obj))
76 if (Obj.ContainsKey(script.ItemID) == true)
77 Obj.Remove(script.ItemID);
78
79 // Add to object
80 Obj.Add(script.ItemID, script);
81 }
82 }
83 private void MemRemoveScript(uint LocalID, UUID ItemID)
84 {
85 // TODO: Also clean up command queue and async commands for object
86 lock (scriptLock)
87 {
88 // Create object if it doesn't exist
89 if (!Scripts.ContainsKey(LocalID))
90 return;
91
92 // Delete script if it exists
93 Dictionary<UUID, ScriptStructure> Obj;
94 if (Scripts.TryGetValue(LocalID, out Obj))
95 if (Obj.ContainsKey(ItemID) == true)
96 Obj.Remove(ItemID);
97
98 // Empty?
99 if (Obj.Count == 0)
100 Scripts.Remove(LocalID);
101
102 }
103 }
104 public bool TryGetScript(uint localID, UUID itemID, ref ScriptStructure script)
105 {
106 lock (scriptLock)
107 {
108
109 if (Scripts.ContainsKey(localID) == false)
110 return false;
111
112 Dictionary<UUID, ScriptStructure> Obj;
113 if (Scripts.TryGetValue(localID, out Obj))
114 if (Obj.ContainsKey(itemID) == false)
115 return false;
116
117 // Get script
118 return Obj.TryGetValue(itemID, out script);
119 }
120 }
121 public ScriptStructure GetScript(uint localID, UUID itemID)
122 {
123 lock (scriptLock)
124 {
125
126 if (Scripts.ContainsKey(localID) == false)
127 throw new Exception("No script with LocalID " + localID + " was found.");
128
129 Dictionary<UUID, ScriptStructure> Obj;
130 if (Scripts.TryGetValue(localID, out Obj))
131 if (Obj.ContainsKey(itemID) == false)
132 throw new Exception("No script with ItemID " + itemID + " was found.");
133
134 // Get script
135 return Obj[itemID];
136 }
137 }
138 public bool TryGetScripts(uint localID, ref Dictionary<UUID, ScriptStructure> returnList)
139 {
140 Dictionary<UUID, ScriptStructure> getList = GetScripts(localID);
141 if (getList != null)
142 {
143 returnList = getList;
144 return true;
145 }
146 return false;
147 }
148 public Dictionary<UUID, ScriptStructure> GetScripts(uint localID)
149 {
150 lock (scriptLock)
151 {
152
153 if (Scripts.ContainsKey(localID) == false)
154 return null;
155 return Scripts[localID];
156 }
157 }
158 #endregion
159
160 public void ExecuteCommand(EventParams p)
161 {
162 ScriptStructure ss = new ScriptStructure();
163 if (TryGetScript(p.LocalID, p.ItemID, ref ss))
164 ExecuteCommand(ref ss, p);
165 }
166
167 public void ExecuteCommand(ref ScriptStructure scriptContainer, EventParams p)
168 {
169 m_log.DebugFormat("[{0}] ######################################################", Name);
170 m_log.DebugFormat("[{0}] Command execution ItemID {1}: \"{2}\".", Name, scriptContainer.ItemID, p.EventName);
171 scriptContainer.ExecuteEvent(p);
172 m_log.DebugFormat("[{0}] ######################################################", Name);
173 }
174
175 }
176}