diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/Api/Implementation/Plugins/Timer.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/Shared/Api/Implementation/Plugins/Timer.cs | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/Plugins/Timer.cs b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/Plugins/Timer.cs new file mode 100644 index 0000000..36e992b --- /dev/null +++ b/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/Plugins/Timer.cs | |||
@@ -0,0 +1,162 @@ | |||
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 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 | using System; | ||
29 | using System.Collections; | ||
30 | using System.Collections.Generic; | ||
31 | using libsecondlife; | ||
32 | using OpenSim.Region.ScriptEngine.Shared.Api; | ||
33 | |||
34 | namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins | ||
35 | { | ||
36 | public class Timer | ||
37 | { | ||
38 | public AsyncCommandManager m_CmdManager; | ||
39 | |||
40 | public Timer(AsyncCommandManager CmdManager) | ||
41 | { | ||
42 | m_CmdManager = CmdManager; | ||
43 | } | ||
44 | |||
45 | // | ||
46 | // TIMER | ||
47 | // | ||
48 | private class TimerClass | ||
49 | { | ||
50 | public uint localID; | ||
51 | public LLUUID itemID; | ||
52 | //public double interval; | ||
53 | public long interval; | ||
54 | //public DateTime next; | ||
55 | public long next; | ||
56 | } | ||
57 | |||
58 | private List<TimerClass> Timers = new List<TimerClass>(); | ||
59 | private object TimerListLock = new object(); | ||
60 | |||
61 | public void SetTimerEvent(uint m_localID, LLUUID m_itemID, double sec) | ||
62 | { | ||
63 | // Always remove first, in case this is a re-set | ||
64 | UnSetTimerEvents(m_localID, m_itemID); | ||
65 | if (sec == 0) // Disabling timer | ||
66 | return; | ||
67 | |||
68 | // Add to timer | ||
69 | TimerClass ts = new TimerClass(); | ||
70 | ts.localID = m_localID; | ||
71 | ts.itemID = m_itemID; | ||
72 | ts.interval = Convert.ToInt64(sec * 10000000); // How many 100 nanoseconds (ticks) should we wait | ||
73 | // 2193386136332921 ticks | ||
74 | // 219338613 seconds | ||
75 | |||
76 | //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval); | ||
77 | ts.next = DateTime.Now.Ticks + ts.interval; | ||
78 | lock (TimerListLock) | ||
79 | { | ||
80 | Timers.Add(ts); | ||
81 | } | ||
82 | } | ||
83 | |||
84 | public void UnSetTimerEvents(uint m_localID, LLUUID m_itemID) | ||
85 | { | ||
86 | // Remove from timer | ||
87 | lock (TimerListLock) | ||
88 | { | ||
89 | foreach (TimerClass ts in new ArrayList(Timers)) | ||
90 | { | ||
91 | if (ts.localID == m_localID && ts.itemID == m_itemID) | ||
92 | Timers.Remove(ts); | ||
93 | } | ||
94 | } | ||
95 | } | ||
96 | |||
97 | public void CheckTimerEvents() | ||
98 | { | ||
99 | // Nothing to do here? | ||
100 | if (Timers.Count == 0) | ||
101 | return; | ||
102 | |||
103 | lock (TimerListLock) | ||
104 | { | ||
105 | // Go through all timers | ||
106 | foreach (TimerClass ts in Timers) | ||
107 | { | ||
108 | // Time has passed? | ||
109 | if (ts.next < DateTime.Now.Ticks) | ||
110 | { | ||
111 | // Console.WriteLine("Time has passed: Now: " + DateTime.Now.Ticks + ", Passed: " + ts.next); | ||
112 | // Add it to queue | ||
113 | m_CmdManager.m_ScriptEngine.PostScriptEvent(ts.itemID, | ||
114 | new EventParams("timer", new Object[0], | ||
115 | new DetectParams[0])); | ||
116 | // set next interval | ||
117 | |||
118 | //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval); | ||
119 | ts.next = DateTime.Now.Ticks + ts.interval; | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | } | ||
124 | |||
125 | public Object[] GetSerializationData(LLUUID itemID) | ||
126 | { | ||
127 | List<Object> data = new List<Object>(); | ||
128 | |||
129 | lock (TimerListLock) | ||
130 | { | ||
131 | foreach (TimerClass ts in Timers) | ||
132 | { | ||
133 | if (ts.itemID == itemID) | ||
134 | { | ||
135 | data.Add(ts.interval); | ||
136 | data.Add(ts.next-DateTime.Now.Ticks); | ||
137 | } | ||
138 | } | ||
139 | } | ||
140 | return data.ToArray(); | ||
141 | } | ||
142 | |||
143 | public void CreateFromData(uint localID, LLUUID itemID, LLUUID objectID, | ||
144 | Object[] data) | ||
145 | { | ||
146 | int idx = 0; | ||
147 | |||
148 | while (idx < data.Length) | ||
149 | { | ||
150 | TimerClass ts = new TimerClass(); | ||
151 | |||
152 | ts.localID = localID; | ||
153 | ts.itemID = itemID; | ||
154 | ts.interval = (long)data[idx]; | ||
155 | ts.next = DateTime.Now.Ticks + (long)data[idx+1]; | ||
156 | idx += 2; | ||
157 | |||
158 | Timers.Add(ts); | ||
159 | } | ||
160 | } | ||
161 | } | ||
162 | } | ||