29 using System.Collections.Generic;
30 using System.Collections.Specialized;
33 using System.Reflection;
37 using OpenSim.Framework;
38 using OpenSim.Region.Framework.Interfaces;
39 using OpenSim.Region.Framework.Scenes;
40 using OpenSim.Services.Interfaces;
42 using OpenMetaverse.StructuredData;
44 namespace OpenSim.Services.Connectors.SimianGrid
49 [Extension(Path =
"/OpenSim/RegionModules", NodeName =
"RegionModule", Id =
"SimianAssetServiceConnector")]
52 private static readonly ILog m_log =
54 MethodBase.GetCurrentMethod().DeclaringType);
55 private static string ZeroID = UUID.Zero.ToString();
57 private string m_serverUrl = String.Empty;
59 private bool m_Enabled =
false;
61 #region ISharedRegionModule
63 public Type ReplaceableInterface {
get {
return null; } }
77 public string Name {
get {
return "SimianAssetServiceConnector"; } }
81 #endregion ISharedRegionModule
90 if (!url.EndsWith(
"/") && !url.EndsWith(
"="))
97 IConfig moduleConfig = source.Configs[
"Modules"];
98 if (moduleConfig != null)
100 string name = moduleConfig.GetString(
"AssetServices",
"");
106 private void CommonInit(IConfigSource source)
108 IConfig gridConfig = source.Configs[
"AssetService"];
109 if (gridConfig != null)
111 string serviceUrl = gridConfig.GetString(
"AssetServerURI");
112 if (!
String.IsNullOrEmpty(serviceUrl))
114 if (!serviceUrl.EndsWith(
"/") && !serviceUrl.EndsWith(
"="))
115 serviceUrl = serviceUrl +
'/';
116 m_serverUrl = serviceUrl;
120 if (
String.IsNullOrEmpty(m_serverUrl))
121 m_log.Info(
"[SIMIAN ASSET CONNECTOR]: No AssetServerURI specified, disabling connector");
126 #region IAssetService
130 if (
String.IsNullOrEmpty(m_serverUrl))
132 m_log.Error(
"[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
133 throw new InvalidOperationException();
144 return SimianGetOperation(
id);
151 return m_cache.Get(id);
163 if (
String.IsNullOrEmpty(m_serverUrl))
165 m_log.Error(
"[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
166 throw new InvalidOperationException();
174 return asset.Metadata;
178 return SimianGetMetadataOperation(
id);
183 if (
String.IsNullOrEmpty(m_serverUrl))
185 m_log.Error(
"[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
186 throw new InvalidOperationException();
206 if (
String.IsNullOrEmpty(m_serverUrl))
208 m_log.Error(
"[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
209 throw new InvalidOperationException();
218 handler(
id, sender, asset);
226 AssetBase asset = SimianGetOperation(
id);
227 handler(
id, sender, asset);
228 }, null,
"SimianAssetServiceConnector.GetFromService"
236 if (
String.IsNullOrEmpty(m_serverUrl))
238 m_log.Error(
"[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
239 throw new InvalidOperationException();
242 bool[] exist =
new bool[ids.Length];
244 for (
int i = 0; i < ids.Length; i++)
247 if (metadata != null)
262 if (
String.IsNullOrEmpty(m_serverUrl))
264 m_log.Error(
"[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
265 throw new InvalidOperationException();
268 bool storedInCache =
false;
271 if (
String.IsNullOrEmpty(asset.
ID) || asset.ID == ZeroID)
273 asset.FullID = UUID.Random();
274 asset.ID = asset.FullID.ToString();
280 m_cache.Cache(asset);
281 storedInCache =
true;
289 m_log.Error(
"Cannot store local " + asset.Metadata.ContentType +
" asset without an asset cache");
291 asset.FullID = UUID.Zero;
297 return SimianStoreOperation(asset);
309 if (
String.IsNullOrEmpty(m_serverUrl))
311 m_log.Error(
"[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
312 throw new InvalidOperationException();
319 m_log.WarnFormat(
"[SIMIAN ASSET CONNECTOR]: Failed to fetch asset {0} for updating", id);
325 string result = Store(asset);
326 return !String.IsNullOrEmpty(result);
336 if (
String.IsNullOrEmpty(m_serverUrl))
338 m_log.Error(
"[SIMIAN ASSET CONNECTOR]: No AssetServerURI configured");
339 throw new InvalidOperationException();
345 return SimianDeleteOperation(
id);
348 #endregion IAssetService
350 #region SimianOperations
351 private bool SimianDeleteOperation(
string id)
360 NameValueCollection requestArgs =
new NameValueCollection
362 {
"RequestMethod",
"xRemoveAsset" },
366 OSDMap response = SimianGrid.PostToService(m_serverUrl,requestArgs);
367 if (! response[
"Success"].AsBoolean())
369 m_log.WarnFormat(
"[SIMIAN ASSET CONNECTOR]: failed to delete asset; {0}",response[
"Message"].AsString());
378 m_log.WarnFormat(
"[SIMIAN ASSET CONNECTOR]: failed to delete asset {0}; {1}", id, ex.Message);
389 private string SimianStoreOperation(
AssetBase asset)
393 NameValueCollection requestArgs =
new NameValueCollection
395 {
"RequestMethod",
"xAddAsset" },
396 {
"ContentType", asset.Metadata.ContentType },
397 {
"EncodedData", Convert.ToBase64String(asset.Data) },
398 {
"AssetID", asset.FullID.ToString() },
399 {
"CreatorID", asset.Metadata.CreatorID },
400 {
"Temporary", asset.Temporary ?
"1" :
"0" },
401 {
"Name", asset.Name }
404 OSDMap response = SimianGrid.PostToService(m_serverUrl,requestArgs);
405 if (! response[
"Success"].AsBoolean())
407 m_log.WarnFormat(
"[SIMIAN ASSET CONNECTOR] failed to store asset; {0}",response[
"Message"].AsString());
417 m_log.ErrorFormat(
"[SIMIAN ASSET CONNECTOR] failed to store asset; {0}",ex.Message);
428 private AssetBase SimianGetOperation(
string id)
432 NameValueCollection requestArgs =
new NameValueCollection
434 {
"RequestMethod",
"xGetAsset" },
438 OSDMap response = SimianGrid.PostToService(m_serverUrl,requestArgs);
439 if (! response[
"Success"].AsBoolean())
441 m_log.WarnFormat(
"[SIMIAN ASSET CONNECTOR] Failed to get asset; {0}",response[
"Message"].AsString());
448 asset.Name = String.Empty;
449 asset.Metadata.ContentType = response[
"ContentType"].AsString();
450 asset.CreatorID = response[
"CreatorID"].AsString();
451 asset.Data = System.Convert.FromBase64String(response[
"EncodedData"].AsString());
453 asset.Temporary = response[
"Temporary"];
459 m_log.WarnFormat(
"[SIMIAN ASSET CONNECTOR]: failed to retrieve asset {0}; {1}", id, ex.Message);
475 NameValueCollection requestArgs =
new NameValueCollection
477 {
"RequestMethod",
"xGetAssetMetadata" },
481 OSDMap response = SimianGrid.PostToService(m_serverUrl,requestArgs);
482 if (! response[
"Success"].AsBoolean())
491 metadata.ContentType = response[
"ContentType"].AsString();
492 metadata.CreatorID = response[
"CreatorID"].AsString();
493 metadata.Local =
false;
494 metadata.Temporary = response[
"Temporary"];
496 string lastModifiedStr = response[
"Last-Modified"].AsString();
497 if (!
String.IsNullOrEmpty(lastModifiedStr))
499 DateTime lastModified;
500 if (DateTime.TryParse(lastModifiedStr, out lastModified))
508 m_log.WarnFormat(
"[SIMIAN ASSET CONNECTOR]: Failed to get asset metadata; {0}", ex.Message);
SimianAssetServiceConnector()
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
bool[] AssetsExist(string[] ids)
Check if assets exist in the database.
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
bool Get(string id, Object sender, AssetRetrieved handler)
Get an asset asynchronously
AssetBase Get(string id)
Get an asset synchronously.
OpenMetaverse.StructuredData.OSDMap OSDMap
SimianAssetServiceConnector(string url)
byte[] GetData(string id)
Get an asset's data, ignoring the metadata.
string Store(AssetBase asset)
Creates a new asset
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
AssetMetadata GetMetadata(string id)
Get an asset's metadata
bool Local
Is this a region only asset, or does this exist on the asset server also
Asset class. All Assets are reference by this class or a class derived from this class ...
void Initialise(IConfigSource source)
This is called to initialize the region module. For shared modules, this is called exactly once...
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
SimianAssetServiceConnector(IConfigSource source)
bool UpdateContent(string id, byte[] data)
Update an asset's content
bool Delete(string id)
Delete an asset
void PostInitialise()
This is called exactly once after all the shared region-modules have been instanciated and IRegionMod...
delegate void AssetRetrieved(string id, Object sender, AssetBase asset)
string ID
Asset MetaData ID (transferring from UUID to string ID)
AssetBase GetCached(string id)
Synchronously fetches an asset from the local cache only.
Connects to the SimianGrid asset service