ScriptTimer.cs 6.8 KB

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