clockwerk-opensim-stable – Blame information for rev 1

Subversion Repositories:
Rev:
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 Mono.Addins;
30 using Nini.Config;
31 using System;
32 using System.Collections.Generic;
33 using System.Reflection;
34 using OpenSim.Framework;
35  
36 using OpenSim.Server.Base;
37 using OpenSim.Region.Framework.Interfaces;
38 using OpenSim.Region.Framework.Scenes;
39 using OpenSim.Services.Interfaces;
40 using OpenMetaverse;
41  
42 namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
43 {
44 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "HGAssetBroker")]
45 public class HGAssetBroker : ISharedRegionModule, IAssetService
46 {
47 private static readonly ILog m_log =
48 LogManager.GetLogger(
49 MethodBase.GetCurrentMethod().DeclaringType);
50  
51 private IImprovedAssetCache m_Cache = null;
52 private IAssetService m_GridService;
53 private IAssetService m_HGService;
54  
55 private Scene m_aScene;
56 private string m_LocalAssetServiceURI;
57  
58 private bool m_Enabled = false;
59  
60 private AssetPermissions m_AssetPerms;
61  
62 public Type ReplaceableInterface
63 {
64 get { return null; }
65 }
66  
67 public string Name
68 {
69 get { return "HGAssetBroker"; }
70 }
71  
72 public void Initialise(IConfigSource source)
73 {
74 IConfig moduleConfig = source.Configs["Modules"];
75 if (moduleConfig != null)
76 {
77 string name = moduleConfig.GetString("AssetServices", "");
78 if (name == Name)
79 {
80 IConfig assetConfig = source.Configs["AssetService"];
81 if (assetConfig == null)
82 {
83 m_log.Error("[HG ASSET CONNECTOR]: AssetService missing from OpenSim.ini");
84 return;
85 }
86  
87 string localDll = assetConfig.GetString("LocalGridAssetService",
88 String.Empty);
89 string HGDll = assetConfig.GetString("HypergridAssetService",
90 String.Empty);
91  
92 if (localDll == String.Empty)
93 {
94 m_log.Error("[HG ASSET CONNECTOR]: No LocalGridAssetService named in section AssetService");
95 return;
96 }
97  
98 if (HGDll == String.Empty)
99 {
100 m_log.Error("[HG ASSET CONNECTOR]: No HypergridAssetService named in section AssetService");
101 return;
102 }
103  
104 Object[] args = new Object[] { source };
105 m_GridService =
106 ServerUtils.LoadPlugin<IAssetService>(localDll,
107 args);
108  
109 m_HGService =
110 ServerUtils.LoadPlugin<IAssetService>(HGDll,
111 args);
112  
113 if (m_GridService == null)
114 {
115 m_log.Error("[HG ASSET CONNECTOR]: Can't load local asset service");
116 return;
117 }
118 if (m_HGService == null)
119 {
120 m_log.Error("[HG ASSET CONNECTOR]: Can't load hypergrid asset service");
121 return;
122 }
123  
124 m_LocalAssetServiceURI = assetConfig.GetString("AssetServerURI", string.Empty);
125 if (m_LocalAssetServiceURI == string.Empty)
126 {
127 IConfig netConfig = source.Configs["Network"];
128 m_LocalAssetServiceURI = netConfig.GetString("asset_server_url", string.Empty);
129 }
130  
131 if (m_LocalAssetServiceURI != string.Empty)
132 m_LocalAssetServiceURI = m_LocalAssetServiceURI.Trim('/');
133  
134 IConfig hgConfig = source.Configs["HGAssetService"];
135 m_AssetPerms = new AssetPermissions(hgConfig); // it's ok if arg is null
136  
137 m_Enabled = true;
138 m_log.Info("[HG ASSET CONNECTOR]: HG asset broker enabled");
139 }
140 }
141 }
142  
143 public void PostInitialise()
144 {
145 }
146  
147 public void Close()
148 {
149 }
150  
151 public void AddRegion(Scene scene)
152 {
153 if (!m_Enabled)
154 return;
155  
156 m_aScene = scene;
157  
158 m_aScene.RegisterModuleInterface<IAssetService>(this);
159 }
160  
161 public void RemoveRegion(Scene scene)
162 {
163 }
164  
165 public void RegionLoaded(Scene scene)
166 {
167 if (!m_Enabled)
168 return;
169  
170 if (m_Cache == null)
171 {
172 m_Cache = scene.RequestModuleInterface<IImprovedAssetCache>();
173  
174 if (!(m_Cache is ISharedRegionModule))
175 m_Cache = null;
176 }
177  
178 m_log.InfoFormat("[HG ASSET CONNECTOR]: Enabled hypergrid asset broker for region {0}", scene.RegionInfo.RegionName);
179  
180 if (m_Cache != null)
181 {
182 m_log.InfoFormat("[HG ASSET CONNECTOR]: Enabled asset caching for region {0}", scene.RegionInfo.RegionName);
183 }
184 }
185  
186 private bool IsHG(string id)
187 {
188 Uri assetUri;
189  
190 if (Uri.TryCreate(id, UriKind.Absolute, out assetUri) &&
191 assetUri.Scheme == Uri.UriSchemeHttp)
192 return true;
193  
194 return false;
195 }
196  
197 public AssetBase Get(string id)
198 {
199 //m_log.DebugFormat("[HG ASSET CONNECTOR]: Get {0}", id);
200 AssetBase asset = null;
201  
202 if (m_Cache != null)
203 {
204 asset = m_Cache.Get(id);
205  
206 if (asset != null)
207 return asset;
208 }
209  
210 if (IsHG(id))
211 {
212 asset = m_HGService.Get(id);
213 if (asset != null)
214 {
215 // Now store it locally, if allowed
216 if (m_AssetPerms.AllowedImport(asset.Type))
217 m_GridService.Store(asset);
218 else
219 return null;
220 }
221 }
222 else
223 asset = m_GridService.Get(id);
224  
225 if (m_Cache != null)
226 m_Cache.Cache(asset);
227  
228 return asset;
229 }
230  
231 public AssetBase GetCached(string id)
232 {
233 if (m_Cache != null)
234 return m_Cache.Get(id);
235  
236 return null;
237 }
238  
239 public AssetMetadata GetMetadata(string id)
240 {
241 AssetBase asset = null;
242  
243 if (m_Cache != null)
244 {
245 if (m_Cache != null)
246 m_Cache.Get(id);
247  
248 if (asset != null)
249 return asset.Metadata;
250 }
251  
252 AssetMetadata metadata;
253  
254 if (IsHG(id))
255 metadata = m_HGService.GetMetadata(id);
256 else
257 metadata = m_GridService.GetMetadata(id);
258  
259 return metadata;
260 }
261  
262 public byte[] GetData(string id)
263 {
264 AssetBase asset = null;
265  
266 if (m_Cache != null)
267 {
268 if (m_Cache != null)
269 m_Cache.Get(id);
270  
271 if (asset != null)
272 return asset.Data;
273 }
274  
275 if (IsHG(id))
276 return m_HGService.GetData(id);
277 else
278 return m_GridService.GetData(id);
279  
280 }
281  
282 public bool Get(string id, Object sender, AssetRetrieved handler)
283 {
284 AssetBase asset = null;
285  
286 if (m_Cache != null)
287 asset = m_Cache.Get(id);
288  
289 if (asset != null)
290 {
291 Util.FireAndForget(delegate { handler(id, sender, asset); });
292 return true;
293 }
294  
295 if (IsHG(id))
296 {
297 return m_HGService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
298 {
299 if (m_Cache != null)
300 m_Cache.Cache(a);
301 handler(assetID, s, a);
302 });
303 }
304 else
305 {
306 return m_GridService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
307 {
308 if (m_Cache != null)
309 m_Cache.Cache(a);
310 handler(assetID, s, a);
311 });
312 }
313 }
314  
315 public string Store(AssetBase asset)
316 {
317 bool isHG = IsHG(asset.ID);
318  
319 if ((m_Cache != null) && !isHG)
320 // Don't store it in the cache if the asset is to
321 // be sent to the other grid, because this is already
322 // a copy of the local asset.
323 m_Cache.Cache(asset);
324  
325 if (asset.Local)
326 {
327 if (m_Cache != null)
328 m_Cache.Cache(asset);
329 return asset.ID;
330 }
331  
332 string id = string.Empty;
333 if (IsHG(asset.ID))
334 {
335 if (m_AssetPerms.AllowedExport(asset.Type))
336 id = m_HGService.Store(asset);
337 else
338 return String.Empty;
339 }
340 else
341 id = m_GridService.Store(asset);
342  
343 if (id != String.Empty)
344 {
345 // Placing this here, so that this work with old asset servers that don't send any reply back
346 // SynchronousRestObjectRequester returns somethins that is not an empty string
347 if (id != null)
348 asset.ID = id;
349  
350 if (m_Cache != null)
351 m_Cache.Cache(asset);
352 }
353 return id;
354  
355 }
356  
357 public bool UpdateContent(string id, byte[] data)
358 {
359 AssetBase asset = null;
360  
361 if (m_Cache != null)
362 asset = m_Cache.Get(id);
363  
364 if (asset != null)
365 {
366 asset.Data = data;
367 m_Cache.Cache(asset);
368 }
369  
370 if (IsHG(id))
371 return m_HGService.UpdateContent(id, data);
372 else
373 return m_GridService.UpdateContent(id, data);
374 }
375  
376 public bool Delete(string id)
377 {
378 if (m_Cache != null)
379 m_Cache.Expire(id);
380  
381 bool result = false;
382 if (IsHG(id))
383 result = m_HGService.Delete(id);
384 else
385 result = m_GridService.Delete(id);
386  
387 if (result && m_Cache != null)
388 m_Cache.Expire(id);
389  
390 return result;
391 }
392  
393 }
394 }