opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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 OpenSimulator 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 OpenMetaverse;
32 using OpenSim.Region.ScriptEngine.Shared.Api;
33  
34 namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins
35 {
36 public class Timer
37 {
38 public class TimerInfo
39 {
40 public uint localID;
41 public UUID itemID;
42 //public double interval;
43 public long interval;
44 //public DateTime next;
45 public long next;
46  
47 public TimerInfo Clone()
48 {
49 return (TimerInfo)this.MemberwiseClone();
50 }
51 }
52  
53 public AsyncCommandManager m_CmdManager;
54  
55 public int TimersCount
56 {
57 get
58 {
59 lock (TimerListLock)
60 return Timers.Count;
61 }
62 }
63  
64 public Timer(AsyncCommandManager CmdManager)
65 {
66 m_CmdManager = CmdManager;
67 }
68  
69 //
70 // TIMER
71 //
72 static private string MakeTimerKey(uint localID, UUID itemID)
73 {
74 return localID.ToString() + itemID.ToString();
75 }
76  
77 private Dictionary<string,TimerInfo> Timers = new Dictionary<string,TimerInfo>();
78 private object TimerListLock = new object();
79  
80 public void SetTimerEvent(uint m_localID, UUID m_itemID, double sec)
81 {
82 if (sec == 0) // Disabling timer
83 {
84 UnSetTimerEvents(m_localID, m_itemID);
85 return;
86 }
87  
88 // Add to timer
89 TimerInfo ts = new TimerInfo();
90 ts.localID = m_localID;
91 ts.itemID = m_itemID;
92 ts.interval = Convert.ToInt64(sec * 10000000); // How many 100 nanoseconds (ticks) should we wait
93 // 2193386136332921 ticks
94 // 219338613 seconds
95  
96 //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
97 ts.next = DateTime.Now.Ticks + ts.interval;
98  
99 string key = MakeTimerKey(m_localID, m_itemID);
100 lock (TimerListLock)
101 {
102 // Adds if timer doesn't exist, otherwise replaces with new timer
103 Timers[key] = ts;
104 }
105 }
106  
107 public void UnSetTimerEvents(uint m_localID, UUID m_itemID)
108 {
109 // Remove from timer
110 string key = MakeTimerKey(m_localID, m_itemID);
111 lock (TimerListLock)
112 {
113 if (Timers.ContainsKey(key))
114 {
115 Timers.Remove(key);
116 }
117 }
118 }
119  
120 public void CheckTimerEvents()
121 {
122 // Nothing to do here?
123 if (Timers.Count == 0)
124 return;
125  
126 lock (TimerListLock)
127 {
128 // Go through all timers
129 Dictionary<string, TimerInfo>.ValueCollection tvals = Timers.Values;
130 foreach (TimerInfo ts in tvals)
131 {
132 // Time has passed?
133 if (ts.next < DateTime.Now.Ticks)
134 {
135 //m_log.Debug("Time has passed: Now: " + DateTime.Now.Ticks + ", Passed: " + ts.next);
136 // Add it to queue
137 m_CmdManager.m_ScriptEngine.PostScriptEvent(ts.itemID,
138 new EventParams("timer", new Object[0],
139 new DetectParams[0]));
140 // set next interval
141  
142 //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
143 ts.next = DateTime.Now.Ticks + ts.interval;
144 }
145 }
146 }
147 }
148  
149 public Object[] GetSerializationData(UUID itemID)
150 {
151 List<Object> data = new List<Object>();
152  
153 lock (TimerListLock)
154 {
155 Dictionary<string, TimerInfo>.ValueCollection tvals = Timers.Values;
156 foreach (TimerInfo ts in tvals)
157 {
158 if (ts.itemID == itemID)
159 {
160 data.Add(ts.interval);
161 data.Add(ts.next-DateTime.Now.Ticks);
162 }
163 }
164 }
165 return data.ToArray();
166 }
167  
168 public void CreateFromData(uint localID, UUID itemID, UUID objectID,
169 Object[] data)
170 {
171 int idx = 0;
172  
173 while (idx < data.Length)
174 {
175 TimerInfo ts = new TimerInfo();
176  
177 ts.localID = localID;
178 ts.itemID = itemID;
179 ts.interval = (long)data[idx];
180 ts.next = DateTime.Now.Ticks + (long)data[idx+1];
181 idx += 2;
182  
183 lock (TimerListLock)
184 {
185 Timers.Add(MakeTimerKey(localID, itemID), ts);
186 }
187 }
188 }
189  
190 public List<TimerInfo> GetTimersInfo()
191 {
192 List<TimerInfo> retList = new List<TimerInfo>();
193  
194 lock (TimerListLock)
195 {
196 foreach (TimerInfo i in Timers.Values)
197 retList.Add(i.Clone());
198 }
199  
200 return retList;
201 }
202 }
203 }