aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs')
-rw-r--r--OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs423
1 files changed, 389 insertions, 34 deletions
diff --git a/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs b/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs
index bb932f2..12cfa5d 100644
--- a/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs
+++ b/OpenSim/Region/ClientStack/Linden/Caps/GetTextureModule.cs
@@ -27,18 +27,13 @@
27 27
28using System; 28using System;
29using System.Collections; 29using System.Collections;
30using System.Collections.Specialized; 30using System.Collections.Generic;
31using System.Drawing;
32using System.Drawing.Imaging;
33using System.Reflection; 31using System.Reflection;
34using System.IO; 32using System.Threading;
35using System.Web;
36using log4net; 33using log4net;
37using Nini.Config; 34using Nini.Config;
38using Mono.Addins; 35using Mono.Addins;
39using OpenMetaverse; 36using OpenMetaverse;
40using OpenMetaverse.StructuredData;
41using OpenMetaverse.Imaging;
42using OpenSim.Framework; 37using OpenSim.Framework;
43using OpenSim.Framework.Servers; 38using OpenSim.Framework.Servers;
44using OpenSim.Framework.Servers.HttpServer; 39using OpenSim.Framework.Servers.HttpServer;
@@ -47,6 +42,7 @@ using OpenSim.Region.Framework.Scenes;
47using OpenSim.Services.Interfaces; 42using OpenSim.Services.Interfaces;
48using Caps = OpenSim.Framework.Capabilities.Caps; 43using Caps = OpenSim.Framework.Capabilities.Caps;
49using OpenSim.Capabilities.Handlers; 44using OpenSim.Capabilities.Handlers;
45using OpenSim.Framework.Monitoring;
50 46
51namespace OpenSim.Region.ClientStack.Linden 47namespace OpenSim.Region.ClientStack.Linden
52{ 48{
@@ -54,27 +50,54 @@ namespace OpenSim.Region.ClientStack.Linden
54 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "GetTextureModule")] 50 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "GetTextureModule")]
55 public class GetTextureModule : INonSharedRegionModule 51 public class GetTextureModule : INonSharedRegionModule
56 { 52 {
57// private static readonly ILog m_log = 53
58// LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 54 struct aPollRequest
59 55 {
56 public PollServiceTextureEventArgs thepoll;
57 public UUID reqID;
58 public Hashtable request;
59 public bool send503;
60 }
61
62 public class aPollResponse
63 {
64 public Hashtable response;
65 public int bytes;
66 }
67
68
69 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
70
60 private Scene m_scene; 71 private Scene m_scene;
61 private IAssetService m_assetService;
62 72
63 private bool m_Enabled = false; 73 private static GetTextureHandler m_getTextureHandler;
74
75 private IAssetService m_assetService = null;
76
77 private Dictionary<UUID, string> m_capsDict = new Dictionary<UUID, string>();
78 private static Thread[] m_workerThreads = null;
79
80 private string m_Url = "localhost";
81
82 private static OpenMetaverse.BlockingQueue<aPollRequest> m_queue =
83 new OpenMetaverse.BlockingQueue<aPollRequest>();
84
64 85
65 // TODO: Change this to a config option 86 // TODO: Change this to a config option
66 private string m_RedirectURL = null; 87 private string m_RedirectURL = null;
67 88
68 private string m_URL; 89 private Dictionary<UUID,PollServiceTextureEventArgs> m_pollservices = new Dictionary<UUID,PollServiceTextureEventArgs>();
90
69 91
70 #region ISharedRegionModule Members 92 #region ISharedRegionModule Members
71 93
72 public void Initialise(IConfigSource source) 94 public void Initialise(IConfigSource source)
73 { 95 {
74 IConfig config = source.Configs["ClientStack.LindenCaps"]; 96 IConfig config = source.Configs["ClientStack.LindenCaps"];
97
75 if (config == null) 98 if (config == null)
76 return; 99 return;
77 100/*
78 m_URL = config.GetString("Cap_GetTexture", string.Empty); 101 m_URL = config.GetString("Cap_GetTexture", string.Empty);
79 // Cap doesn't exist 102 // Cap doesn't exist
80 if (m_URL != string.Empty) 103 if (m_URL != string.Empty)
@@ -82,32 +105,98 @@ namespace OpenSim.Region.ClientStack.Linden
82 m_Enabled = true; 105 m_Enabled = true;
83 m_RedirectURL = config.GetString("GetTextureRedirectURL"); 106 m_RedirectURL = config.GetString("GetTextureRedirectURL");
84 } 107 }
108*/
109 m_Url = config.GetString("Cap_GetTexture", "localhost");
85 } 110 }
86 111
87 public void AddRegion(Scene s) 112 public void AddRegion(Scene s)
88 { 113 {
89 if (!m_Enabled)
90 return;
91
92 m_scene = s; 114 m_scene = s;
115 m_assetService = s.AssetService;
93 } 116 }
94 117
95 public void RemoveRegion(Scene s) 118 public void RemoveRegion(Scene s)
96 { 119 {
97 if (!m_Enabled)
98 return;
99
100 m_scene.EventManager.OnRegisterCaps -= RegisterCaps; 120 m_scene.EventManager.OnRegisterCaps -= RegisterCaps;
121 m_scene.EventManager.OnDeregisterCaps -= DeregisterCaps;
122 m_scene.EventManager.OnThrottleUpdate -= ThrottleUpdate;
101 m_scene = null; 123 m_scene = null;
102 } 124 }
103 125
104 public void RegionLoaded(Scene s) 126 public void RegionLoaded(Scene s)
105 { 127 {
106 if (!m_Enabled) 128 // We'll reuse the same handler for all requests.
107 return; 129 m_getTextureHandler = new GetTextureHandler(m_assetService);
108 130
109 m_assetService = m_scene.RequestModuleInterface<IAssetService>();
110 m_scene.EventManager.OnRegisterCaps += RegisterCaps; 131 m_scene.EventManager.OnRegisterCaps += RegisterCaps;
132 m_scene.EventManager.OnDeregisterCaps += DeregisterCaps;
133 m_scene.EventManager.OnThrottleUpdate += ThrottleUpdate;
134
135 if (m_workerThreads == null)
136 {
137 m_workerThreads = new Thread[2];
138
139 for (uint i = 0; i < 2; i++)
140 {
141 m_workerThreads[i] = WorkManager.StartThread(DoTextureRequests,
142 String.Format("TextureWorkerThread{0}", i),
143 ThreadPriority.Normal,
144 false,
145 false,
146 null,
147 int.MaxValue);
148 }
149 }
150 }
151 private int ExtractImageThrottle(byte[] pthrottles)
152 {
153
154 byte[] adjData;
155 int pos = 0;
156
157 if (!BitConverter.IsLittleEndian)
158 {
159 byte[] newData = new byte[7 * 4];
160 Buffer.BlockCopy(pthrottles, 0, newData, 0, 7 * 4);
161
162 for (int i = 0; i < 7; i++)
163 Array.Reverse(newData, i * 4, 4);
164
165 adjData = newData;
166 }
167 else
168 {
169 adjData = pthrottles;
170 }
171
172 // 0.125f converts from bits to bytes
173 //int resend = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f);
174 //pos += 4;
175 // int land = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f);
176 //pos += 4;
177 // int wind = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f);
178 // pos += 4;
179 // int cloud = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f);
180 // pos += 4;
181 // int task = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f);
182 // pos += 4;
183 pos = pos + 20;
184 int texture = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f); //pos += 4;
185 //int asset = (int)(BitConverter.ToSingle(adjData, pos) * 0.125f);
186 return texture;
187 }
188
189 // Now we know when the throttle is changed by the client in the case of a root agent or by a neighbor region in the case of a child agent.
190 public void ThrottleUpdate(ScenePresence p)
191 {
192 byte[] throttles = p.ControllingClient.GetThrottlesPacked(1);
193 UUID user = p.UUID;
194 int imagethrottle = ExtractImageThrottle(throttles);
195 PollServiceTextureEventArgs args;
196 if (m_pollservices.TryGetValue(user,out args))
197 {
198 args.UpdateThrottle(imagethrottle);
199 }
111 } 200 }
112 201
113 public void PostInitialise() 202 public void PostInitialise()
@@ -125,28 +214,294 @@ namespace OpenSim.Region.ClientStack.Linden
125 214
126 #endregion 215 #endregion
127 216
128 public void RegisterCaps(UUID agentID, Caps caps) 217 ~GetTextureModule()
218 {
219 foreach (Thread t in m_workerThreads)
220 Watchdog.AbortThread(t.ManagedThreadId);
221
222 }
223
224 private class PollServiceTextureEventArgs : PollServiceEventArgs
129 { 225 {
130 UUID capID = UUID.Random(); 226 private List<Hashtable> requests =
227 new List<Hashtable>();
228 private Dictionary<UUID, aPollResponse> responses =
229 new Dictionary<UUID, aPollResponse>();
131 230
132 //caps.RegisterHandler("GetTexture", new StreamHandler("GET", "/CAPS/" + capID, ProcessGetTexture)); 231 private Scene m_scene;
133 if (m_URL == "localhost") 232 private CapsDataThrottler m_throttler = new CapsDataThrottler(100000, 1400000,10000);
233 public PollServiceTextureEventArgs(UUID pId, Scene scene) :
234 base(null, "", null, null, null, pId, int.MaxValue)
134 { 235 {
135// m_log.DebugFormat("[GETTEXTURE]: /CAPS/{0} in region {1}", capID, m_scene.RegionInfo.RegionName); 236 m_scene = scene;
136 caps.RegisterHandler( 237 // x is request id, y is userid
137 "GetTexture", 238 HasEvents = (x, y) =>
138 new GetTextureHandler("/CAPS/" + capID + "/", m_assetService, "GetTexture", agentID.ToString(), m_RedirectURL)); 239 {
240 lock (responses)
241 {
242 bool ret = m_throttler.hasEvents(x, responses);
243 m_throttler.ProcessTime();
244 return ret;
245
246 }
247 };
248 GetEvents = (x, y) =>
249 {
250 lock (responses)
251 {
252 try
253 {
254 return responses[x].response;
255 }
256 finally
257 {
258 responses.Remove(x);
259 }
260 }
261 };
262 // x is request id, y is request data hashtable
263 Request = (x, y) =>
264 {
265 aPollRequest reqinfo = new aPollRequest();
266 reqinfo.thepoll = this;
267 reqinfo.reqID = x;
268 reqinfo.request = y;
269 reqinfo.send503 = false;
270
271 lock (responses)
272 {
273 if (responses.Count > 0)
274 {
275 if (m_queue.Count >= 4)
276 {
277 // Never allow more than 4 fetches to wait
278 reqinfo.send503 = true;
279 }
280 }
281 }
282 m_queue.Enqueue(reqinfo);
283 };
284
285 // this should never happen except possible on shutdown
286 NoEvents = (x, y) =>
287 {
288/*
289 lock (requests)
290 {
291 Hashtable request = requests.Find(id => id["RequestID"].ToString() == x.ToString());
292 requests.Remove(request);
293 }
294*/
295 Hashtable response = new Hashtable();
296
297 response["int_response_code"] = 500;
298 response["str_response_string"] = "Script timeout";
299 response["content_type"] = "text/plain";
300 response["keepalive"] = false;
301 response["reusecontext"] = false;
302
303 return response;
304 };
305 }
306
307 public void Process(aPollRequest requestinfo)
308 {
309 Hashtable response;
310
311 UUID requestID = requestinfo.reqID;
312
313 if (requestinfo.send503)
314 {
315 response = new Hashtable();
316
317
318 response["int_response_code"] = 503;
319 response["str_response_string"] = "Throttled";
320 response["content_type"] = "text/plain";
321 response["keepalive"] = false;
322 response["reusecontext"] = false;
323
324 Hashtable headers = new Hashtable();
325 headers["Retry-After"] = 30;
326 response["headers"] = headers;
327
328 lock (responses)
329 responses[requestID] = new aPollResponse() {bytes = 0, response = response};
330
331 return;
332 }
333
334 // If the avatar is gone, don't bother to get the texture
335 if (m_scene.GetScenePresence(Id) == null)
336 {
337 response = new Hashtable();
338
339 response["int_response_code"] = 500;
340 response["str_response_string"] = "Script timeout";
341 response["content_type"] = "text/plain";
342 response["keepalive"] = false;
343 response["reusecontext"] = false;
344
345 lock (responses)
346 responses[requestID] = new aPollResponse() {bytes = 0, response = response};
347
348 return;
349 }
350
351 response = m_getTextureHandler.Handle(requestinfo.request);
352 lock (responses)
353 {
354 responses[requestID] = new aPollResponse()
355 {
356 bytes = (int) response["int_bytes"],
357 response = response
358 };
359
360 }
361 m_throttler.ProcessTime();
362 }
363
364 internal void UpdateThrottle(int pimagethrottle)
365 {
366 m_throttler.ThrottleBytes = pimagethrottle;
367 }
368 }
369
370 private void RegisterCaps(UUID agentID, Caps caps)
371 {
372 if (m_Url == "localhost")
373 {
374 string capUrl = "/CAPS/" + UUID.Random() + "/";
375
376 // Register this as a poll service
377 PollServiceTextureEventArgs args = new PollServiceTextureEventArgs(agentID, m_scene);
378
379 args.Type = PollServiceEventArgs.EventType.Texture;
380 MainServer.Instance.AddPollServiceHTTPHandler(capUrl, args);
381
382 string hostName = m_scene.RegionInfo.ExternalHostName;
383 uint port = (MainServer.Instance == null) ? 0 : MainServer.Instance.Port;
384 string protocol = "http";
385
386 if (MainServer.Instance.UseSSL)
387 {
388 hostName = MainServer.Instance.SSLCommonName;
389 port = MainServer.Instance.SSLPort;
390 protocol = "https";
391 }
392 m_pollservices[agentID] = args;
393 m_capsDict[agentID] = capUrl;
139 } 394 }
140 else 395 else
141 { 396 {
142// m_log.DebugFormat("[GETTEXTURE]: {0} in region {1}", m_URL, m_scene.RegionInfo.RegionName);
143 IExternalCapsModule handler = m_scene.RequestModuleInterface<IExternalCapsModule>(); 397 IExternalCapsModule handler = m_scene.RequestModuleInterface<IExternalCapsModule>();
144 if (handler != null) 398 if (handler != null)
145 handler.RegisterExternalUserCapsHandler(agentID,caps,"GetTexture", m_URL); 399 handler.RegisterExternalUserCapsHandler(agentID, caps, "GetTexture", m_Url);
146 else 400 else
147 caps.RegisterHandler("GetTexture", m_URL); 401 caps.RegisterHandler("GetTexture", m_Url);
402 }
403 }
404
405 private void DeregisterCaps(UUID agentID, Caps caps)
406 {
407 PollServiceTextureEventArgs args;
408
409 MainServer.Instance.RemoveHTTPHandler("", m_Url);
410 m_capsDict.Remove(agentID);
411
412 if (m_pollservices.TryGetValue(agentID, out args))
413 {
414 m_pollservices.Remove(agentID);
148 } 415 }
149 } 416 }
150 417
418 private void DoTextureRequests()
419 {
420 while (true)
421 {
422 aPollRequest poolreq = m_queue.Dequeue();
423
424 poolreq.thepoll.Process(poolreq);
425 }
426 }
427 internal sealed class CapsDataThrottler
428 {
429
430 private volatile int currenttime = 0;
431 private volatile int lastTimeElapsed = 0;
432 private volatile int BytesSent = 0;
433 private int oversizedImages = 0;
434 public CapsDataThrottler(int pBytes, int max, int min)
435 {
436 ThrottleBytes = pBytes;
437 lastTimeElapsed = Util.EnvironmentTickCount();
438 }
439 public bool hasEvents(UUID key, Dictionary<UUID, GetTextureModule.aPollResponse> responses)
440 {
441 PassTime();
442 // Note, this is called IN LOCK
443 bool haskey = responses.ContainsKey(key);
444 if (!haskey)
445 {
446 return false;
447 }
448 GetTextureModule.aPollResponse response;
449 if (responses.TryGetValue(key, out response))
450 {
451 // This is any error response
452 if (response.bytes == 0)
453 return true;
454
455 // Normal
456 if (BytesSent + response.bytes <= ThrottleBytes)
457 {
458 BytesSent += response.bytes;
459 //TimeBasedAction timeBasedAction = new TimeBasedAction { byteRemoval = response.bytes, requestId = key, timeMS = currenttime + 1000, unlockyn = false };
460 //m_actions.Add(timeBasedAction);
461 return true;
462 }
463 // Big textures
464 else if (response.bytes > ThrottleBytes && oversizedImages <= ((ThrottleBytes % 50000) + 1))
465 {
466 Interlocked.Increment(ref oversizedImages);
467 BytesSent += response.bytes;
468 //TimeBasedAction timeBasedAction = new TimeBasedAction { byteRemoval = response.bytes, requestId = key, timeMS = currenttime + (((response.bytes % ThrottleBytes)+1)*1000) , unlockyn = false };
469 //m_actions.Add(timeBasedAction);
470 return true;
471 }
472 else
473 {
474 return false;
475 }
476 }
477
478 return haskey;
479 }
480
481 public void ProcessTime()
482 {
483 PassTime();
484 }
485
486 private void PassTime()
487 {
488 currenttime = Util.EnvironmentTickCount();
489 int timeElapsed = Util.EnvironmentTickCountSubtract(currenttime, lastTimeElapsed);
490 //processTimeBasedActions(responses);
491 if (Util.EnvironmentTickCountSubtract(currenttime, timeElapsed) >= 1000)
492 {
493 lastTimeElapsed = Util.EnvironmentTickCount();
494 BytesSent -= ThrottleBytes;
495 if (BytesSent < 0) BytesSent = 0;
496 if (BytesSent < ThrottleBytes)
497 {
498 oversizedImages = 0;
499 }
500 }
501 }
502 public int ThrottleBytes;
503 }
151 } 504 }
505
506
152} 507}