CapabilitiesModule.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 copyrightD
  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 log4net;
  32. using Nini.Config;
  33. using Mono.Addins;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Console;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.Framework.Scenes;
  39. using Caps=OpenSim.Framework.Capabilities.Caps;
  40. namespace OpenSim.Region.CoreModules.Framework
  41. {
  42. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
  43. public class CapabilitiesModule : INonSharedRegionModule, ICapabilitiesModule
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. protected Scene m_scene;
  47. /// <summary>
  48. /// Each agent has its own capabilities handler.
  49. /// </summary>
  50. protected Dictionary<UUID, Caps> m_capsObjects = new Dictionary<UUID, Caps>();
  51. protected Dictionary<UUID, string> capsPaths = new Dictionary<UUID, string>();
  52. protected Dictionary<UUID, Dictionary<ulong, string>> childrenSeeds
  53. = new Dictionary<UUID, Dictionary<ulong, string>>();
  54. public void Initialise(IConfigSource source)
  55. {
  56. }
  57. public void AddRegion(Scene scene)
  58. {
  59. m_scene = scene;
  60. m_scene.RegisterModuleInterface<ICapabilitiesModule>(this);
  61. MainConsole.Instance.Commands.AddCommand("Capabilities", false, "show caps",
  62. "show caps",
  63. "Shows all registered capabilities", CapabilitiesCommand);
  64. }
  65. public void RegionLoaded(Scene scene)
  66. {
  67. }
  68. public void RemoveRegion(Scene scene)
  69. {
  70. m_scene.UnregisterModuleInterface<ICapabilitiesModule>(this);
  71. }
  72. public void PostInitialise()
  73. {
  74. }
  75. public void Close() {}
  76. public string Name
  77. {
  78. get { return "Capabilities Module"; }
  79. }
  80. public Type ReplaceableInterface
  81. {
  82. get { return null; }
  83. }
  84. public void CreateCaps(UUID agentId)
  85. {
  86. if (m_scene.RegionInfo.EstateSettings.IsBanned(agentId))
  87. return;
  88. String capsObjectPath = GetCapsPath(agentId);
  89. if (m_capsObjects.ContainsKey(agentId))
  90. {
  91. Caps oldCaps = m_capsObjects[agentId];
  92. m_log.DebugFormat(
  93. "[CAPS]: Recreating caps for agent {0}. Old caps path {1}, new caps path {2}. ",
  94. agentId, oldCaps.CapsObjectPath, capsObjectPath);
  95. // This should not happen. The caller code is confused. We need to fix that.
  96. // CAPs can never be reregistered, or the client will be confused.
  97. // Hence this return here.
  98. //return;
  99. }
  100. Caps caps = new Caps(MainServer.Instance, m_scene.RegionInfo.ExternalHostName,
  101. (MainServer.Instance == null) ? 0: MainServer.Instance.Port,
  102. capsObjectPath, agentId, m_scene.RegionInfo.RegionName);
  103. m_capsObjects[agentId] = caps;
  104. m_scene.EventManager.TriggerOnRegisterCaps(agentId, caps);
  105. }
  106. public void RemoveCaps(UUID agentId)
  107. {
  108. if (childrenSeeds.ContainsKey(agentId))
  109. {
  110. childrenSeeds.Remove(agentId);
  111. }
  112. lock (m_capsObjects)
  113. {
  114. if (m_capsObjects.ContainsKey(agentId))
  115. {
  116. m_capsObjects[agentId].DeregisterHandlers();
  117. m_scene.EventManager.TriggerOnDeregisterCaps(agentId, m_capsObjects[agentId]);
  118. m_capsObjects.Remove(agentId);
  119. }
  120. else
  121. {
  122. m_log.WarnFormat(
  123. "[CAPS]: Received request to remove CAPS handler for root agent {0} in {1}, but no such CAPS handler found!",
  124. agentId, m_scene.RegionInfo.RegionName);
  125. }
  126. }
  127. }
  128. public Caps GetCapsForUser(UUID agentId)
  129. {
  130. lock (m_capsObjects)
  131. {
  132. if (m_capsObjects.ContainsKey(agentId))
  133. {
  134. return m_capsObjects[agentId];
  135. }
  136. }
  137. return null;
  138. }
  139. public void SetAgentCapsSeeds(AgentCircuitData agent)
  140. {
  141. capsPaths[agent.AgentID] = agent.CapsPath;
  142. childrenSeeds[agent.AgentID]
  143. = ((agent.ChildrenCapSeeds == null) ? new Dictionary<ulong, string>() : agent.ChildrenCapSeeds);
  144. }
  145. public string GetCapsPath(UUID agentId)
  146. {
  147. if (capsPaths.ContainsKey(agentId))
  148. {
  149. return capsPaths[agentId];
  150. }
  151. return null;
  152. }
  153. public Dictionary<ulong, string> GetChildrenSeeds(UUID agentID)
  154. {
  155. Dictionary<ulong, string> seeds = null;
  156. if (childrenSeeds.TryGetValue(agentID, out seeds))
  157. return seeds;
  158. return new Dictionary<ulong, string>();
  159. }
  160. public void DropChildSeed(UUID agentID, ulong handle)
  161. {
  162. Dictionary<ulong, string> seeds;
  163. if (childrenSeeds.TryGetValue(agentID, out seeds))
  164. {
  165. seeds.Remove(handle);
  166. }
  167. }
  168. public string GetChildSeed(UUID agentID, ulong handle)
  169. {
  170. Dictionary<ulong, string> seeds;
  171. string returnval;
  172. if (childrenSeeds.TryGetValue(agentID, out seeds))
  173. {
  174. if (seeds.TryGetValue(handle, out returnval))
  175. return returnval;
  176. }
  177. return null;
  178. }
  179. public void SetChildrenSeed(UUID agentID, Dictionary<ulong, string> seeds)
  180. {
  181. //m_log.DebugFormat(" !!! Setting child seeds in {0} to {1}", m_scene.RegionInfo.RegionName, seeds.Count);
  182. childrenSeeds[agentID] = seeds;
  183. }
  184. public void DumpChildrenSeeds(UUID agentID)
  185. {
  186. m_log.Info("================ ChildrenSeed "+m_scene.RegionInfo.RegionName+" ================");
  187. foreach (KeyValuePair<ulong, string> kvp in childrenSeeds[agentID])
  188. {
  189. uint x, y;
  190. Utils.LongToUInts(kvp.Key, out x, out y);
  191. x = x / Constants.RegionSize;
  192. y = y / Constants.RegionSize;
  193. m_log.Info(" >> "+x+", "+y+": "+kvp.Value);
  194. }
  195. }
  196. private void CapabilitiesCommand(string module, string[] cmdparams)
  197. {
  198. System.Text.StringBuilder caps = new System.Text.StringBuilder();
  199. caps.AppendFormat("Region {0}:\n", m_scene.RegionInfo.RegionName);
  200. foreach (KeyValuePair<UUID, Caps> kvp in m_capsObjects)
  201. {
  202. caps.AppendFormat("** User {0}:\n", kvp.Key);
  203. for (IDictionaryEnumerator kvp2 = kvp.Value.CapsHandlers.CapsDetails.GetEnumerator(); kvp2.MoveNext(); )
  204. {
  205. Uri uri = new Uri(kvp2.Value.ToString());
  206. caps.AppendFormat(" {0} = {1}\n", kvp2.Key, uri.PathAndQuery);
  207. }
  208. foreach (KeyValuePair<string, string> kvp3 in kvp.Value.ExternalCapsHandlers)
  209. caps.AppendFormat(" {0} = {1}\n", kvp3.Key, kvp3.Value);
  210. }
  211. MainConsole.Instance.Output(caps.ToString());
  212. }
  213. }
  214. }