diff options
Diffstat (limited to 'OpenSim/Region/ClientStack/Linden/Caps/WebFetchInvDescModule.cs')
-rw-r--r-- | OpenSim/Region/ClientStack/Linden/Caps/WebFetchInvDescModule.cs | 263 |
1 files changed, 211 insertions, 52 deletions
diff --git a/OpenSim/Region/ClientStack/Linden/Caps/WebFetchInvDescModule.cs b/OpenSim/Region/ClientStack/Linden/Caps/WebFetchInvDescModule.cs index 6890f4a..a823dd8 100644 --- a/OpenSim/Region/ClientStack/Linden/Caps/WebFetchInvDescModule.cs +++ b/OpenSim/Region/ClientStack/Linden/Caps/WebFetchInvDescModule.cs | |||
@@ -27,18 +27,25 @@ | |||
27 | 27 | ||
28 | using System; | 28 | using System; |
29 | using System.Collections; | 29 | using System.Collections; |
30 | using System.Collections.Generic; | ||
30 | using System.Reflection; | 31 | using System.Reflection; |
32 | using System.Threading; | ||
31 | using log4net; | 33 | using log4net; |
32 | using Nini.Config; | 34 | using Nini.Config; |
33 | using Mono.Addins; | 35 | using Mono.Addins; |
34 | using OpenMetaverse; | 36 | using OpenMetaverse; |
35 | using OpenSim.Framework; | 37 | using OpenSim.Framework; |
38 | using OpenSim.Framework.Servers; | ||
36 | using OpenSim.Framework.Servers.HttpServer; | 39 | using OpenSim.Framework.Servers.HttpServer; |
37 | using OpenSim.Region.Framework.Interfaces; | 40 | using OpenSim.Region.Framework.Interfaces; |
38 | using OpenSim.Region.Framework.Scenes; | 41 | using OpenSim.Region.Framework.Scenes; |
42 | using OpenSim.Framework.Capabilities; | ||
39 | using OpenSim.Services.Interfaces; | 43 | using OpenSim.Services.Interfaces; |
40 | using Caps = OpenSim.Framework.Capabilities.Caps; | 44 | using Caps = OpenSim.Framework.Capabilities.Caps; |
41 | using OpenSim.Capabilities.Handlers; | 45 | using OpenSim.Capabilities.Handlers; |
46 | using OpenSim.Framework.Monitoring; | ||
47 | using OpenMetaverse; | ||
48 | using OpenMetaverse.StructuredData; | ||
42 | 49 | ||
43 | namespace OpenSim.Region.ClientStack.Linden | 50 | namespace OpenSim.Region.ClientStack.Linden |
44 | { | 51 | { |
@@ -48,67 +55,74 @@ namespace OpenSim.Region.ClientStack.Linden | |||
48 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "WebFetchInvDescModule")] | 55 | [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "WebFetchInvDescModule")] |
49 | public class WebFetchInvDescModule : INonSharedRegionModule | 56 | public class WebFetchInvDescModule : INonSharedRegionModule |
50 | { | 57 | { |
51 | // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | 58 | class aPollRequest |
59 | { | ||
60 | public PollServiceInventoryEventArgs thepoll; | ||
61 | public UUID reqID; | ||
62 | public Hashtable request; | ||
63 | public ScenePresence presence; | ||
64 | public List<UUID> folders; | ||
65 | } | ||
66 | |||
67 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
52 | 68 | ||
53 | private Scene m_scene; | 69 | private Scene m_scene; |
54 | 70 | ||
55 | private IInventoryService m_InventoryService; | 71 | private IInventoryService m_InventoryService; |
56 | private ILibraryService m_LibraryService; | 72 | private ILibraryService m_LibraryService; |
57 | 73 | ||
58 | private bool m_Enabled; | 74 | private static WebFetchInvDescHandler m_webFetchHandler; |
59 | 75 | ||
60 | private string m_fetchInventoryDescendents2Url; | 76 | private Dictionary<UUID, string> m_capsDict = new Dictionary<UUID, string>(); |
61 | private string m_webFetchInventoryDescendentsUrl; | 77 | private static Thread[] m_workerThreads = null; |
62 | 78 | ||
63 | private WebFetchInvDescHandler m_webFetchHandler; | 79 | private static DoubleQueue<aPollRequest> m_queue = |
80 | new DoubleQueue<aPollRequest>(); | ||
64 | 81 | ||
65 | #region ISharedRegionModule Members | 82 | #region ISharedRegionModule Members |
66 | 83 | ||
67 | public void Initialise(IConfigSource source) | 84 | public void Initialise(IConfigSource source) |
68 | { | 85 | { |
69 | IConfig config = source.Configs["ClientStack.LindenCaps"]; | ||
70 | if (config == null) | ||
71 | return; | ||
72 | |||
73 | m_fetchInventoryDescendents2Url = config.GetString("Cap_FetchInventoryDescendents2", string.Empty); | ||
74 | m_webFetchInventoryDescendentsUrl = config.GetString("Cap_WebFetchInventoryDescendents", string.Empty); | ||
75 | |||
76 | if (m_fetchInventoryDescendents2Url != string.Empty || m_webFetchInventoryDescendentsUrl != string.Empty) | ||
77 | { | ||
78 | m_Enabled = true; | ||
79 | } | ||
80 | } | 86 | } |
81 | 87 | ||
82 | public void AddRegion(Scene s) | 88 | public void AddRegion(Scene s) |
83 | { | 89 | { |
84 | if (!m_Enabled) | ||
85 | return; | ||
86 | |||
87 | m_scene = s; | 90 | m_scene = s; |
88 | } | 91 | } |
89 | 92 | ||
90 | public void RemoveRegion(Scene s) | 93 | public void RemoveRegion(Scene s) |
91 | { | 94 | { |
92 | if (!m_Enabled) | ||
93 | return; | ||
94 | |||
95 | m_scene.EventManager.OnRegisterCaps -= RegisterCaps; | 95 | m_scene.EventManager.OnRegisterCaps -= RegisterCaps; |
96 | m_scene.EventManager.OnDeregisterCaps -= DeregisterCaps; | ||
96 | m_scene = null; | 97 | m_scene = null; |
97 | } | 98 | } |
98 | 99 | ||
99 | public void RegionLoaded(Scene s) | 100 | public void RegionLoaded(Scene s) |
100 | { | 101 | { |
101 | if (!m_Enabled) | ||
102 | return; | ||
103 | |||
104 | m_InventoryService = m_scene.InventoryService; | 102 | m_InventoryService = m_scene.InventoryService; |
105 | m_LibraryService = m_scene.LibraryService; | 103 | m_LibraryService = m_scene.LibraryService; |
106 | 104 | ||
107 | // We'll reuse the same handler for all requests. | 105 | // We'll reuse the same handler for all requests. |
108 | if (m_fetchInventoryDescendents2Url == "localhost" || m_webFetchInventoryDescendentsUrl == "localhost") | 106 | m_webFetchHandler = new WebFetchInvDescHandler(m_InventoryService, m_LibraryService); |
109 | m_webFetchHandler = new WebFetchInvDescHandler(m_InventoryService, m_LibraryService); | ||
110 | 107 | ||
111 | m_scene.EventManager.OnRegisterCaps += RegisterCaps; | 108 | m_scene.EventManager.OnRegisterCaps += RegisterCaps; |
109 | m_scene.EventManager.OnDeregisterCaps += DeregisterCaps; | ||
110 | |||
111 | if (m_workerThreads == null) | ||
112 | { | ||
113 | m_workerThreads = new Thread[2]; | ||
114 | |||
115 | for (uint i = 0; i < 2; i++) | ||
116 | { | ||
117 | m_workerThreads[i] = Watchdog.StartThread(DoInventoryRequests, | ||
118 | String.Format("InventoryWorkerThread{0}", i), | ||
119 | ThreadPriority.Normal, | ||
120 | false, | ||
121 | true, | ||
122 | null, | ||
123 | int.MaxValue); | ||
124 | } | ||
125 | } | ||
112 | } | 126 | } |
113 | 127 | ||
114 | public void PostInitialise() | 128 | public void PostInitialise() |
@@ -126,43 +140,188 @@ namespace OpenSim.Region.ClientStack.Linden | |||
126 | 140 | ||
127 | #endregion | 141 | #endregion |
128 | 142 | ||
129 | private void RegisterCaps(UUID agentID, Caps caps) | 143 | ~WebFetchInvDescModule() |
130 | { | 144 | { |
131 | if (m_webFetchInventoryDescendentsUrl != "") | 145 | foreach (Thread t in m_workerThreads) |
132 | RegisterFetchCap(agentID, caps, "WebFetchInventoryDescendents", m_webFetchInventoryDescendentsUrl); | 146 | Watchdog.AbortThread(t.ManagedThreadId); |
133 | |||
134 | if (m_fetchInventoryDescendents2Url != "") | ||
135 | RegisterFetchCap(agentID, caps, "FetchInventoryDescendents2", m_fetchInventoryDescendents2Url); | ||
136 | } | 147 | } |
137 | 148 | ||
138 | private void RegisterFetchCap(UUID agentID, Caps caps, string capName, string url) | 149 | private class PollServiceInventoryEventArgs : PollServiceEventArgs |
139 | { | 150 | { |
140 | string capUrl; | 151 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
152 | |||
153 | private Dictionary<UUID, Hashtable> responses = | ||
154 | new Dictionary<UUID, Hashtable>(); | ||
155 | |||
156 | private Scene m_scene; | ||
141 | 157 | ||
142 | if (url == "localhost") | 158 | public PollServiceInventoryEventArgs(Scene scene, UUID pId) : |
159 | base(null, null, null, null, pId, int.MaxValue) | ||
143 | { | 160 | { |
144 | capUrl = "/CAPS/" + UUID.Random(); | 161 | m_scene = scene; |
162 | |||
163 | HasEvents = (x, y) => { lock (responses) return responses.ContainsKey(x); }; | ||
164 | GetEvents = (x, y) => | ||
165 | { | ||
166 | lock (responses) | ||
167 | { | ||
168 | try | ||
169 | { | ||
170 | return responses[x]; | ||
171 | } | ||
172 | finally | ||
173 | { | ||
174 | responses.Remove(x); | ||
175 | } | ||
176 | } | ||
177 | }; | ||
178 | |||
179 | Request = (x, y) => | ||
180 | { | ||
181 | ScenePresence sp = m_scene.GetScenePresence(Id); | ||
182 | if (sp == null) | ||
183 | { | ||
184 | m_log.ErrorFormat("[INVENTORY]: Unable to find ScenePresence for {0}", Id); | ||
185 | return; | ||
186 | } | ||
187 | |||
188 | aPollRequest reqinfo = new aPollRequest(); | ||
189 | reqinfo.thepoll = this; | ||
190 | reqinfo.reqID = x; | ||
191 | reqinfo.request = y; | ||
192 | reqinfo.presence = sp; | ||
193 | reqinfo.folders = new List<UUID>(); | ||
194 | |||
195 | // Decode the request here | ||
196 | string request = y["body"].ToString(); | ||
197 | |||
198 | request = request.Replace("<string>00000000-0000-0000-0000-000000000000</string>", "<uuid>00000000-0000-0000-0000-000000000000</uuid>"); | ||
199 | |||
200 | request = request.Replace("<key>fetch_folders</key><integer>0</integer>", "<key>fetch_folders</key><boolean>0</boolean>"); | ||
201 | request = request.Replace("<key>fetch_folders</key><integer>1</integer>", "<key>fetch_folders</key><boolean>1</boolean>"); | ||
202 | |||
203 | Hashtable hash = new Hashtable(); | ||
204 | try | ||
205 | { | ||
206 | hash = (Hashtable)LLSD.LLSDDeserialize(Utils.StringToBytes(request)); | ||
207 | } | ||
208 | catch (LLSD.LLSDParseException e) | ||
209 | { | ||
210 | m_log.ErrorFormat("[INVENTORY]: Fetch error: {0}{1}" + e.Message, e.StackTrace); | ||
211 | m_log.Error("Request: " + request); | ||
212 | return; | ||
213 | } | ||
214 | |||
215 | ArrayList foldersrequested = (ArrayList)hash["folders"]; | ||
216 | |||
217 | bool highPriority = false; | ||
218 | |||
219 | for (int i = 0; i < foldersrequested.Count; i++) | ||
220 | { | ||
221 | Hashtable inventoryhash = (Hashtable)foldersrequested[i]; | ||
222 | string folder = inventoryhash["folder_id"].ToString(); | ||
223 | UUID folderID; | ||
224 | if (UUID.TryParse(folder, out folderID)) | ||
225 | { | ||
226 | if (!reqinfo.folders.Contains(folderID)) | ||
227 | { | ||
228 | if (sp.COF != UUID.Zero && sp.COF == folderID) | ||
229 | highPriority = true; | ||
230 | reqinfo.folders.Add(folderID); | ||
231 | } | ||
232 | } | ||
233 | } | ||
234 | |||
235 | if (highPriority) | ||
236 | m_queue.EnqueueHigh(reqinfo); | ||
237 | else | ||
238 | m_queue.EnqueueLow(reqinfo); | ||
239 | }; | ||
240 | |||
241 | NoEvents = (x, y) => | ||
242 | { | ||
243 | /* | ||
244 | lock (requests) | ||
245 | { | ||
246 | Hashtable request = requests.Find(id => id["RequestID"].ToString() == x.ToString()); | ||
247 | requests.Remove(request); | ||
248 | } | ||
249 | */ | ||
250 | Hashtable response = new Hashtable(); | ||
251 | |||
252 | response["int_response_code"] = 500; | ||
253 | response["str_response_string"] = "Script timeout"; | ||
254 | response["content_type"] = "text/plain"; | ||
255 | response["keepalive"] = false; | ||
256 | response["reusecontext"] = false; | ||
257 | |||
258 | return response; | ||
259 | }; | ||
260 | } | ||
145 | 261 | ||
146 | IRequestHandler reqHandler | 262 | public void Process(aPollRequest requestinfo) |
147 | = new RestStreamHandler( | 263 | { |
148 | "POST", | 264 | UUID requestID = requestinfo.reqID; |
149 | capUrl, | 265 | |
150 | m_webFetchHandler.FetchInventoryDescendentsRequest, | 266 | Hashtable response = new Hashtable(); |
151 | "FetchInventoryDescendents2", | 267 | |
152 | agentID.ToString()); | 268 | response["int_response_code"] = 200; |
269 | response["content_type"] = "text/plain"; | ||
270 | response["keepalive"] = false; | ||
271 | response["reusecontext"] = false; | ||
153 | 272 | ||
154 | caps.RegisterHandler(capName, reqHandler); | 273 | response["str_response_string"] = m_webFetchHandler.FetchInventoryDescendentsRequest( |
274 | requestinfo.request["body"].ToString(), String.Empty, String.Empty, null, null); | ||
275 | |||
276 | lock (responses) | ||
277 | responses[requestID] = response; | ||
155 | } | 278 | } |
156 | else | 279 | } |
280 | |||
281 | private void RegisterCaps(UUID agentID, Caps caps) | ||
282 | { | ||
283 | string capUrl = "/CAPS/" + UUID.Random() + "/"; | ||
284 | |||
285 | // Register this as a poll service | ||
286 | PollServiceInventoryEventArgs args = new PollServiceInventoryEventArgs(m_scene, agentID); | ||
287 | |||
288 | args.Type = PollServiceEventArgs.EventType.Inventory; | ||
289 | MainServer.Instance.AddPollServiceHTTPHandler(capUrl, args); | ||
290 | |||
291 | string hostName = m_scene.RegionInfo.ExternalHostName; | ||
292 | uint port = (MainServer.Instance == null) ? 0 : MainServer.Instance.Port; | ||
293 | string protocol = "http"; | ||
294 | |||
295 | if (MainServer.Instance.UseSSL) | ||
157 | { | 296 | { |
158 | capUrl = url; | 297 | hostName = MainServer.Instance.SSLCommonName; |
298 | port = MainServer.Instance.SSLPort; | ||
299 | protocol = "https"; | ||
300 | } | ||
301 | caps.RegisterHandler("FetchInventoryDescendents2", String.Format("{0}://{1}:{2}{3}", protocol, hostName, port, capUrl)); | ||
302 | |||
303 | m_capsDict[agentID] = capUrl; | ||
304 | } | ||
159 | 305 | ||
160 | caps.RegisterHandler(capName, capUrl); | 306 | private void DeregisterCaps(UUID agentID, Caps caps) |
307 | { | ||
308 | string capUrl; | ||
309 | |||
310 | if (m_capsDict.TryGetValue(agentID, out capUrl)) | ||
311 | { | ||
312 | MainServer.Instance.RemoveHTTPHandler("", capUrl); | ||
313 | m_capsDict.Remove(agentID); | ||
161 | } | 314 | } |
315 | } | ||
162 | 316 | ||
163 | // m_log.DebugFormat( | 317 | private void DoInventoryRequests() |
164 | // "[WEB FETCH INV DESC MODULE]: Registered capability {0} at {1} in region {2} for {3}", | 318 | { |
165 | // capName, capUrl, m_scene.RegionInfo.RegionName, agentID); | 319 | while (true) |
320 | { | ||
321 | aPollRequest poolreq = m_queue.Dequeue(); | ||
322 | |||
323 | poolreq.thepoll.Process(poolreq); | ||
324 | } | ||
166 | } | 325 | } |
167 | } | 326 | } |
168 | } \ No newline at end of file | 327 | } |