CapabilitiesModule.cs 9.2 KB

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