PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/OpenSim/Services/Scheduler/Scheduler.cs

https://bitbucket.org/VirtualReality/aurora-sim
C# | 234 lines | 160 code | 32 blank | 42 comment | 18 complexity | 6993715f3db37fea4ca83af47ccccf0b MD5 | raw file
  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/, 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 Aurora-Sim 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 System.Linq;
  30. using System.Timers;
  31. using Aurora.DataManager;
  32. using Aurora.Framework;
  33. using Aurora.Simulation.Base;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenSim.Services.Interfaces;
  37. namespace OpenSim.Services
  38. {
  39. public class Scheduler : ConnectorBase, IScheduleService, IService
  40. {
  41. public AuroraEventManager EventManager = new AuroraEventManager();
  42. private ISchedulerDataPlugin m_database;
  43. private bool m_enabled = false;
  44. private readonly Timer scheduleTimer = new Timer();
  45. #region Implementation of IService
  46. /// <summary>
  47. /// Set up and register the module
  48. /// </summary>
  49. /// <param name = "config">Config file</param>
  50. /// <param name = "registry">Place to register the modules into</param>
  51. public void Initialize(IConfigSource config, IRegistryCore registry)
  52. {
  53. registry.RegisterModuleInterface<IScheduleService>(this);
  54. base.Init(registry, "Scheduler");
  55. }
  56. /// <summary>
  57. /// Load other IService modules now that this is set up
  58. /// </summary>
  59. /// <param name = "config">Config file</param>
  60. /// <param name = "registry">Place to register and retrieve module interfaces</param>
  61. public void Start(IConfigSource config, IRegistryCore registry)
  62. {
  63. }
  64. /// <summary>
  65. /// All modules have started up and it is ready to run
  66. /// </summary>
  67. public void FinishedStartup()
  68. {
  69. if (!m_doRemoteCalls)
  70. {
  71. m_database = DataManager.RequestPlugin<ISchedulerDataPlugin>();
  72. if (m_database != null)
  73. m_enabled = true;
  74. if (m_enabled)
  75. {
  76. // don't want to start to soon
  77. scheduleTimer.Interval = 60000;
  78. scheduleTimer.Elapsed += t_Elapsed;
  79. scheduleTimer.Start();
  80. }
  81. }
  82. }
  83. #endregion
  84. #region Implementation of IScheduleService
  85. public bool Register(SchedulerItem I, OnGenericEventHandler handler)
  86. {
  87. if (m_doRemoteCalls) return false;
  88. EventManager.RegisterEventHandler(I.FireFunction, handler);
  89. return true;
  90. }
  91. public bool Register(string fName, OnGenericEventHandler handler)
  92. {
  93. if (m_doRemoteCalls) return false;
  94. EventManager.RegisterEventHandler(fName, handler);
  95. return true;
  96. }
  97. [CanBeReflected(ThreatLevel = ThreatLevel.High, RenamedMethod = "SchedulerSave")]
  98. public string Save(SchedulerItem I)
  99. {
  100. if (m_doRemoteCalls)
  101. return (string)DoRemote(I);
  102. return m_database.SchedulerSave(I);
  103. }
  104. [CanBeReflected(ThreatLevel = ThreatLevel.High, RenamedMethod = "SchedulerRemove")]
  105. public void Remove(string id)
  106. {
  107. if (m_doRemoteCalls)
  108. {
  109. DoRemote(id);
  110. return;
  111. }
  112. m_database.SchedulerRemove(id);
  113. }
  114. [CanBeReflected(ThreatLevel = ThreatLevel.Low, RenamedMethod = "SchedulerExist")]
  115. public bool Exist(string scdID)
  116. {
  117. if (m_doRemoteCalls)
  118. return (bool) DoRemote(scdID);
  119. return m_database.SchedulerExist(scdID);
  120. }
  121. [CanBeReflected(ThreatLevel = ThreatLevel.Low, RenamedMethod = "SchedulerGet")]
  122. public SchedulerItem Get(string ID)
  123. {
  124. if (m_doRemoteCalls)
  125. return (SchedulerItem)DoRemote(ID);
  126. return m_database.Get(ID);
  127. }
  128. [CanBeReflected(ThreatLevel = ThreatLevel.Low, RenamedMethod = "SchedulerGet")]
  129. public SchedulerItem Get(string scheduleFor, string fireFunction)
  130. {
  131. if (m_doRemoteCalls)
  132. return (SchedulerItem)DoRemote(scheduleFor, fireFunction);
  133. return m_database.Get(scheduleFor, fireFunction);
  134. }
  135. #endregion
  136. #region Timer
  137. private void t_Elapsed(object sender, ElapsedEventArgs e)
  138. {
  139. scheduleTimer.Enabled = false;
  140. try
  141. {
  142. List<SchedulerItem> CurrentSchedule = m_database.ToRun();
  143. foreach (SchedulerItem I in CurrentSchedule)
  144. {
  145. FireEvent(I);
  146. }
  147. }
  148. catch (Exception ee)
  149. {
  150. MainConsole.Instance.Error("[Scheduler] t_Elapsed Error ", ee);
  151. }
  152. finally
  153. {
  154. scheduleTimer.Enabled = true;
  155. }
  156. }
  157. private void FireEvent(SchedulerItem I)
  158. {
  159. try
  160. {
  161. // save chagnes before it fires in case its chagned during the fire
  162. I = m_database.SaveHistory(I);
  163. if (I.RunOnce) I.Enabled = false;
  164. if (I.Enabled) I.CalculateNextRunTime(I.TimeToRun);
  165. if (!I.HisotryKeep)
  166. m_database.HistoryDeleteOld(I);
  167. m_database.SchedulerSave(I);
  168. // now fire
  169. List<Object> reciept = EventManager.FireGenericEventHandler(I.FireFunction, I.FireParams);
  170. if (!I.HistoryReciept)
  171. I = m_database.SaveHistoryComplete(I);
  172. else
  173. {
  174. #if (!ISWIN)
  175. foreach (Object o in reciept)
  176. {
  177. string results = (string)o;
  178. if (results != "")
  179. {
  180. m_database.SaveHistoryCompleteReciept(I.HistoryLastID, results);
  181. }
  182. }
  183. #else
  184. foreach (string results in reciept.Cast<string>().Where(results => results != ""))
  185. {
  186. m_database.SaveHistoryCompleteReciept(I.HistoryLastID, results);
  187. }
  188. #endif
  189. }
  190. }
  191. catch (Exception e)
  192. {
  193. MainConsole.Instance.Error("[Scheduler] FireEvent Error " + I.id, e);
  194. }
  195. }
  196. public void MarkComplete(string history_id, string reciept)
  197. {
  198. m_database.SaveHistoryCompleteReciept(history_id, reciept);
  199. }
  200. #endregion
  201. }
  202. }