Caps.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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.IO;
  31. using System.Reflection;
  32. using System.Threading;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Framework.Servers.HttpServer;
  38. using OpenSim.Services.Interfaces;
  39. // using OpenSim.Region.Framework.Interfaces;
  40. namespace OpenSim.Framework.Capabilities
  41. {
  42. /// <summary>
  43. /// XXX Probably not a particularly nice way of allow us to get the scene presence from the scene (chiefly so that
  44. /// we can popup a message on the user's client if the inventory service has permanently failed). But I didn't want
  45. /// to just pass the whole Scene into CAPS.
  46. /// </summary>
  47. public delegate IClientAPI GetClientDelegate(UUID agentID);
  48. public class Caps
  49. {
  50. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. private string m_httpListenerHostName;
  52. private uint m_httpListenPort;
  53. /// <summary>
  54. /// This is the uuid portion of every CAPS path. It is used to make capability urls private to the requester.
  55. /// </summary>
  56. private string m_capsObjectPath;
  57. public string CapsObjectPath { get { return m_capsObjectPath; } }
  58. private CapsHandlers m_capsHandlers;
  59. private Dictionary<string, PollServiceEventArgs> m_pollServiceHandlers
  60. = new Dictionary<string, PollServiceEventArgs>();
  61. private Dictionary<string, string> m_externalCapsHandlers = new Dictionary<string, string>();
  62. private IHttpServer m_httpListener;
  63. private UUID m_agentID;
  64. private string m_regionName;
  65. private ManualResetEvent m_capsActive = new ManualResetEvent(false);
  66. public UUID AgentID
  67. {
  68. get { return m_agentID; }
  69. }
  70. public string RegionName
  71. {
  72. get { return m_regionName; }
  73. }
  74. public string HostName
  75. {
  76. get { return m_httpListenerHostName; }
  77. }
  78. public uint Port
  79. {
  80. get { return m_httpListenPort; }
  81. }
  82. public IHttpServer HttpListener
  83. {
  84. get { return m_httpListener; }
  85. }
  86. public bool SSLCaps
  87. {
  88. get { return m_httpListener.UseSSL; }
  89. }
  90. public string SSLCommonName
  91. {
  92. get { return m_httpListener.SSLCommonName; }
  93. }
  94. public CapsHandlers CapsHandlers
  95. {
  96. get { return m_capsHandlers; }
  97. }
  98. public Dictionary<string, string> ExternalCapsHandlers
  99. {
  100. get { return m_externalCapsHandlers; }
  101. }
  102. [Flags]
  103. public enum CapsFlags:uint
  104. {
  105. None = 0,
  106. SentSeeds = 1,
  107. ObjectAnim = 0x10
  108. }
  109. public CapsFlags Flags { get; set;}
  110. public Caps(IHttpServer httpServer, string httpListen, uint httpPort, string capsPath,
  111. UUID agent, string regionName)
  112. {
  113. m_capsObjectPath = capsPath;
  114. m_httpListener = httpServer;
  115. m_httpListenerHostName = httpListen;
  116. m_httpListenPort = httpPort;
  117. if (httpServer != null && httpServer.UseSSL)
  118. {
  119. m_httpListenPort = httpServer.SSLPort;
  120. httpListen = httpServer.SSLCommonName;
  121. httpPort = httpServer.SSLPort;
  122. }
  123. m_agentID = agent;
  124. m_capsHandlers = new CapsHandlers(httpServer, httpListen, httpPort);
  125. m_regionName = regionName;
  126. Flags = CapsFlags.None;
  127. m_capsActive.Reset();
  128. }
  129. ~Caps()
  130. {
  131. Flags = CapsFlags.None;
  132. if (m_capsActive!= null)
  133. {
  134. m_capsActive.Dispose();
  135. m_capsActive = null;
  136. }
  137. }
  138. /// <summary>
  139. /// Register a handler. This allows modules to register handlers.
  140. /// </summary>
  141. /// <param name="capName"></param>
  142. /// <param name="handler"></param>
  143. public void RegisterHandler(string capName, IRequestHandler handler)
  144. {
  145. //m_log.DebugFormat("[CAPS]: Registering handler for \"{0}\": path {1}", capName, handler.Path);
  146. m_capsHandlers[capName] = handler;
  147. }
  148. public void RegisterPollHandler(string capName, PollServiceEventArgs pollServiceHandler)
  149. {
  150. // m_log.DebugFormat(
  151. // "[CAPS]: Registering handler with name {0}, url {1} for {2}",
  152. // capName, pollServiceHandler.Url, m_agentID, m_regionName);
  153. m_pollServiceHandlers.Add(capName, pollServiceHandler);
  154. m_httpListener.AddPollServiceHTTPHandler(pollServiceHandler.Url, pollServiceHandler);
  155. // uint port = (MainServer.Instance == null) ? 0 : MainServer.Instance.Port;
  156. // string protocol = "http";
  157. // string hostName = m_httpListenerHostName;
  158. //
  159. // if (MainServer.Instance.UseSSL)
  160. // {
  161. // hostName = MainServer.Instance.SSLCommonName;
  162. // port = MainServer.Instance.SSLPort;
  163. // protocol = "https";
  164. // }
  165. // RegisterHandler(
  166. // capName, String.Format("{0}://{1}:{2}{3}", protocol, hostName, port, pollServiceHandler.Url));
  167. }
  168. /// <summary>
  169. /// Register an external handler. The service for this capability is somewhere else
  170. /// given by the URL.
  171. /// </summary>
  172. /// <param name="capsName"></param>
  173. /// <param name="url"></param>
  174. public void RegisterHandler(string capsName, string url)
  175. {
  176. m_externalCapsHandlers.Add(capsName, url);
  177. }
  178. /// <summary>
  179. /// Remove all CAPS service handlers.
  180. /// </summary>
  181. public void DeregisterHandlers()
  182. {
  183. foreach (string capsName in m_capsHandlers.Caps)
  184. {
  185. m_capsHandlers.Remove(capsName);
  186. }
  187. foreach (PollServiceEventArgs handler in m_pollServiceHandlers.Values)
  188. {
  189. m_httpListener.RemovePollServiceHTTPHandler("", handler.Url);
  190. }
  191. m_pollServiceHandlers.Clear();
  192. }
  193. public bool TryGetPollHandler(string name, out PollServiceEventArgs pollHandler)
  194. {
  195. return m_pollServiceHandlers.TryGetValue(name, out pollHandler);
  196. }
  197. public Dictionary<string, PollServiceEventArgs> GetPollHandlers()
  198. {
  199. return new Dictionary<string, PollServiceEventArgs>(m_pollServiceHandlers);
  200. }
  201. /// <summary>
  202. /// Return an LLSD-serializable Hashtable describing the
  203. /// capabilities and their handler details.
  204. /// </summary>
  205. /// <param name="excludeSeed">If true, then exclude the seed cap.</param>
  206. public Hashtable GetCapsDetails(bool excludeSeed, List<string> requestedCaps)
  207. {
  208. Hashtable caps = CapsHandlers.GetCapsDetails(excludeSeed, requestedCaps);
  209. lock (m_pollServiceHandlers)
  210. {
  211. foreach (KeyValuePair <string, PollServiceEventArgs> kvp in m_pollServiceHandlers)
  212. {
  213. if (!requestedCaps.Contains(kvp.Key))
  214. continue;
  215. string hostName = m_httpListenerHostName;
  216. uint port = (MainServer.Instance == null) ? 0 : MainServer.Instance.Port;
  217. string protocol = "http";
  218. if (MainServer.Instance.UseSSL)
  219. {
  220. hostName = MainServer.Instance.SSLCommonName;
  221. port = MainServer.Instance.SSLPort;
  222. protocol = "https";
  223. }
  224. //
  225. // caps.RegisterHandler("FetchInventoryDescendents2", String.Format("{0}://{1}:{2}{3}", protocol, hostName, port, capUrl));
  226. caps[kvp.Key] = string.Format("{0}://{1}:{2}{3}", protocol, hostName, port, kvp.Value.Url);
  227. }
  228. }
  229. // Add the external too
  230. foreach (KeyValuePair<string, string> kvp in ExternalCapsHandlers)
  231. {
  232. if (!requestedCaps.Contains(kvp.Key))
  233. continue;
  234. caps[kvp.Key] = kvp.Value;
  235. }
  236. Flags |= CapsFlags.SentSeeds;
  237. return caps;
  238. }
  239. public void Activate()
  240. {
  241. m_capsActive.Set();
  242. }
  243. public bool WaitForActivation()
  244. {
  245. // Wait for 30s. If that elapses, return false and run without caps
  246. return m_capsActive.WaitOne(120000);
  247. }
  248. }
  249. }