View of /trunk/AssetClient/AssetClient.cs
Parent Directory
|
Revision Log
Revision 25 -
(download)
(annotate)
Sat Nov 15 03:56:39 2008 UTC (4 years, 7 months ago) by jhurliman
File size: 7663 byte(s)
Sat Nov 15 03:56:39 2008 UTC (4 years, 7 months ago) by jhurliman
File size: 7663 byte(s)
* Added MemcacheStorage, sits on top of another storage provider and uses memcached to cache metadata and asset data * Implemented Robert's suggestion to replace AssetURL with a list of method to URI mappings that contains data=>uri * Upgraded to latest ExtensionLoader with simplified extension starting * Simplified SimpleStorage by removing SimpleMetadata and using a dictionary of UUID to filenames * Moved Metadata into its own file, added serialization and deserialization methods * Added LICENSES.txt file for third party libraries (not complete yet)
/*
* Copyright (c) 2008 Intel Corporation
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* -- Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* -- Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* -- Neither the name of the Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Net;
using System.IO;
using System.Xml;
using OpenMetaverse;
using OpenMetaverse.StructuredData;
namespace AssetClient
{
public static class AssetClient
{
public const string ASSET_SERVER_URL = "http://localhost:9001";
public static bool TryGetAsset(UUID assetID, string assetURL, out byte[] assetData)
{
assetData = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(assetURL);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
int contentLength = (int)response.ContentLength;
assetData = new byte[contentLength];
using (Stream stream = response.GetResponseStream())
{
int pos = 0;
while (pos < contentLength)
pos += stream.Read(assetData, pos, contentLength - pos);
}
return true;
}
}
catch (WebException ex)
{
if (ex.Response == null || ((HttpWebResponse)ex.Response).StatusCode != HttpStatusCode.NotFound)
Logger.Log(ex.Message, Helpers.LogLevel.Error);
}
return false;
}
public static bool TryGetMetadata(UUID assetID, out byte[] metadata)
{
return TryGetMetadata(assetID, ASSET_SERVER_URL, out metadata);
}
public static bool TryGetMetadata(UUID assetID, string assetServerURL, out byte[] metadata)
{
metadata = null;
assetServerURL = assetServerURL.TrimEnd('/');
string requestURL = String.Format("{0}/{1}/metadata", assetServerURL, assetID);
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
int contentLength = (int)response.ContentLength;
metadata = new byte[contentLength];
using (Stream stream = response.GetResponseStream())
{
int pos = 0;
while (pos < contentLength)
pos += stream.Read(metadata, pos, contentLength - pos);
}
return true;
}
}
catch (WebException ex)
{
if (ex.Response == null || ((HttpWebResponse)ex.Response).StatusCode != HttpStatusCode.NotFound)
Logger.Log(ex.Message, Helpers.LogLevel.Error);
}
return false;
}
public static UUID CreateAsset(string name, string description, AssetType type, bool temporary, byte[] assetData)
{
return CreateAsset(name, description, type, temporary, assetData, UUID.Zero, ASSET_SERVER_URL);
}
public static UUID CreateAsset(string name, string description, AssetType type, bool temporary, byte[] assetData,
UUID assetID)
{
return CreateAsset(name, description, type, temporary, assetData, assetID, ASSET_SERVER_URL);
}
public static UUID CreateAsset(string name, string description, AssetType type, bool temporary, byte[] assetData,
UUID assetID, string assetServerURL)
{
assetServerURL = assetServerURL.TrimEnd('/');
string requestURL = assetServerURL + "/createasset";
OSDMap map = new OSDMap();
if (assetID != UUID.Zero) map["id"] = OSD.FromUUID(assetID);
map["name"] = OSD.FromString(name);
map["description"] = OSD.FromString(description);
map["type"] = OSD.FromString(type.ToString());
map["temporary"] = OSD.FromBoolean(temporary);
map["data"] = OSD.FromBinary(assetData);
byte[] postData = OSDParser.SerializeLLSDXmlBytes(map);
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL);
request.Method = "POST";
request.ContentLength = postData.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postData, 0, postData.Length);
stream.Flush();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.Created)
{
assetID = UUID.Zero;
using (Stream stream = response.GetResponseStream())
{
XmlTextReader reader = new XmlTextReader(stream);
OSD osdata = OSDParser.DeserializeLLSDXml(reader);
if (osdata.Type == OSDType.Map)
{
OSDMap responseMap = (OSDMap)osdata;
if (responseMap.ContainsKey("id"))
assetID = responseMap["id"].AsUUID();
}
}
response.Close();
return assetID;
}
else
{
response.Close();
return UUID.Zero;
}
}
catch (WebException ex)
{
Logger.Log(ex.Message, Helpers.LogLevel.Error);
}
return UUID.Zero;
}
}
}
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

