Executor.cs 4.9 KB

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