ExecutorBase.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 OpenSim 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.Runtime.Remoting.Lifetime;
  30. using System.Text;
  31. namespace OpenSim.Region.ScriptEngine.Common
  32. {
  33. public abstract class ExecutorBase : MarshalByRefObject
  34. {
  35. /// <summary>
  36. /// Contains the script to execute functions in.
  37. /// </summary>
  38. protected IScript m_Script;
  39. /// <summary>
  40. /// If set to False events will not be executed.
  41. /// </summary>
  42. protected bool m_Running = true;
  43. /// <summary>
  44. /// True indicates that the ScriptManager has stopped
  45. /// this script. This prevents a script that has been
  46. /// stopped as part of deactivation from being
  47. /// resumed by a pending llSetScriptState request.
  48. /// </summary>
  49. protected bool m_Disable = false;
  50. /// <summary>
  51. /// Indicate the scripts current running status.
  52. /// </summary>
  53. public bool Running
  54. {
  55. get { return m_Running; }
  56. set {
  57. if(!m_Disable)
  58. m_Running = value;
  59. }
  60. }
  61. /// <summary>
  62. /// Create a new instance of ExecutorBase
  63. /// </summary>
  64. /// <param name="Script"></param>
  65. public ExecutorBase(IScript Script)
  66. {
  67. m_Script = Script;
  68. }
  69. /// <summary>
  70. /// Make sure our object does not timeout when in AppDomain. (Called by ILease base class)
  71. /// </summary>
  72. /// <returns></returns>
  73. public override Object InitializeLifetimeService()
  74. {
  75. //Console.WriteLine("Executor: InitializeLifetimeService()");
  76. // return null;
  77. ILease lease = (ILease)base.InitializeLifetimeService();
  78. if (lease.CurrentState == LeaseState.Initial)
  79. {
  80. lease.InitialLeaseTime = TimeSpan.Zero; // TimeSpan.FromMinutes(1);
  81. // lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
  82. // lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
  83. }
  84. return lease;
  85. }
  86. /// <summary>
  87. /// Get current AppDomain
  88. /// </summary>
  89. /// <returns>Current AppDomain</returns>
  90. public AppDomain GetAppDomain()
  91. {
  92. return AppDomain.CurrentDomain;
  93. }
  94. /// <summary>
  95. /// Execute a specific function/event in script.
  96. /// </summary>
  97. /// <param name="FunctionName">Name of function to execute</param>
  98. /// <param name="args">Arguments to pass to function</param>
  99. public void ExecuteEvent(string FunctionName, object[] args)
  100. {
  101. if (m_Running == false)
  102. {
  103. // Script is inactive, do not execute!
  104. return;
  105. }
  106. DoExecuteEvent(FunctionName, args);
  107. }
  108. protected abstract void DoExecuteEvent(string FunctionName, object[] args);
  109. /// <summary>
  110. /// Stop script from running. Event execution will be ignored.
  111. /// </summary>
  112. public void StopScript()
  113. {
  114. m_Running = false;
  115. m_Disable = true;
  116. }
  117. }
  118. }