Timer.cs 6.2 KB

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