Timer.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.TryGetValue(key, out TimerInfo ts))
  101. {
  102. m_CmdManager.m_ScriptEngine.CancelScriptEvent(ts.itemID, "timer");
  103. Timers.Remove(key);
  104. }
  105. }
  106. }
  107. public void CheckTimerEvents()
  108. {
  109. // Nothing to do here?
  110. if (Timers.Count == 0)
  111. return;
  112. Dictionary<string, TimerInfo> tvals;
  113. lock (TimerListLock)
  114. {
  115. // Go through all timers
  116. tvals = new Dictionary<string, TimerInfo>(Timers);
  117. }
  118. foreach (TimerInfo ts in tvals.Values)
  119. {
  120. // Time has passed?
  121. if (ts.next < DateTime.Now.Ticks)
  122. {
  123. //m_log.Debug("Time has passed: Now: " + DateTime.Now.Ticks + ", Passed: " + ts.next);
  124. // Add it to queue
  125. m_CmdManager.m_ScriptEngine.PostScriptEvent(ts.itemID,
  126. new EventParams("timer", new Object[0],
  127. new DetectParams[0]));
  128. // set next interval
  129. //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  130. ts.next = DateTime.Now.Ticks + ts.interval;
  131. }
  132. }
  133. }
  134. public Object[] GetSerializationData(UUID itemID)
  135. {
  136. List<Object> data = new List<Object>();
  137. lock (TimerListLock)
  138. {
  139. Dictionary<string, TimerInfo>.ValueCollection tvals = Timers.Values;
  140. foreach (TimerInfo ts in tvals)
  141. {
  142. if (ts.itemID == itemID)
  143. {
  144. data.Add(ts.interval);
  145. data.Add(ts.next-DateTime.Now.Ticks);
  146. }
  147. }
  148. }
  149. return data.ToArray();
  150. }
  151. public void CreateFromData(uint localID, UUID itemID, UUID objectID,
  152. Object[] data)
  153. {
  154. int idx = 0;
  155. while (idx < data.Length)
  156. {
  157. TimerInfo ts = new TimerInfo();
  158. ts.localID = localID;
  159. ts.itemID = itemID;
  160. ts.interval = (long)data[idx];
  161. ts.next = DateTime.Now.Ticks + (long)data[idx+1];
  162. idx += 2;
  163. lock (TimerListLock)
  164. {
  165. Timers.Add(MakeTimerKey(localID, itemID), ts);
  166. }
  167. }
  168. }
  169. public List<TimerInfo> GetTimersInfo()
  170. {
  171. List<TimerInfo> retList = new List<TimerInfo>();
  172. lock (TimerListLock)
  173. {
  174. foreach (TimerInfo i in Timers.Values)
  175. retList.Add(i.Clone());
  176. }
  177. return retList;
  178. }
  179. }
  180. }