From 5e7dba726896fcb84882b53952651742926e6efb Mon Sep 17 00:00:00 2001 From: Tleiades Hax Date: Fri, 26 Oct 2007 11:46:27 +0000 Subject: Very early first implementation of grid based assets. Run this on a major grid, and weep --- OpenSim/Grid/AssetServer/Main.cs | 170 ++++++++++++++++++++------------ OpenSim/Grid/AssetServer/RestService.cs | 109 ++++++++++++++++++++ 2 files changed, 217 insertions(+), 62 deletions(-) create mode 100644 OpenSim/Grid/AssetServer/RestService.cs (limited to 'OpenSim/Grid') diff --git a/OpenSim/Grid/AssetServer/Main.cs b/OpenSim/Grid/AssetServer/Main.cs index 9f1f9a2..dabacd4 100644 --- a/OpenSim/Grid/AssetServer/Main.cs +++ b/OpenSim/Grid/AssetServer/Main.cs @@ -28,15 +28,18 @@ using System; using System.IO; +using System.Reflection; using libsecondlife; +using Nini.Config; + +using OpenSim.Framework.Types; using OpenSim.Framework.Communications.Cache; using OpenSim.Framework.Configuration; using OpenSim.Framework.Console; -using OpenSim.Framework.Servers; -using OpenSim.Framework.Configuration; using OpenSim.Framework.Interfaces; using OpenSim.Framework.Utilities; +using OpenSim.Framework.Servers; /* using System.Text; @@ -57,7 +60,7 @@ namespace OpenSim.Grid.AssetServer public static OpenAsset_Main assetserver; private LogBase m_console; - private IAssetServer m_assetServer; + private IAssetProvider m_assetProvider; [STAThread] public static void Main(string[] args) @@ -97,11 +100,14 @@ namespace OpenSim.Grid.AssetServer m_console.Verbose("ASSET", "Setting up asset DB"); setupDB(m_config); + m_console.Verbose("ASSET", "Loading default asset set.."); + LoadDefaultAssets(); + m_console.Verbose("ASSET", "Starting HTTP process"); BaseHttpServer httpServer = new BaseHttpServer((int)m_config.HttpPort); - httpServer.AddStreamHandler(new GetAssetStreamHandler(this)); - httpServer.AddStreamHandler(new PostAssetStreamHandler( this )); + httpServer.AddStreamHandler(new GetAssetStreamHandler(this, m_assetProvider)); + httpServer.AddStreamHandler(new PostAssetStreamHandler(this, m_assetProvider)); httpServer.Start(); } @@ -111,14 +117,49 @@ namespace OpenSim.Grid.AssetServer return null; } + + public IAssetProvider LoadDatabasePlugin(string FileName) + { + MainLog.Instance.Verbose("ASSET SERVER", "LoadDatabasePlugin: Attempting to load " + FileName); + Assembly pluginAssembly = Assembly.LoadFrom(FileName); + IAssetProvider assetPlugin = null; + foreach (Type pluginType in pluginAssembly.GetTypes()) + { + if (!pluginType.IsAbstract) + { + Type typeInterface = pluginType.GetInterface("IAssetProvider", true); + + if (typeInterface != null) + { + IAssetProvider plug = (IAssetProvider)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); + assetPlugin = plug; + assetPlugin.Initialise(); + + MainLog.Instance.Verbose("ASSET SERVER", "Added " + assetPlugin.Name + " " + assetPlugin.Version); + break; + } + + typeInterface = null; + } + } + + pluginAssembly = null; + return assetPlugin; + } + public void setupDB(AssetConfig config) { try { - SQLAssetServer assetServer = new SQLAssetServer(config.DatabaseProvider ); - assetServer.LoadDefaultAssets(); + m_assetProvider = LoadDatabasePlugin(config.DatabaseProvider); + if (m_assetProvider == null) + { + MainLog.Instance.Error("ASSET", "Failed to load a database plugin, server halting"); + Environment.Exit(-1); + } +// assetServer.LoadDefaultAssets(); - m_assetServer = assetServer; +// m_assetServer = assetServer; } catch (Exception e) { @@ -127,84 +168,89 @@ namespace OpenSim.Grid.AssetServer } } - public void RunCmd(string cmd, string[] cmdparams) + public void LoadAsset(AssetBase info, bool image, string filename) { - switch (cmd) - { - case "help": - m_console.Notice("shutdown - shutdown this asset server (USE CAUTION!)"); - break; - - case "shutdown": - m_console.Close(); - Environment.Exit(0); - break; - } - } + //should request Asset from storage manager + //but for now read from file - public void Show(string ShowWhat) - { + string dataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "assets"); //+ folder; + string fileName = Path.Combine(dataPath, filename); + FileInfo fInfo = new FileInfo(fileName); + long numBytes = fInfo.Length; + FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); + byte[] idata = new byte[numBytes]; + BinaryReader br = new BinaryReader(fStream); + idata = br.ReadBytes((int)numBytes); + br.Close(); + fStream.Close(); + info.Data = idata; + //info.loaded=true; } - } - public class GetAssetStreamHandler : BaseStreamHandler - { - OpenAsset_Main m_assetManager; - - override public byte[] Handle(string path, Stream request) + public AssetBase CreateAsset(string assetIdStr, string name, string filename, bool isImage) { - string param = GetParam(path); + AssetBase asset = new AssetBase( + new LLUUID(assetIdStr), + name + ); - byte[] assetdata = m_assetManager.GetAssetData(new LLUUID(param), false); - if (assetdata != null) + if (!String.IsNullOrEmpty(filename)) { - return assetdata; + MainLog.Instance.Verbose("ASSETS", "Loading: [{0}][{1}]", name, filename); + + LoadAsset(asset, isImage, filename); } else { - return new byte[]{}; + MainLog.Instance.Verbose("ASSETS", "Instantiated: [{0}]", name); } - } - public GetAssetStreamHandler(OpenAsset_Main assetManager) : base("/assets/", "GET") - { - m_assetManager = assetManager; + return asset; } - } - - public class PostAssetStreamHandler : BaseStreamHandler - { - OpenAsset_Main m_assetManager; - override public byte[] Handle(string path, Stream request) + public void LoadDefaultAssets() { - string param = GetParam(path); - LLUUID assetId = new LLUUID(param); - byte[] txBuffer = new byte[4096]; - - using( BinaryReader binReader = new BinaryReader( request ) ) + string filePath = Path.Combine(Util.configDir(), "OpenSimAssetSet.xml"); + if (File.Exists(filePath)) { - using (MemoryStream memoryStream = new MemoryStream(4096)) + XmlConfigSource source = new XmlConfigSource(filePath); + + for (int i = 0; i < source.Configs.Count; i++) { - int count; - while ((count = binReader.Read(txBuffer, 0, 4096)) > 0) - { - memoryStream.Write(txBuffer, 0, count); - } + string assetIdStr = source.Configs[i].GetString("assetID", LLUUID.Random().ToStringHyphenated()); + string name = source.Configs[i].GetString("name", ""); + sbyte type = (sbyte)source.Configs[i].GetInt("assetType", 0); + sbyte invType = (sbyte)source.Configs[i].GetInt("inventoryType", 0); + string fileName = source.Configs[i].GetString("fileName", ""); + + AssetBase newAsset = CreateAsset(assetIdStr, name, fileName, false); - byte[] assetData = memoryStream.ToArray(); + newAsset.Type = type; + newAsset.InvType = invType; -// m_assetManager.CreateAsset(assetId, assetData); + m_assetProvider.CreateAsset(newAsset); } } - - return new byte[]{}; } - public PostAssetStreamHandler( OpenAsset_Main assetManager ) - : base("/assets/", "POST") + + public void RunCmd(string cmd, string[] cmdparams) + { + switch (cmd) + { + case "help": + m_console.Notice("shutdown - shutdown this asset server (USE CAUTION!)"); + break; + + case "shutdown": + m_console.Close(); + Environment.Exit(0); + break; + } + } + + public void Show(string ShowWhat) { - m_assetManager = assetManager; } } } diff --git a/OpenSim/Grid/AssetServer/RestService.cs b/OpenSim/Grid/AssetServer/RestService.cs new file mode 100644 index 0000000..88a1668 --- /dev/null +++ b/OpenSim/Grid/AssetServer/RestService.cs @@ -0,0 +1,109 @@ +using System; +using System.IO; +using System.Xml; +using System.Xml.Serialization; +using System.Text; + +using libsecondlife; +using OpenSim.Framework.Types; +using OpenSim.Framework.Servers; +using OpenSim.Framework.Interfaces; +using OpenSim.Framework.Console; + +namespace OpenSim.Grid.AssetServer +{ + public class GetAssetStreamHandler : BaseStreamHandler + { + OpenAsset_Main m_assetManager; + IAssetProvider m_assetProvider; + + override public byte[] Handle(string path, Stream request) + { + string param = GetParam(path); + byte[] result = new byte[] { }; + try { + + string[] p = param.Split(new char[] { '/', '?', '&' }, StringSplitOptions.RemoveEmptyEntries); + + if (p.Length > 0) + { + LLUUID assetID; + bool isTexture = false; + LLUUID.TryParse(p[0], out assetID); + if (p.Length > 1) + { + if (string.Compare(p[1], "texture", true) == 0) + isTexture = true; + } + + + AssetBase asset = m_assetProvider.FetchAsset(assetID); + if (asset != null) + { + MainLog.Instance.Debug("REST", "GET:/asset found {0}, {1}", assetID, asset.Name); + + XmlSerializer xs = new XmlSerializer(typeof(AssetBase)); + MemoryStream ms = new MemoryStream(); + XmlTextWriter xw = new XmlTextWriter(ms, Encoding.UTF8); + xw.Formatting = Formatting.Indented; + xs.Serialize(xw, asset); + xw.Flush(); + + ms.Seek(0, SeekOrigin.Begin); + StreamReader sr = new StreamReader(ms); + + result = ms.GetBuffer(); + Array.Resize(ref result, (int)ms.Length); + } + else + { + MainLog.Instance.Verbose("REST", "GET:/asset failed to find {0}", assetID); + } + } + } + catch (Exception e) + { + MainLog.Instance.Error(e.ToString()); + } + return result; + } + + public GetAssetStreamHandler(OpenAsset_Main assetManager, IAssetProvider assetProvider) + : base("GET", "/assets" ) + { + m_assetManager = assetManager; + m_assetProvider = assetProvider; + } + } + + public class PostAssetStreamHandler : BaseStreamHandler + { + OpenAsset_Main m_assetManager; + IAssetProvider m_assetProvider; + + override public byte[] Handle(string path, Stream request) + { + string param = GetParam(path); + + LLUUID assetId; + if(param.Length > 0) + LLUUID.TryParse(param, out assetId); + byte[] txBuffer = new byte[4096]; + + XmlSerializer xs = new XmlSerializer(typeof(AssetBase)); + AssetBase asset = (AssetBase)xs.Deserialize(request); + + MainLog.Instance.Verbose("REST", "StoreAndCommitAsset {0}", asset.FullID); + m_assetProvider.CreateAsset(asset); + + return new byte[] { }; + } + + public PostAssetStreamHandler(OpenAsset_Main assetManager, IAssetProvider assetProvider) + : base("POST", "/assets") + { + m_assetManager = assetManager; + m_assetProvider = assetProvider; + } + } +} -- cgit v1.1