aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules/Agent/TextureDownload/TextureDownloadModule.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/CoreModules/Agent/TextureDownload/TextureDownloadModule.cs')
-rw-r--r--OpenSim/Region/CoreModules/Agent/TextureDownload/TextureDownloadModule.cs295
1 files changed, 295 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/Agent/TextureDownload/TextureDownloadModule.cs b/OpenSim/Region/CoreModules/Agent/TextureDownload/TextureDownloadModule.cs
new file mode 100644
index 0000000..107855d
--- /dev/null
+++ b/OpenSim/Region/CoreModules/Agent/TextureDownload/TextureDownloadModule.cs
@@ -0,0 +1,295 @@
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 OpenSim 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
28using System;
29using System.Collections.Generic;
30using System.Threading;
31using OpenMetaverse;
32using Nini.Config;
33using OpenSim.Framework;
34using OpenSim.Region.Framework.Interfaces;
35using OpenSim.Region.Framework.Scenes;
36using OpenSim.Framework.Communications.Cache;
37using BlockingQueue = OpenSim.Framework.BlockingQueue<OpenSim.Region.Framework.Interfaces.ITextureSender>;
38
39namespace OpenSim.Region.CoreModules.Agent.TextureDownload
40{
41 public class TextureDownloadModule : IRegionModule
42 {
43 private static readonly log4net.ILog m_log
44 = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
45
46 /// <summary>
47 /// There is one queue for all textures waiting to be sent, regardless of the requesting user.
48 /// </summary>
49 private readonly OpenSim.Framework.BlockingQueue<ITextureSender> m_queueSenders
50 = new OpenSim.Framework.BlockingQueue<ITextureSender>();
51
52 /// <summary>
53 /// Each user has their own texture download service.
54 /// </summary>
55 private readonly Dictionary<UUID, UserTextureDownloadService> m_userTextureServices =
56 new Dictionary<UUID, UserTextureDownloadService>();
57
58 private Scene m_scene;
59 private List<Scene> m_scenes = new List<Scene>();
60
61 private Thread m_thread;
62
63 public TextureDownloadModule()
64 {
65 }
66
67 #region IRegionModule Members
68
69 public void Initialise(Scene scene, IConfigSource config)
70 {
71 if (m_scene == null)
72 {
73 //Console.WriteLine("Creating Texture download module");
74 m_scene = scene;
75 m_thread = new Thread(new ThreadStart(ProcessTextureSenders));
76 m_thread.Name = "ProcessTextureSenderThread";
77 m_thread.IsBackground = true;
78 m_thread.Start();
79 ThreadTracker.Add(m_thread);
80 }
81
82 if (!m_scenes.Contains(scene))
83 {
84 m_scenes.Add(scene);
85 m_scene = scene;
86 m_scene.EventManager.OnNewClient += NewClient;
87 m_scene.EventManager.OnRemovePresence += EventManager_OnRemovePresence;
88 }
89 }
90
91 public void PostInitialise()
92 {
93 }
94
95 public void Close()
96 {
97 }
98
99 public string Name
100 {
101 get { return "TextureDownloadModule"; }
102 }
103
104 public bool IsSharedModule
105 {
106 get { return false; }
107 }
108
109 #endregion
110
111 /// <summary>
112 /// Cleanup the texture service related objects for the removed presence.
113 /// </summary>
114 /// <param name="agentId"> </param>
115 private void EventManager_OnRemovePresence(UUID agentId)
116 {
117 UserTextureDownloadService textureService;
118
119 lock (m_userTextureServices)
120 {
121 if (m_userTextureServices.TryGetValue(agentId, out textureService))
122 {
123 textureService.Close();
124 //m_log.DebugFormat("[TEXTURE MODULE]: Removing UserTextureServices from {0}", m_scene.RegionInfo.RegionName);
125 m_userTextureServices.Remove(agentId);
126 }
127 }
128 }
129
130 public void NewClient(IClientAPI client)
131 {
132 UserTextureDownloadService textureService;
133
134 lock (m_userTextureServices)
135 {
136 if (m_userTextureServices.TryGetValue(client.AgentId, out textureService))
137 {
138 textureService.Close();
139 //m_log.DebugFormat("[TEXTURE MODULE]: Removing outdated UserTextureServices from {0}", m_scene.RegionInfo.RegionName);
140 m_userTextureServices.Remove(client.AgentId);
141 }
142 m_userTextureServices.Add(client.AgentId, new UserTextureDownloadService(client, m_scene, m_queueSenders));
143 }
144
145 client.OnRequestTexture += TextureRequest;
146 }
147
148 /// I'm commenting this out, and replacing it with the implementation below, which
149 /// may return a null value. This is necessary for avoiding race conditions
150 /// recreating UserTextureServices for clients that have just been closed.
151 /// That behavior of always returning a UserTextureServices was causing the
152 /// A-B-A problem (mantis #2855).
153 ///
154 ///// <summary>
155 ///// Does this user have a registered texture download service?
156 ///// </summary>
157 ///// <param name="userID"></param>
158 ///// <param name="textureService"></param>
159 ///// <returns>Always returns true, since a service is created if one does not already exist</returns>
160 //private bool TryGetUserTextureService(
161 // IClientAPI client, out UserTextureDownloadService textureService)
162 //{
163 // lock (m_userTextureServices)
164 // {
165 // if (m_userTextureServices.TryGetValue(client.AgentId, out textureService))
166 // {
167 // //m_log.DebugFormat("[TEXTURE MODULE]: Found existing UserTextureServices in ", m_scene.RegionInfo.RegionName);
168 // return true;
169 // }
170
171 // m_log.DebugFormat("[TEXTURE MODULE]: Creating new UserTextureServices in ", m_scene.RegionInfo.RegionName);
172 // textureService = new UserTextureDownloadService(client, m_scene, m_queueSenders);
173 // m_userTextureServices.Add(client.AgentId, textureService);
174
175 // return true;
176 // }
177 //}
178
179 /// <summary>
180 /// Does this user have a registered texture download service?
181 /// </summary>
182 /// <param name="userID"></param>
183 /// <param name="textureService"></param>
184 /// <returns>A UserTextureDownloadService or null in the output parameter, and true or false accordingly.</returns>
185 private bool TryGetUserTextureService(IClientAPI client, out UserTextureDownloadService textureService)
186 {
187 lock (m_userTextureServices)
188 {
189 if (m_userTextureServices.TryGetValue(client.AgentId, out textureService))
190 {
191 //m_log.DebugFormat("[TEXTURE MODULE]: Found existing UserTextureServices in ", m_scene.RegionInfo.RegionName);
192 return true;
193 }
194
195 textureService = null;
196 return false;
197 }
198 }
199
200 /// <summary>
201 /// Start the process of requesting a given texture.
202 /// </summary>
203 /// <param name="sender"> </param>
204 /// <param name="e"></param>
205 public void TextureRequest(Object sender, TextureRequestArgs e)
206 {
207 IClientAPI client = (IClientAPI)sender;
208
209 if (e.Priority == 1016001f) // Preview
210 {
211 if (client.Scene is Scene)
212 {
213 Scene scene = (Scene)client.Scene;
214
215 CachedUserInfo profile = scene.CommsManager.UserProfileCacheService.GetUserDetails(client.AgentId);
216 if (profile == null) // Deny unknown user
217 return;
218
219 if (profile.RootFolder == null) // Deny no inventory
220 return;
221
222 if (profile.UserProfile.GodLevel < 200 && profile.RootFolder.FindAsset(e.RequestedAssetID) == null) // Deny if not owned
223 return;
224
225 m_log.Debug("Texture preview");
226 }
227 }
228
229 UserTextureDownloadService textureService;
230
231 if (TryGetUserTextureService(client, out textureService))
232 {
233 textureService.HandleTextureRequest(e);
234 }
235 }
236
237 /// <summary>
238 /// Entry point for the thread dedicated to processing the texture queue.
239 /// </summary>
240 public void ProcessTextureSenders()
241 {
242 ITextureSender sender = null;
243
244 try
245 {
246 while (true)
247 {
248 sender = m_queueSenders.Dequeue();
249
250 if (sender.Cancel)
251 {
252 TextureSent(sender);
253
254 sender.Cancel = false;
255 }
256 else
257 {
258 bool finished = sender.SendTexturePacket();
259 if (finished)
260 {
261 TextureSent(sender);
262 }
263 else
264 {
265 m_queueSenders.Enqueue(sender);
266 }
267 }
268
269 // Make sure that any sender we currently have can get garbage collected
270 sender = null;
271
272 //m_log.InfoFormat("[TEXTURE] Texture sender queue size: {0}", m_queueSenders.Count());
273 }
274 }
275 catch (Exception e)
276 {
277 // TODO: Let users in the sim and those entering it and possibly an external watchdog know what has happened
278 m_log.ErrorFormat(
279 "[TEXTURE]: Texture send thread terminating with exception. PLEASE REBOOT YOUR SIM - TEXTURES WILL NOT BE AVAILABLE UNTIL YOU DO. Exception is {0}",
280 e);
281 }
282 }
283
284 /// <summary>
285 /// Called when the texture has finished sending.
286 /// </summary>
287 /// <param name="sender"></param>
288 private void TextureSent(ITextureSender sender)
289 {
290 sender.Sending = false;
291 //m_log.DebugFormat("[TEXTURE]: Removing download stat for {0}", sender.assetID);
292 m_scene.AddPendingDownloads(-1);
293 }
294 }
295}