Executor.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using System.Runtime.Remoting.Lifetime;
  32. namespace OpenSim.Grid.ScriptEngine.Common
  33. {
  34. public class Executor : MarshalByRefObject
  35. {
  36. // Private instance for each script
  37. private IScript m_Script;
  38. private Dictionary<string, MethodInfo> Events = new Dictionary<string, MethodInfo>();
  39. private bool m_Running = true;
  40. //private List<IScript> Scripts = new List<IScript>();
  41. public Executor(IScript Script)
  42. {
  43. m_Script = Script;
  44. }
  45. // Object never expires
  46. public override Object InitializeLifetimeService()
  47. {
  48. //Console.WriteLine("Executor: InitializeLifetimeService()");
  49. // return null;
  50. ILease lease = (ILease) base.InitializeLifetimeService();
  51. if (lease.CurrentState == LeaseState.Initial)
  52. {
  53. lease.InitialLeaseTime = TimeSpan.Zero; // TimeSpan.FromMinutes(1);
  54. // lease.SponsorshipTimeout = TimeSpan.FromMinutes(2);
  55. // lease.RenewOnCallTime = TimeSpan.FromSeconds(2);
  56. }
  57. return lease;
  58. }
  59. public AppDomain GetAppDomain()
  60. {
  61. return AppDomain.CurrentDomain;
  62. }
  63. public void ExecuteEvent(string FunctionName, object[] args)
  64. {
  65. // IMPORTANT: Types and MemberInfo-derived objects require a LOT of memory.
  66. // Instead use RuntimeTypeHandle, RuntimeFieldHandle and RunTimeHandle (IntPtr) instead!
  67. //try
  68. //{
  69. if (m_Running == false)
  70. {
  71. // Script is inactive, do not execute!
  72. return;
  73. }
  74. string EventName = m_Script.State() + "_event_" + FunctionName;
  75. //type.InvokeMember(EventName, BindingFlags.InvokeMethod, null, m_Script, args);
  76. //Console.WriteLine("ScriptEngine Executor.ExecuteEvent: \"" + EventName + "\"");
  77. if (Events.ContainsKey(EventName) == false)
  78. {
  79. // Not found, create
  80. Type type = m_Script.GetType();
  81. try
  82. {
  83. MethodInfo mi = type.GetMethod(EventName);
  84. Events.Add(EventName, mi);
  85. }
  86. catch
  87. {
  88. // Event name not found, cache it as not found
  89. Events.Add(EventName, null);
  90. }
  91. }
  92. // Get event
  93. MethodInfo ev = null;
  94. Events.TryGetValue(EventName, out ev);
  95. if (ev == null) // No event by that name!
  96. {
  97. //Console.WriteLine("ScriptEngine Can not find any event named: \"" + EventName + "\"");
  98. return;
  99. }
  100. // Found
  101. //try
  102. //{
  103. // Invoke it
  104. ev.Invoke(m_Script, args);
  105. //}
  106. //catch (Exception e)
  107. //{
  108. // // TODO: Send to correct place
  109. // Console.WriteLine("ScriptEngine Exception attempting to executing script function: " + e.ToString());
  110. //}
  111. //}
  112. //catch { }
  113. }
  114. public void StopScript()
  115. {
  116. m_Running = false;
  117. }
  118. }
  119. }