OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
HGAssetBroker.cs
Go to the documentation of this file.
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")]
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 HGAssetBroker() {}
73 
74  public HGAssetBroker(IConfigSource config)
75  {
76  Initialise(config);
77  }
78 
79  public void Initialise(IConfigSource source)
80  {
81  IConfig moduleConfig = source.Configs["Modules"];
82  if (moduleConfig != null)
83  {
84  string name = moduleConfig.GetString("AssetServices", "");
85  if (name == Name)
86  {
87  IConfig assetConfig = source.Configs["AssetService"];
88  if (assetConfig == null)
89  {
90  m_log.Error("[HG ASSET CONNECTOR]: AssetService missing from OpenSim.ini");
91  return;
92  }
93 
94  string localDll = assetConfig.GetString("LocalGridAssetService",
95  String.Empty);
96  string HGDll = assetConfig.GetString("HypergridAssetService",
97  String.Empty);
98 
99  if (localDll == String.Empty)
100  {
101  m_log.Error("[HG ASSET CONNECTOR]: No LocalGridAssetService named in section AssetService");
102  return;
103  }
104 
105  if (HGDll == String.Empty)
106  {
107  m_log.Error("[HG ASSET CONNECTOR]: No HypergridAssetService named in section AssetService");
108  return;
109  }
110 
111  Object[] args = new Object[] { source };
112  m_GridService =
113  ServerUtils.LoadPlugin<IAssetService>(localDll,
114  args);
115 
116  m_HGService =
117  ServerUtils.LoadPlugin<IAssetService>(HGDll,
118  args);
119 
120  if (m_GridService == null)
121  {
122  m_log.Error("[HG ASSET CONNECTOR]: Can't load local asset service");
123  return;
124  }
125  if (m_HGService == null)
126  {
127  m_log.Error("[HG ASSET CONNECTOR]: Can't load hypergrid asset service");
128  return;
129  }
130 
131  m_LocalAssetServiceURI = assetConfig.GetString("AssetServerURI", string.Empty);
132  if (m_LocalAssetServiceURI == string.Empty)
133  {
134  IConfig netConfig = source.Configs["Network"];
135  m_LocalAssetServiceURI = netConfig.GetString("asset_server_url", string.Empty);
136  }
137 
138  if (m_LocalAssetServiceURI != string.Empty)
139  m_LocalAssetServiceURI = m_LocalAssetServiceURI.Trim('/');
140 
141  IConfig hgConfig = source.Configs["HGAssetService"];
142  m_AssetPerms = new AssetPermissions(hgConfig); // it's ok if arg is null
143 
144  m_Enabled = true;
145  m_log.Info("[HG ASSET CONNECTOR]: HG asset broker enabled");
146  }
147  }
148  }
149 
150  public void PostInitialise()
151  {
152  }
153 
154  public void Close()
155  {
156  }
157 
158  public void AddRegion(Scene scene)
159  {
160  if (!m_Enabled)
161  return;
162 
163  m_aScene = scene;
164 
165  m_aScene.RegisterModuleInterface<IAssetService>(this);
166  }
167 
168  public void RemoveRegion(Scene scene)
169  {
170  }
171 
172  public void RegionLoaded(Scene scene)
173  {
174  if (!m_Enabled)
175  return;
176 
177  if (m_Cache == null)
178  {
179  m_Cache = scene.RequestModuleInterface<IImprovedAssetCache>();
180 
181  if (!(m_Cache is ISharedRegionModule))
182  m_Cache = null;
183 
184  }
185 
186  m_log.InfoFormat("[HG ASSET CONNECTOR]: Enabled hypergrid asset broker for region {0}", scene.RegionInfo.RegionName);
187 
188  if (m_Cache != null)
189  {
190  m_log.InfoFormat("[HG ASSET CONNECTOR]: Enabled asset caching for region {0}", scene.RegionInfo.RegionName);
191  }
192  }
193 
194  private bool IsHG(string id)
195  {
196  Uri assetUri;
197 
198  if (Uri.TryCreate(id, UriKind.Absolute, out assetUri) &&
199  assetUri.Scheme == Uri.UriSchemeHttp)
200  return true;
201 
202  return false;
203  }
204 
205  public AssetBase Get(string id)
206  {
207  //m_log.DebugFormat("[HG ASSET CONNECTOR]: Get {0}", id);
208  AssetBase asset = null;
209 
210  if (m_Cache != null)
211  {
212  asset = m_Cache.Get(id);
213 
214  if (asset != null)
215  return asset;
216  }
217 
218  if (IsHG(id))
219  {
220  asset = m_HGService.Get(id);
221  if (asset != null)
222  {
223  // Now store it locally, if allowed
224  if (m_AssetPerms.AllowedImport(asset.Type))
225  m_GridService.Store(asset);
226  else
227  return null;
228  }
229  }
230  else
231  asset = m_GridService.Get(id);
232 
233  if (m_Cache != null)
234  m_Cache.Cache(asset);
235 
236  return asset;
237  }
238 
239  public AssetBase GetCached(string id)
240  {
241  if (m_Cache != null)
242  return m_Cache.Get(id);
243 
244  return null;
245  }
246 
247  public AssetMetadata GetMetadata(string id)
248  {
249  AssetBase asset = null;
250 
251  if (m_Cache != null)
252  {
253  if (m_Cache != null)
254  m_Cache.Get(id);
255 
256  if (asset != null)
257  return asset.Metadata;
258  }
259 
260  AssetMetadata metadata;
261 
262  if (IsHG(id))
263  metadata = m_HGService.GetMetadata(id);
264  else
265  metadata = m_GridService.GetMetadata(id);
266 
267  return metadata;
268  }
269 
270  public byte[] GetData(string id)
271  {
272  AssetBase asset = null;
273 
274  if (m_Cache != null)
275  {
276  if (m_Cache != null)
277  m_Cache.Get(id);
278 
279  if (asset != null)
280  return asset.Data;
281  }
282 
283  if (IsHG(id))
284  return m_HGService.GetData(id);
285  else
286  return m_GridService.GetData(id);
287 
288  }
289 
290  public bool Get(string id, Object sender, AssetRetrieved handler)
291  {
292  AssetBase asset = null;
293 
294  if (m_Cache != null)
295  asset = m_Cache.Get(id);
296 
297  if (asset != null)
298  {
299  Util.FireAndForget(delegate { handler(id, sender, asset); }, null, "HGAssetBroker.GotFromCache");
300  return true;
301  }
302 
303  if (IsHG(id))
304  {
305  return m_HGService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
306  {
307  if (m_Cache != null)
308  m_Cache.Cache(a);
309  handler(assetID, s, a);
310  });
311  }
312  else
313  {
314  return m_GridService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
315  {
316  if (m_Cache != null)
317  m_Cache.Cache(a);
318  handler(assetID, s, a);
319  });
320  }
321  }
322 
323  public virtual bool[] AssetsExist(string[] ids)
324  {
325  int numHG = 0;
326  foreach (string id in ids)
327  {
328  if (IsHG(id))
329  ++numHG;
330  }
331 
332  if (numHG == 0)
333  return m_GridService.AssetsExist(ids);
334  else if (numHG == ids.Length)
335  return m_HGService.AssetsExist(ids);
336  else
337  throw new Exception("[HG ASSET CONNECTOR]: AssetsExist: all the assets must be either local or foreign");
338  }
339 
340  public string Store(AssetBase asset)
341  {
342  if (asset.Local || asset.Temporary)
343  {
344  if (m_Cache != null)
345  m_Cache.Cache(asset);
346  return asset.ID;
347  }
348 
349  bool isHG = IsHG(asset.ID);
350  if ((m_Cache != null) && !isHG)
351  // Don't store it in the cache if the asset is to
352  // be sent to the other grid, because this is already
353  // a copy of the local asset.
354  m_Cache.Cache(asset);
355 
356  string id;
357  if (IsHG(asset.ID))
358  {
359  if (m_AssetPerms.AllowedExport(asset.Type))
360  id = m_HGService.Store(asset);
361  else
362  return String.Empty;
363  }
364  else
365  id = m_GridService.Store(asset);
366 
367  if (String.IsNullOrEmpty(id))
368  return string.Empty;
369 
370  asset.ID = id;
371 
372  if (m_Cache != null)
373  m_Cache.Cache(asset);
374 
375  return id;
376  }
377 
378  public bool UpdateContent(string id, byte[] data)
379  {
380  AssetBase asset = null;
381 
382  if (m_Cache != null)
383  asset = m_Cache.Get(id);
384 
385  if (asset != null)
386  {
387  asset.Data = data;
388  m_Cache.Cache(asset);
389  }
390 
391  if (IsHG(id))
392  return m_HGService.UpdateContent(id, data);
393  else
394  return m_GridService.UpdateContent(id, data);
395  }
396 
397  public bool Delete(string id)
398  {
399  if (m_Cache != null)
400  m_Cache.Expire(id);
401 
402  bool result = false;
403  if (IsHG(id))
404  result = m_HGService.Delete(id);
405  else
406  result = m_GridService.Delete(id);
407 
408  if (result && m_Cache != null)
409  m_Cache.Expire(id);
410 
411  return result;
412  }
413 
414  }
415 }
byte[] GetData(string id)
Get an asset's data, ignoring the metadata.
AssetBase GetCached(string id)
Synchronously fetches an asset from the local cache only.
void Initialise(IConfigSource source)
This is called to initialize the region module. For shared modules, this is called exactly once...
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
sbyte Type
(sbyte) AssetType enum
Definition: AssetBase.cs:198
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
bool Get(string id, Object sender, AssetRetrieved handler)
Get an asset synchronously or asynchronously (depending on whether it is locally cached) and fire a c...
bool Local
Is this a region only asset, or does this exist on the asset server also
Definition: AssetBase.cs:213
virtual bool[] AssetsExist(string[] ids)
Check if assets exist in the database.
Asset class. All Assets are reference by this class or a class derived from this class ...
Definition: AssetBase.cs:49
void PostInitialise()
This is called exactly once after all the shared region-modules have been instanciated and IRegionMod...
Non-texture assets
bool UpdateContent(string id, byte[] data)
Update an asset's content
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
Interactive OpenSim region server
Definition: OpenSim.cs:55
AssetBase Get(string id)
Get an asset synchronously.
AssetMetadata GetMetadata(string id)
Get an asset's metadata
delegate void AssetRetrieved(string id, Object sender, AssetBase asset)
string ID
Asset MetaData ID (transferring from UUID to string ID)
Definition: AssetBase.cs:177
bool Temporary
Is this asset going to be saved to the asset database?
Definition: AssetBase.cs:222