clockwerk-opensim – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 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 OpenSimulator 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 log4net; |
||
29 | using System; |
||
30 | using System.Collections.Generic; |
||
31 | using System.IO; |
||
32 | using System.Reflection; |
||
33 | using Nini.Config; |
||
34 | using OpenSim.Framework; |
||
35 | using OpenSim.Framework.Console; |
||
36 | using OpenSim.Framework.Communications; |
||
37 | using OpenSim.Services.Interfaces; |
||
38 | using OpenMetaverse; |
||
39 | |||
40 | namespace OpenSim.Services.Connectors |
||
41 | { |
||
42 | public class AssetServicesConnector : BaseServiceConnector, IAssetService |
||
43 | { |
||
44 | private static readonly ILog m_log = |
||
45 | LogManager.GetLogger( |
||
46 | MethodBase.GetCurrentMethod().DeclaringType); |
||
47 | |||
48 | private string m_ServerURI = String.Empty; |
||
49 | private IImprovedAssetCache m_Cache = null; |
||
50 | private int m_maxAssetRequestConcurrency = 30; |
||
51 | |||
52 | private delegate void AssetRetrievedEx(AssetBase asset); |
||
53 | |||
54 | // Keeps track of concurrent requests for the same asset, so that it's only loaded once. |
||
55 | // Maps: Asset ID -> Handlers which will be called when the asset has been loaded |
||
56 | private Dictionary<string, AssetRetrievedEx> m_AssetHandlers = new Dictionary<string, AssetRetrievedEx>(); |
||
57 | |||
58 | public int MaxAssetRequestConcurrency |
||
59 | { |
||
60 | get { return m_maxAssetRequestConcurrency; } |
||
61 | set { m_maxAssetRequestConcurrency = value; } |
||
62 | } |
||
63 | |||
64 | public AssetServicesConnector() |
||
65 | { |
||
66 | } |
||
67 | |||
68 | public AssetServicesConnector(string serverURI) |
||
69 | { |
||
70 | m_ServerURI = serverURI.TrimEnd('/'); |
||
71 | } |
||
72 | |||
73 | public AssetServicesConnector(IConfigSource source) |
||
74 | : base(source, "AssetService") |
||
75 | { |
||
76 | Initialise(source); |
||
77 | } |
||
78 | |||
79 | public virtual void Initialise(IConfigSource source) |
||
80 | { |
||
81 | IConfig netconfig = source.Configs["Network"]; |
||
82 | if (netconfig != null) |
||
83 | m_maxAssetRequestConcurrency = netconfig.GetInt("MaxRequestConcurrency",m_maxAssetRequestConcurrency); |
||
84 | |||
85 | IConfig assetConfig = source.Configs["AssetService"]; |
||
86 | if (assetConfig == null) |
||
87 | { |
||
88 | m_log.Error("[ASSET CONNECTOR]: AssetService missing from OpenSim.ini"); |
||
89 | throw new Exception("Asset connector init error"); |
||
90 | } |
||
91 | |||
92 | string serviceURI = assetConfig.GetString("AssetServerURI", |
||
93 | String.Empty); |
||
94 | |||
95 | if (serviceURI == String.Empty) |
||
96 | { |
||
97 | m_log.Error("[ASSET CONNECTOR]: No Server URI named in section AssetService"); |
||
98 | throw new Exception("Asset connector init error"); |
||
99 | } |
||
100 | |||
101 | m_ServerURI = serviceURI; |
||
102 | } |
||
103 | |||
104 | protected void SetCache(IImprovedAssetCache cache) |
||
105 | { |
||
106 | m_Cache = cache; |
||
107 | } |
||
108 | |||
109 | public AssetBase Get(string id) |
||
110 | { |
||
111 | // m_log.DebugFormat("[ASSET SERVICE CONNECTOR]: Synchronous get request for {0}", id); |
||
112 | |||
113 | string uri = m_ServerURI + "/assets/" + id; |
||
114 | |||
115 | AssetBase asset = null; |
||
116 | if (m_Cache != null) |
||
117 | asset = m_Cache.Get(id); |
||
118 | |||
119 | if (asset == null) |
||
120 | { |
||
121 | // XXX: Commented out for now since this has either never been properly operational or not for some time |
||
122 | // as m_maxAssetRequestConcurrency was being passed as the timeout, not a concurrency limiting option. |
||
123 | // Wasn't noticed before because timeout wasn't actually used. |
||
124 | // Not attempting concurrency setting for now as this omission was discovered in release candidate |
||
125 | // phase for OpenSimulator 0.8. Need to revisit afterwards. |
||
126 | // asset |
||
127 | // = SynchronousRestObjectRequester.MakeRequest<int, AssetBase>( |
||
128 | // "GET", uri, 0, m_maxAssetRequestConcurrency); |
||
129 | |||
130 | asset = SynchronousRestObjectRequester.MakeRequest<int, AssetBase>("GET", uri, 0, m_Auth); |
||
131 | |||
132 | if (m_Cache != null) |
||
133 | m_Cache.Cache(asset); |
||
134 | } |
||
135 | return asset; |
||
136 | } |
||
137 | |||
138 | public AssetBase GetCached(string id) |
||
139 | { |
||
140 | // m_log.DebugFormat("[ASSET SERVICE CONNECTOR]: Cache request for {0}", id); |
||
141 | |||
142 | if (m_Cache != null) |
||
143 | return m_Cache.Get(id); |
||
144 | |||
145 | return null; |
||
146 | } |
||
147 | |||
148 | public AssetMetadata GetMetadata(string id) |
||
149 | { |
||
150 | if (m_Cache != null) |
||
151 | { |
||
152 | AssetBase fullAsset = m_Cache.Get(id); |
||
153 | |||
154 | if (fullAsset != null) |
||
155 | return fullAsset.Metadata; |
||
156 | } |
||
157 | |||
158 | string uri = m_ServerURI + "/assets/" + id + "/metadata"; |
||
159 | |||
160 | AssetMetadata asset = SynchronousRestObjectRequester.MakeRequest<int, AssetMetadata>("GET", uri, 0, m_Auth); |
||
161 | return asset; |
||
162 | } |
||
163 | |||
164 | public byte[] GetData(string id) |
||
165 | { |
||
166 | if (m_Cache != null) |
||
167 | { |
||
168 | AssetBase fullAsset = m_Cache.Get(id); |
||
169 | |||
170 | if (fullAsset != null) |
||
171 | return fullAsset.Data; |
||
172 | } |
||
173 | |||
174 | using (RestClient rc = new RestClient(m_ServerURI)) |
||
175 | { |
||
176 | rc.AddResourcePath("assets"); |
||
177 | rc.AddResourcePath(id); |
||
178 | rc.AddResourcePath("data"); |
||
179 | |||
180 | rc.RequestMethod = "GET"; |
||
181 | |||
182 | Stream s = rc.Request(m_Auth); |
||
183 | |||
184 | if (s == null) |
||
185 | return null; |
||
186 | |||
187 | if (s.Length > 0) |
||
188 | { |
||
189 | byte[] ret = new byte[s.Length]; |
||
190 | s.Read(ret, 0, (int)s.Length); |
||
191 | |||
192 | return ret; |
||
193 | } |
||
194 | |||
195 | return null; |
||
196 | } |
||
197 | } |
||
198 | |||
199 | public bool Get(string id, Object sender, AssetRetrieved handler) |
||
200 | { |
||
201 | // m_log.DebugFormat("[ASSET SERVICE CONNECTOR]: Potentially asynchronous get request for {0}", id); |
||
202 | |||
203 | string uri = m_ServerURI + "/assets/" + id; |
||
204 | |||
205 | AssetBase asset = null; |
||
206 | if (m_Cache != null) |
||
207 | asset = m_Cache.Get(id); |
||
208 | |||
209 | if (asset == null) |
||
210 | { |
||
211 | lock (m_AssetHandlers) |
||
212 | { |
||
213 | AssetRetrievedEx handlerEx = new AssetRetrievedEx(delegate(AssetBase _asset) { handler(id, sender, _asset); }); |
||
214 | |||
215 | AssetRetrievedEx handlers; |
||
216 | if (m_AssetHandlers.TryGetValue(id, out handlers)) |
||
217 | { |
||
218 | // Someone else is already loading this asset. It will notify our handler when done. |
||
219 | handlers += handlerEx; |
||
220 | return true; |
||
221 | } |
||
222 | |||
223 | // Load the asset ourselves |
||
224 | handlers += handlerEx; |
||
225 | m_AssetHandlers.Add(id, handlers); |
||
226 | } |
||
227 | |||
228 | bool success = false; |
||
229 | try |
||
230 | { |
||
231 | AsynchronousRestObjectRequester.MakeRequest<int, AssetBase>("GET", uri, 0, |
||
232 | delegate(AssetBase a) |
||
233 | { |
||
234 | if (a != null && m_Cache != null) |
||
235 | m_Cache.Cache(a); |
||
236 | |||
237 | AssetRetrievedEx handlers; |
||
238 | lock (m_AssetHandlers) |
||
239 | { |
||
240 | handlers = m_AssetHandlers[id]; |
||
241 | m_AssetHandlers.Remove(id); |
||
242 | } |
||
243 | handlers.Invoke(a); |
||
244 | }, m_maxAssetRequestConcurrency, m_Auth); |
||
245 | |||
246 | success = true; |
||
247 | } |
||
248 | finally |
||
249 | { |
||
250 | if (!success) |
||
251 | { |
||
252 | lock (m_AssetHandlers) |
||
253 | { |
||
254 | m_AssetHandlers.Remove(id); |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | else |
||
260 | { |
||
261 | handler(id, sender, asset); |
||
262 | } |
||
263 | |||
264 | return true; |
||
265 | } |
||
266 | |||
267 | public virtual bool[] AssetsExist(string[] ids) |
||
268 | { |
||
269 | string uri = m_ServerURI + "/get_assets_exist"; |
||
270 | |||
271 | bool[] exist = null; |
||
272 | try |
||
273 | { |
||
274 | exist = SynchronousRestObjectRequester.MakeRequest<string[], bool[]>("POST", uri, ids, m_Auth); |
||
275 | } |
||
276 | catch (Exception) |
||
277 | { |
||
278 | // This is most likely to happen because the server doesn't support this function, |
||
279 | // so just silently return "doesn't exist" for all the assets. |
||
280 | } |
||
281 | |||
282 | if (exist == null) |
||
283 | exist = new bool[ids.Length]; |
||
284 | |||
285 | return exist; |
||
286 | } |
||
287 | |||
288 | public string Store(AssetBase asset) |
||
289 | { |
||
290 | if (asset.Local) |
||
291 | { |
||
292 | if (m_Cache != null) |
||
293 | m_Cache.Cache(asset); |
||
294 | |||
295 | return asset.ID; |
||
296 | } |
||
297 | |||
298 | string uri = m_ServerURI + "/assets/"; |
||
299 | |||
300 | string newID; |
||
301 | try |
||
302 | { |
||
303 | newID = SynchronousRestObjectRequester.MakeRequest<AssetBase, string>("POST", uri, asset, m_Auth); |
||
304 | } |
||
305 | catch (Exception e) |
||
306 | { |
||
307 | m_log.Warn(string.Format("[ASSET CONNECTOR]: Unable to send asset {0} to asset server. Reason: {1} ", asset.ID, e.Message), e); |
||
308 | return string.Empty; |
||
309 | } |
||
310 | |||
311 | // TEMPORARY: SRAS returns 'null' when it's asked to store existing assets |
||
312 | if (newID == null) |
||
313 | { |
||
314 | m_log.DebugFormat("[ASSET CONNECTOR]: Storing of asset {0} returned null; assuming the asset already exists", asset.ID); |
||
315 | return asset.ID; |
||
316 | } |
||
317 | |||
318 | if (string.IsNullOrEmpty(newID)) |
||
319 | return string.Empty; |
||
320 | |||
321 | asset.ID = newID; |
||
322 | |||
323 | if (m_Cache != null) |
||
324 | m_Cache.Cache(asset); |
||
325 | |||
326 | return newID; |
||
327 | } |
||
328 | |||
329 | public bool UpdateContent(string id, byte[] data) |
||
330 | { |
||
331 | AssetBase asset = null; |
||
332 | |||
333 | if (m_Cache != null) |
||
334 | asset = m_Cache.Get(id); |
||
335 | |||
336 | if (asset == null) |
||
337 | { |
||
338 | AssetMetadata metadata = GetMetadata(id); |
||
339 | if (metadata == null) |
||
340 | return false; |
||
341 | |||
342 | asset = new AssetBase(metadata.FullID, metadata.Name, metadata.Type, UUID.Zero.ToString()); |
||
343 | asset.Metadata = metadata; |
||
344 | } |
||
345 | asset.Data = data; |
||
346 | |||
347 | string uri = m_ServerURI + "/assets/" + id; |
||
348 | |||
349 | if (SynchronousRestObjectRequester.MakeRequest<AssetBase, bool>("POST", uri, asset, m_Auth)) |
||
350 | { |
||
351 | if (m_Cache != null) |
||
352 | m_Cache.Cache(asset); |
||
353 | |||
354 | return true; |
||
355 | } |
||
356 | return false; |
||
357 | } |
||
358 | |||
359 | public bool Delete(string id) |
||
360 | { |
||
361 | string uri = m_ServerURI + "/assets/" + id; |
||
362 | |||
363 | if (SynchronousRestObjectRequester.MakeRequest<int, bool>("DELETE", uri, 0, m_Auth)) |
||
364 | { |
||
365 | if (m_Cache != null) |
||
366 | m_Cache.Expire(id); |
||
367 | |||
368 | return true; |
||
369 | } |
||
370 | return false; |
||
371 | } |
||
372 | } |
||
373 | } |