XmlRpcGridRouterModule.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.Generic;
  29. using System.Reflection;
  30. using log4net;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using Mono.Addins;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Servers;
  36. using OpenSim.Framework.Servers.HttpServer;
  37. using OpenSim.Framework.Client;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. namespace OpenSim.Region.OptionalModules.Scripting.XmlRpcGridRouterModule
  41. {
  42. public class XmlRpcInfo
  43. {
  44. public UUID item;
  45. public UUID channel;
  46. public string uri;
  47. }
  48. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "XmlRpcGridRouter")]
  49. public class XmlRpcGridRouter : INonSharedRegionModule, IXmlRpcRouter
  50. {
  51. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. private Dictionary<UUID, UUID> m_Channels =
  53. new Dictionary<UUID, UUID>();
  54. private bool m_Enabled = false;
  55. private string m_ServerURI = String.Empty;
  56. #region INonSharedRegionModule
  57. public void Initialise(IConfigSource config)
  58. {
  59. IConfig startupConfig = config.Configs["XMLRPC"];
  60. if (startupConfig == null)
  61. return;
  62. if (startupConfig.GetString("XmlRpcRouterModule",
  63. "XmlRpcRouterModule") == "XmlRpcGridRouterModule")
  64. {
  65. m_ServerURI = startupConfig.GetString("XmlRpcHubURI", String.Empty);
  66. if (m_ServerURI == String.Empty)
  67. {
  68. m_log.Error("[XMLRPC GRID ROUTER] Module configured but no URI given. Disabling");
  69. return;
  70. }
  71. m_Enabled = true;
  72. }
  73. }
  74. public void AddRegion(Scene scene)
  75. {
  76. if (!m_Enabled)
  77. return;
  78. scene.RegisterModuleInterface<IXmlRpcRouter>(this);
  79. IScriptModule scriptEngine = scene.RequestModuleInterface<IScriptModule>();
  80. if ( scriptEngine != null )
  81. {
  82. scriptEngine.OnScriptRemoved += this.ScriptRemoved;
  83. scriptEngine.OnObjectRemoved += this.ObjectRemoved;
  84. }
  85. }
  86. public void RegionLoaded(Scene scene)
  87. {
  88. }
  89. public void RemoveRegion(Scene scene)
  90. {
  91. if (!m_Enabled)
  92. return;
  93. scene.UnregisterModuleInterface<IXmlRpcRouter>(this);
  94. }
  95. public void Close()
  96. {
  97. }
  98. public string Name
  99. {
  100. get { return "XmlRpcGridRouterModule"; }
  101. }
  102. public Type ReplaceableInterface
  103. {
  104. get { return null; }
  105. }
  106. #endregion
  107. public void RegisterNewReceiver(IScriptModule scriptEngine, UUID channel, UUID objectID, UUID itemID, string uri)
  108. {
  109. if (!m_Enabled)
  110. return;
  111. m_log.InfoFormat("[XMLRPC GRID ROUTER]: New receiver Obj: {0} Ch: {1} ID: {2} URI: {3}",
  112. objectID.ToString(), channel.ToString(), itemID.ToString(), uri);
  113. XmlRpcInfo info = new XmlRpcInfo();
  114. info.channel = channel;
  115. info.uri = uri;
  116. info.item = itemID;
  117. bool success = SynchronousRestObjectRequester.MakeRequest<XmlRpcInfo, bool>(
  118. "POST", m_ServerURI+"/RegisterChannel/", info);
  119. if (!success)
  120. {
  121. m_log.Error("[XMLRPC GRID ROUTER] Error contacting server");
  122. }
  123. m_Channels[itemID] = channel;
  124. }
  125. public void UnRegisterReceiver(string channelID, UUID itemID)
  126. {
  127. if (!m_Enabled)
  128. return;
  129. RemoveChannel(itemID);
  130. }
  131. public void ScriptRemoved(UUID itemID)
  132. {
  133. if (!m_Enabled)
  134. return;
  135. RemoveChannel(itemID);
  136. }
  137. public void ObjectRemoved(UUID objectID)
  138. {
  139. // m_log.InfoFormat("[XMLRPC GRID ROUTER]: Object Removed {0}",objectID.ToString());
  140. }
  141. private bool RemoveChannel(UUID itemID)
  142. {
  143. if(!m_Channels.ContainsKey(itemID))
  144. {
  145. //m_log.InfoFormat("[XMLRPC GRID ROUTER]: Attempted to unregister non-existing Item: {0}", itemID.ToString());
  146. return false;
  147. }
  148. XmlRpcInfo info = new XmlRpcInfo();
  149. info.channel = m_Channels[itemID];
  150. info.item = itemID;
  151. info.uri = "http://0.0.0.0:00";
  152. if (info != null)
  153. {
  154. bool success = SynchronousRestObjectRequester.MakeRequest<XmlRpcInfo, bool>(
  155. "POST", m_ServerURI+"/RemoveChannel/", info);
  156. if (!success)
  157. {
  158. m_log.Error("[XMLRPC GRID ROUTER] Error contacting server");
  159. }
  160. m_Channels.Remove(itemID);
  161. return true;
  162. }
  163. return false;
  164. }
  165. }
  166. }