SimianExternalCapsModule.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using System.IO;
  32. using System.Web;
  33. using log4net;
  34. using Nini.Config;
  35. using Mono.Addins;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. using OpenSim.Services.Interfaces;
  42. using Caps = OpenSim.Framework.Capabilities.Caps;
  43. namespace OpenSim.Services.Connectors.SimianGrid
  44. {
  45. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "SimianExternalCapsModule")]
  46. public class SimianExternalCapsModule : INonSharedRegionModule, IExternalCapsModule
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. private bool m_enabled = true;
  50. private Scene m_scene;
  51. private String m_simianURL;
  52. #region IRegionModule Members
  53. public string Name
  54. {
  55. get { return this.GetType().Name; }
  56. }
  57. public void Initialise(IConfigSource config)
  58. {
  59. try
  60. {
  61. IConfig m_config;
  62. if ((m_config = config.Configs["SimianExternalCaps"]) != null)
  63. {
  64. m_enabled = m_config.GetBoolean("Enabled", m_enabled);
  65. if ((m_config = config.Configs["SimianGrid"]) != null)
  66. {
  67. m_simianURL = m_config.GetString("SimianServiceURL");
  68. if (String.IsNullOrEmpty(m_simianURL))
  69. {
  70. //m_log.DebugFormat("[SimianGrid] service URL is not defined");
  71. m_enabled = false;
  72. return;
  73. }
  74. }
  75. }
  76. else
  77. m_enabled = false;
  78. }
  79. catch (Exception e)
  80. {
  81. m_log.ErrorFormat("[SimianExternalCaps] initialization error: {0}",e.Message);
  82. return;
  83. }
  84. }
  85. public void PostInitialise() { }
  86. public void Close() { }
  87. public void AddRegion(Scene scene)
  88. {
  89. if (! m_enabled)
  90. return;
  91. m_scene = scene;
  92. m_scene.RegisterModuleInterface<IExternalCapsModule>(this);
  93. }
  94. public void RemoveRegion(Scene scene)
  95. {
  96. if (! m_enabled)
  97. return;
  98. m_scene.EventManager.OnRegisterCaps -= RegisterCapsEventHandler;
  99. m_scene.EventManager.OnDeregisterCaps -= DeregisterCapsEventHandler;
  100. }
  101. public void RegionLoaded(Scene scene)
  102. {
  103. if (! m_enabled)
  104. return;
  105. m_scene.EventManager.OnRegisterCaps += RegisterCapsEventHandler;
  106. m_scene.EventManager.OnDeregisterCaps += DeregisterCapsEventHandler;
  107. }
  108. public Type ReplaceableInterface
  109. {
  110. get { return null; }
  111. }
  112. #endregion
  113. #region IExternalCapsModule
  114. // Eg http://grid.sciencesim.com/GridPublic/%CAP%/%OP%/"
  115. public bool RegisterExternalUserCapsHandler(UUID agentID, Caps caps, String capName, String urlSkel)
  116. {
  117. UUID cap = UUID.Random();
  118. // Call to simian to register the cap we generated
  119. // NameValueCollection requestArgs = new NameValueCollection
  120. // {
  121. // { "RequestMethod", "AddCapability" },
  122. // { "Resource", "user" },
  123. // { "Expiration", 0 },
  124. // { "OwnerID", agentID.ToString() },
  125. // { "CapabilityID", cap.ToString() }
  126. // };
  127. // OSDMap response = SimianGrid.PostToService(m_simianURL, requestArgs);
  128. Dictionary<String,String> subs = new Dictionary<String,String>();
  129. subs["%OP%"] = capName;
  130. subs["%USR%"] = agentID.ToString();
  131. subs["%CAP%"] = cap.ToString();
  132. subs["%SIM%"] = m_scene.RegionInfo.RegionID.ToString();
  133. caps.RegisterHandler(capName,ExpandSkeletonURL(urlSkel,subs));
  134. return true;
  135. }
  136. #endregion
  137. #region EventHandlers
  138. public void RegisterCapsEventHandler(UUID agentID, Caps caps) { }
  139. public void DeregisterCapsEventHandler(UUID agentID, Caps caps) { }
  140. #endregion
  141. private String ExpandSkeletonURL(String urlSkel, Dictionary<String,String> subs)
  142. {
  143. String result = urlSkel;
  144. foreach (KeyValuePair<String,String> kvp in subs)
  145. {
  146. result = result.Replace(kvp.Key,kvp.Value);
  147. }
  148. return result;
  149. }
  150. }
  151. }