OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
Caps.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.IO;
32 using System.Reflection;
33 using System.Threading;
34 using log4net;
35 using Nini.Config;
36 using OpenMetaverse;
37 using OpenSim.Framework.Servers;
38 using OpenSim.Framework.Servers.HttpServer;
39 using OpenSim.Services.Interfaces;
40 
41 // using OpenSim.Region.Framework.Interfaces;
42 
43 namespace OpenSim.Framework.Capabilities
44 {
50  public delegate IClientAPI GetClientDelegate(UUID agentID);
51 
52  public class Caps
53  {
54 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
55 
56  private string m_httpListenerHostName;
57  private uint m_httpListenPort;
58 
62  private string m_capsObjectPath;
63  public string CapsObjectPath { get { return m_capsObjectPath; } }
64 
65  private CapsHandlers m_capsHandlers;
66 
67  private Dictionary<string, PollServiceEventArgs> m_pollServiceHandlers
68  = new Dictionary<string, PollServiceEventArgs>();
69 
70  private Dictionary<string, string> m_externalCapsHandlers = new Dictionary<string, string>();
71 
72  private IHttpServer m_httpListener;
73  private UUID m_agentID;
74  private string m_regionName;
75  private ManualResetEvent m_capsActive = new ManualResetEvent(false);
76 
77  public UUID AgentID
78  {
79  get { return m_agentID; }
80  }
81 
82  public string RegionName
83  {
84  get { return m_regionName; }
85  }
86 
87  public string HostName
88  {
89  get { return m_httpListenerHostName; }
90  }
91 
92  public uint Port
93  {
94  get { return m_httpListenPort; }
95  }
96 
98  {
99  get { return m_httpListener; }
100  }
101 
102  public bool SSLCaps
103  {
104  get { return m_httpListener.UseSSL; }
105  }
106 
107  public string SSLCommonName
108  {
109  get { return m_httpListener.SSLCommonName; }
110  }
111 
113  {
114  get { return m_capsHandlers; }
115  }
116 
117  public Dictionary<string, string> ExternalCapsHandlers
118  {
119  get { return m_externalCapsHandlers; }
120  }
121 
122  public Caps(IHttpServer httpServer, string httpListen, uint httpPort, string capsPath,
123  UUID agent, string regionName)
124  {
125  m_capsObjectPath = capsPath;
126  m_httpListener = httpServer;
127  m_httpListenerHostName = httpListen;
128 
129  m_httpListenPort = httpPort;
130 
131  if (httpServer != null && httpServer.UseSSL)
132  {
133  m_httpListenPort = httpServer.SSLPort;
134  httpListen = httpServer.SSLCommonName;
135  httpPort = httpServer.SSLPort;
136  }
137 
138  m_agentID = agent;
139  m_capsHandlers = new CapsHandlers(httpServer, httpListen, httpPort, (httpServer == null) ? false : httpServer.UseSSL);
140  m_regionName = regionName;
141  m_capsActive.Reset();
142  }
143 
149  public void RegisterHandler(string capName, IRequestHandler handler)
150  {
151  //m_log.DebugFormat("[CAPS]: Registering handler for \"{0}\": path {1}", capName, handler.Path);
152  m_capsHandlers[capName] = handler;
153  }
154 
155  public void RegisterPollHandler(string capName, PollServiceEventArgs pollServiceHandler)
156  {
157 // m_log.DebugFormat(
158 // "[CAPS]: Registering handler with name {0}, url {1} for {2}",
159 // capName, pollServiceHandler.Url, m_agentID, m_regionName);
160 
161  m_pollServiceHandlers.Add(capName, pollServiceHandler);
162 
163  m_httpListener.AddPollServiceHTTPHandler(pollServiceHandler.Url, pollServiceHandler);
164 
165 // uint port = (MainServer.Instance == null) ? 0 : MainServer.Instance.Port;
166 // string protocol = "http";
167 // string hostName = m_httpListenerHostName;
168 //
169 // if (MainServer.Instance.UseSSL)
170 // {
171 // hostName = MainServer.Instance.SSLCommonName;
172 // port = MainServer.Instance.SSLPort;
173 // protocol = "https";
174 // }
175 
176 // RegisterHandler(
177 // capName, String.Format("{0}://{1}:{2}{3}", protocol, hostName, port, pollServiceHandler.Url));
178  }
179 
186  public void RegisterHandler(string capsName, string url)
187  {
188  m_externalCapsHandlers.Add(capsName, url);
189  }
190 
194  public void DeregisterHandlers()
195  {
196  foreach (string capsName in m_capsHandlers.Caps)
197  {
198  m_capsHandlers.Remove(capsName);
199  }
200 
201  foreach (PollServiceEventArgs handler in m_pollServiceHandlers.Values)
202  {
203  m_httpListener.RemovePollServiceHTTPHandler("", handler.Url);
204  }
205  }
206 
207  public bool TryGetPollHandler(string name, out PollServiceEventArgs pollHandler)
208  {
209  return m_pollServiceHandlers.TryGetValue(name, out pollHandler);
210  }
211 
212  public Dictionary<string, PollServiceEventArgs> GetPollHandlers()
213  {
214  return new Dictionary<string, PollServiceEventArgs>(m_pollServiceHandlers);
215  }
216 
222  public Hashtable GetCapsDetails(bool excludeSeed, List<string> requestedCaps)
223  {
224  Hashtable caps = CapsHandlers.GetCapsDetails(excludeSeed, requestedCaps);
225 
226  lock (m_pollServiceHandlers)
227  {
228  foreach (KeyValuePair <string, PollServiceEventArgs> kvp in m_pollServiceHandlers)
229  {
230  if (!requestedCaps.Contains(kvp.Key))
231  continue;
232 
233  string hostName = m_httpListenerHostName;
234  uint port = (MainServer.Instance == null) ? 0 : MainServer.Instance.Port;
235  string protocol = "http";
236 
238  {
239  hostName = MainServer.Instance.SSLCommonName;
240  port = MainServer.Instance.SSLPort;
241  protocol = "https";
242  }
243  //
244  // caps.RegisterHandler("FetchInventoryDescendents2", String.Format("{0}://{1}:{2}{3}", protocol, hostName, port, capUrl));
245 
246  caps[kvp.Key] = string.Format("{0}://{1}:{2}{3}", protocol, hostName, port, kvp.Value.Url);
247  }
248  }
249 
250  // Add the external too
251  foreach (KeyValuePair<string, string> kvp in ExternalCapsHandlers)
252  {
253  if (!requestedCaps.Contains(kvp.Key))
254  continue;
255 
256  caps[kvp.Key] = kvp.Value;
257  }
258 
259  return caps;
260  }
261 
262  public void Activate()
263  {
264  m_capsActive.Set();
265  }
266 
267  public bool WaitForActivation()
268  {
269  // Wait for 30s. If that elapses, return false and run without caps
270  return m_capsActive.WaitOne(120000);
271  }
272  }
273 }
bool TryGetPollHandler(string name, out PollServiceEventArgs pollHandler)
Definition: Caps.cs:207
void RegisterPollHandler(string capName, PollServiceEventArgs pollServiceHandler)
Definition: Caps.cs:155
Interface to OpenSimulator's built in HTTP server. Use this to register handlers (http, llsd, xmlrpc, etc.) for given URLs.
Definition: IHttpServer.cs:36
CapsHandlers is a cap handler container but also takes care of adding and removing cap handlers to an...
Definition: CapsHandlers.cs:40
void DeregisterHandlers()
Remove all CAPS service handlers.
Definition: Caps.cs:194
Dictionary< string, PollServiceEventArgs > GetPollHandlers()
Definition: Caps.cs:212
void RegisterHandler(string capsName, string url)
Register an external handler. The service for this capability is somewhere else given by the URL...
Definition: Caps.cs:186
void RegisterHandler(string capName, IRequestHandler handler)
Register a handler. This allows modules to register handlers.
Definition: Caps.cs:149
Interactive OpenSim region server
Definition: OpenSim.cs:55
static BaseHttpServer Instance
Set the main HTTP server instance.
Definition: MainServer.cs:83
delegate IClientAPI GetClientDelegate(UUID agentID)
XXX Probably not a particularly nice way of allow us to get the scene presence from the scene (chiefl...
Caps(IHttpServer httpServer, string httpListen, uint httpPort, string capsPath, UUID agent, string regionName)
Definition: Caps.cs:122
System.Net.HttpListener HttpListener
Hashtable GetCapsDetails(bool excludeSeed, List< string > requestedCaps)
Return an LLSD-serializable Hashtable describing the capabilities and their handler details...
Definition: Caps.cs:222