OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
Timer.cs
Go to the documentation of this file.
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 
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  Dictionary<string, TimerInfo> tvals;
127  lock (TimerListLock)
128  {
129  // Go through all timers
130  tvals = new Dictionary<string, TimerInfo>(Timers);
131  }
132 
133  foreach (TimerInfo ts in tvals.Values)
134  {
135  // Time has passed?
136  if (ts.next < DateTime.Now.Ticks)
137  {
138  //m_log.Debug("Time has passed: Now: " + DateTime.Now.Ticks + ", Passed: " + ts.next);
139  // Add it to queue
140  m_CmdManager.m_ScriptEngine.PostScriptEvent(ts.itemID,
141  new EventParams("timer", new Object[0],
142  new DetectParams[0]));
143  // set next interval
144 
145  //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
146  ts.next = DateTime.Now.Ticks + ts.interval;
147  }
148  }
149  }
150 
151  public Object[] GetSerializationData(UUID itemID)
152  {
153  List<Object> data = new List<Object>();
154 
155  lock (TimerListLock)
156  {
157  Dictionary<string, TimerInfo>.ValueCollection tvals = Timers.Values;
158  foreach (TimerInfo ts in tvals)
159  {
160  if (ts.itemID == itemID)
161  {
162  data.Add(ts.interval);
163  data.Add(ts.next-DateTime.Now.Ticks);
164  }
165  }
166  }
167  return data.ToArray();
168  }
169 
170  public void CreateFromData(uint localID, UUID itemID, UUID objectID,
171  Object[] data)
172  {
173  int idx = 0;
174 
175  while (idx < data.Length)
176  {
177  TimerInfo ts = new TimerInfo();
178 
179  ts.localID = localID;
180  ts.itemID = itemID;
181  ts.interval = (long)data[idx];
182  ts.next = DateTime.Now.Ticks + (long)data[idx+1];
183  idx += 2;
184 
185  lock (TimerListLock)
186  {
187  Timers.Add(MakeTimerKey(localID, itemID), ts);
188  }
189  }
190  }
191 
192  public List<TimerInfo> GetTimersInfo()
193  {
194  List<TimerInfo> retList = new List<TimerInfo>();
195 
196  lock (TimerListLock)
197  {
198  foreach (TimerInfo i in Timers.Values)
199  retList.Add(i.Clone());
200  }
201 
202  return retList;
203  }
204  }
205 }
Timer(AsyncCommandManager CmdManager)
Definition: Timer.cs:64
Handles LSL commands that takes long time and returns an event, for example timers, HTTP requests, etc.
void SetTimerEvent(uint m_localID, UUID m_itemID, double sec)
Definition: Timer.cs:80
void UnSetTimerEvents(uint m_localID, UUID m_itemID)
Definition: Timer.cs:107
OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString key
Definition: ICM_Api.cs:31
Holds all the data required to execute a scripting event.
Definition: Helpers.cs:281
void CreateFromData(uint localID, UUID itemID, UUID objectID, Object[] data)
Definition: Timer.cs:170