Timer.cs 6.7 KB

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