XmlRpcGridRouterModule.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 OpenSim.Framework;
  34. using OpenSim.Framework.Communications;
  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 channel;
  45. public string uri;
  46. }
  47. public class XmlRpcGridRouter : IRegionModule, IXmlRpcRouter
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private Dictionary<UUID, UUID> m_Channels =
  51. new Dictionary<UUID, UUID>();
  52. private bool m_Enabled = false;
  53. private string m_ServerURI = String.Empty;
  54. public void Initialise(Scene scene, IConfigSource config)
  55. {
  56. IConfig startupConfig = config.Configs["Startup"];
  57. if (startupConfig == null)
  58. return;
  59. if (startupConfig.GetString("XmlRpcRouterModule",
  60. "XmlRpcRouterModule") == "XmlRpcGridRouterModule")
  61. {
  62. m_ServerURI = startupConfig.GetString("XmlRpcHubURI", String.Empty);
  63. if (m_ServerURI == String.Empty)
  64. {
  65. m_log.Error("[XMLRPC GRID ROUTER] Module configured but no URI given. Disabling");
  66. return;
  67. }
  68. scene.RegisterModuleInterface<IXmlRpcRouter>(this);
  69. m_Enabled = true;
  70. }
  71. }
  72. public void PostInitialise()
  73. {
  74. }
  75. public void Close()
  76. {
  77. }
  78. public string Name
  79. {
  80. get { return "XmlRpcGridRouterModule"; }
  81. }
  82. public bool IsSharedModule
  83. {
  84. get { return false; }
  85. }
  86. public void RegisterNewReceiver(IScriptModule scriptEngine, UUID channel, UUID objectID, UUID itemID, string uri)
  87. {
  88. if (!m_Channels.ContainsKey(itemID))
  89. {
  90. XmlRpcInfo info = new XmlRpcInfo();
  91. info.channel = channel;
  92. info.uri = uri;
  93. bool success = SynchronousRestObjectPoster.BeginPostObject<XmlRpcInfo, bool>(
  94. "POST", m_ServerURI+"/RegisterChannel/", info);
  95. if (!success)
  96. {
  97. m_log.Error("[XMLRPC GRID ROUTER] Error contacting server");
  98. }
  99. m_Channels[itemID] = channel;
  100. }
  101. }
  102. public void ScriptRemoved(UUID itemID)
  103. {
  104. if (!m_Enabled)
  105. return;
  106. if (m_Channels.ContainsKey(itemID))
  107. {
  108. bool success = SynchronousRestObjectPoster.BeginPostObject<UUID, bool>(
  109. "POST", m_ServerURI+"/RemoveChannel/", m_Channels[itemID]);
  110. if (!success)
  111. {
  112. m_log.Error("[XMLRPC GRID ROUTER] Error contacting server");
  113. }
  114. m_Channels.Remove(itemID);
  115. }
  116. }
  117. public void ObjectRemoved(UUID objectID)
  118. {
  119. }
  120. }
  121. }