diff options
Diffstat (limited to 'OpenSim/Region/Environment/Modules/Agent/TextureDownload/UserTextureDownloadService.cs')
-rw-r--r-- | OpenSim/Region/Environment/Modules/Agent/TextureDownload/UserTextureDownloadService.cs | 252 |
1 files changed, 252 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/Agent/TextureDownload/UserTextureDownloadService.cs b/OpenSim/Region/Environment/Modules/Agent/TextureDownload/UserTextureDownloadService.cs new file mode 100644 index 0000000..08a22d6 --- /dev/null +++ b/OpenSim/Region/Environment/Modules/Agent/TextureDownload/UserTextureDownloadService.cs | |||
@@ -0,0 +1,252 @@ | |||
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 | |||
28 | using System.Collections.Generic; | ||
29 | using System.Reflection; | ||
30 | using libsecondlife; | ||
31 | using log4net; | ||
32 | using OpenSim.Framework; | ||
33 | using OpenSim.Framework.Communications.Limit; | ||
34 | using OpenSim.Region.Environment.Interfaces; | ||
35 | using OpenSim.Region.Environment.Modules.Agent.TextureDownload; | ||
36 | using OpenSim.Region.Environment.Modules.Agent.TextureSender; | ||
37 | using OpenSim.Region.Environment.Scenes; | ||
38 | |||
39 | namespace OpenSim.Region.Environment.Modules.Agent.TextureDownload | ||
40 | { | ||
41 | /// <summary> | ||
42 | /// This module sets up texture senders in response to client texture requests, and places them on a | ||
43 | /// processing queue once those senders have the appropriate data (i.e. a texture retrieved from the | ||
44 | /// asset cache). | ||
45 | /// </summary> | ||
46 | public class UserTextureDownloadService | ||
47 | { | ||
48 | private static readonly ILog m_log | ||
49 | = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
50 | |||
51 | /// <summary> | ||
52 | /// We will allow the client to request the same texture n times before dropping further requests | ||
53 | /// | ||
54 | /// This number includes repeated requests for the same texture at different resolutions (which we don't | ||
55 | /// currently handle properly as far as I know). However, this situation should be handled in a more | ||
56 | /// sophisticated way. | ||
57 | /// </summary> | ||
58 | private static readonly int MAX_ALLOWED_TEXTURE_REQUESTS = 5; | ||
59 | |||
60 | /// <summary> | ||
61 | /// We're going to limit requests for the same missing texture. | ||
62 | /// XXX This is really a temporary solution to deal with the situation where a client continually requests | ||
63 | /// the same missing textures | ||
64 | /// </summary> | ||
65 | private readonly IRequestLimitStrategy<LLUUID> missingTextureLimitStrategy | ||
66 | = new RepeatLimitStrategy<LLUUID>(MAX_ALLOWED_TEXTURE_REQUESTS); | ||
67 | |||
68 | /// <summary> | ||
69 | /// XXX Also going to limit requests for found textures. | ||
70 | /// </summary> | ||
71 | private readonly IRequestLimitStrategy<LLUUID> foundTextureLimitStrategy | ||
72 | = new RepeatLimitStrategy<LLUUID>(MAX_ALLOWED_TEXTURE_REQUESTS); | ||
73 | |||
74 | /// <summary> | ||
75 | /// Holds texture senders before they have received the appropriate texture from the asset cache. | ||
76 | /// </summary> | ||
77 | private readonly Dictionary<LLUUID, TextureSender.TextureSender> m_textureSenders = new Dictionary<LLUUID, TextureSender.TextureSender>(); | ||
78 | |||
79 | /// <summary> | ||
80 | /// Texture Senders are placed in this queue once they have received their texture from the asset | ||
81 | /// cache. Another module actually invokes the send. | ||
82 | /// </summary> | ||
83 | private readonly BlockingQueue<ITextureSender> m_sharedSendersQueue; | ||
84 | |||
85 | private readonly Scene m_scene; | ||
86 | |||
87 | private readonly IClientAPI m_client; | ||
88 | |||
89 | public UserTextureDownloadService( | ||
90 | IClientAPI client, Scene scene, BlockingQueue<ITextureSender> sharedQueue) | ||
91 | { | ||
92 | m_client = client; | ||
93 | m_scene = scene; | ||
94 | m_sharedSendersQueue = sharedQueue; | ||
95 | } | ||
96 | |||
97 | /// <summary> | ||
98 | /// Handle a texture request. This involves creating a texture sender and placing it on the | ||
99 | /// previously passed in shared queue. | ||
100 | /// </summary> | ||
101 | /// <param name="e"></param> | ||
102 | public void HandleTextureRequest(TextureRequestArgs e) | ||
103 | { | ||
104 | TextureSender.TextureSender textureSender; | ||
105 | |||
106 | //TODO: should be working out the data size/ number of packets to be sent for each discard level | ||
107 | if ((e.DiscardLevel >= 0) || (e.Priority != 0)) | ||
108 | { | ||
109 | lock (m_textureSenders) | ||
110 | { | ||
111 | if (m_textureSenders.TryGetValue(e.RequestedAssetID, out textureSender)) | ||
112 | { | ||
113 | // If we've received new non UUID information for this request and it hasn't dispatched | ||
114 | // yet, then update the request accordingly. | ||
115 | textureSender.UpdateRequest(e.DiscardLevel, e.PacketNumber); | ||
116 | } | ||
117 | else | ||
118 | { | ||
119 | if (!foundTextureLimitStrategy.AllowRequest(e.RequestedAssetID)) | ||
120 | { | ||
121 | // m_log.DebugFormat( | ||
122 | // "[USER TEXTURE DOWNLOAD SERVICE]: Refusing request for {0} from client {1}", | ||
123 | // e.RequestedAssetID, m_client.AgentId); | ||
124 | |||
125 | return; | ||
126 | } | ||
127 | else if (!missingTextureLimitStrategy.AllowRequest(e.RequestedAssetID)) | ||
128 | { | ||
129 | if (missingTextureLimitStrategy.IsFirstRefusal(e.RequestedAssetID)) | ||
130 | { | ||
131 | // Commenting out this message for now as it causes too much noise with other | ||
132 | // debug messages. | ||
133 | // TODO: possibly record this as a statistic in the future | ||
134 | // | ||
135 | // m_log.DebugFormat( | ||
136 | // "[USER TEXTURE DOWNLOAD SERVICE]: Dropping requests for notified missing texture {0} for client {1} since we have received more than {2} requests", | ||
137 | // e.RequestedAssetID, m_client.AgentId, MAX_ALLOWED_TEXTURE_REQUESTS); | ||
138 | } | ||
139 | |||
140 | return; | ||
141 | } | ||
142 | |||
143 | m_scene.AddPendingDownloads(1); | ||
144 | |||
145 | TextureSender.TextureSender requestHandler = new TextureSender.TextureSender(m_client, e.DiscardLevel, e.PacketNumber); | ||
146 | m_textureSenders.Add(e.RequestedAssetID, requestHandler); | ||
147 | |||
148 | m_scene.AssetCache.GetAsset(e.RequestedAssetID, TextureCallback, true); | ||
149 | } | ||
150 | } | ||
151 | } | ||
152 | else | ||
153 | { | ||
154 | lock (m_textureSenders) | ||
155 | { | ||
156 | if (m_textureSenders.TryGetValue(e.RequestedAssetID, out textureSender)) | ||
157 | { | ||
158 | textureSender.Cancel = true; | ||
159 | } | ||
160 | } | ||
161 | } | ||
162 | } | ||
163 | |||
164 | /// <summary> | ||
165 | /// The callback for the asset cache when a texture has been retrieved. This method queues the | ||
166 | /// texture sender for processing. | ||
167 | /// </summary> | ||
168 | /// <param name="textureID"></param> | ||
169 | /// <param name="texture"></param> | ||
170 | public void TextureCallback(LLUUID textureID, AssetBase texture) | ||
171 | { | ||
172 | //m_log.DebugFormat("[USER TEXTURE DOWNLOAD SERVICE]: Calling TextureCallback with {0}, texture == null is {1}", textureID, (texture == null ? true : false)); | ||
173 | |||
174 | lock (m_textureSenders) | ||
175 | { | ||
176 | TextureSender.TextureSender textureSender; | ||
177 | |||
178 | if (m_textureSenders.TryGetValue(textureID, out textureSender)) | ||
179 | { | ||
180 | // XXX It may be perfectly valid for a texture to have no data... but if we pass | ||
181 | // this on to the TextureSender it will blow up, so just discard for now. | ||
182 | // Needs investigation. | ||
183 | if (texture == null || texture.Data == null) | ||
184 | { | ||
185 | if (!missingTextureLimitStrategy.IsMonitoringRequests(textureID)) | ||
186 | { | ||
187 | missingTextureLimitStrategy.MonitorRequests(textureID); | ||
188 | |||
189 | m_log.DebugFormat( | ||
190 | "[USER TEXTURE DOWNLOAD SERVICE]: Queueing first TextureNotFoundSender for {0}, client {1}", | ||
191 | textureID, m_client.AgentId); | ||
192 | } | ||
193 | |||
194 | ITextureSender textureNotFoundSender = new TextureNotFoundSender(m_client, textureID); | ||
195 | EnqueueTextureSender(textureNotFoundSender); | ||
196 | } | ||
197 | else | ||
198 | { | ||
199 | if (!textureSender.ImageLoaded) | ||
200 | { | ||
201 | textureSender.TextureReceived(texture); | ||
202 | EnqueueTextureSender(textureSender); | ||
203 | |||
204 | foundTextureLimitStrategy.MonitorRequests(textureID); | ||
205 | } | ||
206 | } | ||
207 | |||
208 | //m_log.InfoFormat("[TEXTURE SENDER] Removing texture sender with uuid {0}", textureID); | ||
209 | m_textureSenders.Remove(textureID); | ||
210 | //m_log.InfoFormat("[TEXTURE SENDER] Current texture senders in dictionary: {0}", m_textureSenders.Count); | ||
211 | } | ||
212 | else | ||
213 | { | ||
214 | m_log.WarnFormat( | ||
215 | "Got a texture uuid {0} with no sender object to handle it, this shouldn't happen", | ||
216 | textureID); | ||
217 | } | ||
218 | } | ||
219 | } | ||
220 | |||
221 | /// <summary> | ||
222 | /// Place a ready texture sender on the processing queue. | ||
223 | /// </summary> | ||
224 | /// <param name="textureSender"></param> | ||
225 | private void EnqueueTextureSender(ITextureSender textureSender) | ||
226 | { | ||
227 | textureSender.Cancel = false; | ||
228 | textureSender.Sending = true; | ||
229 | |||
230 | if (!m_sharedSendersQueue.Contains(textureSender)) | ||
231 | { | ||
232 | m_sharedSendersQueue.Enqueue(textureSender); | ||
233 | } | ||
234 | } | ||
235 | |||
236 | /// <summary> | ||
237 | /// Close this module. | ||
238 | /// </summary> | ||
239 | internal void Close() | ||
240 | { | ||
241 | lock (m_textureSenders) | ||
242 | { | ||
243 | foreach( TextureSender.TextureSender textureSender in m_textureSenders.Values ) | ||
244 | { | ||
245 | textureSender.Cancel = true; | ||
246 | } | ||
247 | |||
248 | m_textureSenders.Clear(); | ||
249 | } | ||
250 | } | ||
251 | } | ||
252 | } \ No newline at end of file | ||