PageRenderTime 110ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/Plugins/Timer.cs

https://bitbucket.org/KyanhaLLC/opensim
C# | 203 lines | 134 code | 25 blank | 44 comment | 9 complexity | 28c463db092ce00ad97278505a526378 MD5 | raw file
Possible License(s): Unlicense, MIT, BSD-3-Clause
  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. lock (TimerListLock)
  112. {
  113. // Go through all timers
  114. Dictionary<string, TimerInfo>.ValueCollection tvals = Timers.Values;
  115. foreach (TimerInfo ts in tvals)
  116. {
  117. // Time has passed?
  118. if (ts.next < DateTime.Now.Ticks)
  119. {
  120. //m_log.Debug("Time has passed: Now: " + DateTime.Now.Ticks + ", Passed: " + ts.next);
  121. // Add it to queue
  122. m_CmdManager.m_ScriptEngine.PostScriptEvent(ts.itemID,
  123. new EventParams("timer", new Object[0],
  124. new DetectParams[0]));
  125. // set next interval
  126. //ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  127. ts.next = DateTime.Now.Ticks + ts.interval;
  128. }
  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. }