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