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