XmlRpcGridRouterModule.cs 6.6 KB

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