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