From 6a8d8f54e88a21e6cfd78dc7981cdeec2f18094d Mon Sep 17 00:00:00 2001 From: Tleiades Hax Date: Tue, 30 Oct 2007 22:42:34 +0000 Subject: Step one on the long march towards grid based inventory. Introduction of an InevntoryServer --- OpenSim/Grid/InventoryServer/InventoryManager.cs | 170 +++++++++++++++++------ OpenSim/Grid/InventoryServer/Main.cs | 55 +++++--- 2 files changed, 166 insertions(+), 59 deletions(-) (limited to 'OpenSim/Grid') diff --git a/OpenSim/Grid/InventoryServer/InventoryManager.cs b/OpenSim/Grid/InventoryServer/InventoryManager.cs index 016b8bb..ca9ebf4 100644 --- a/OpenSim/Grid/InventoryServer/InventoryManager.cs +++ b/OpenSim/Grid/InventoryServer/InventoryManager.cs @@ -26,37 +26,36 @@ * */ using System; -using System.Collections; -using System.Collections.Generic; +using System.IO; using System.Text; -using OpenGrid.Framework.Data; -using libsecondlife; using System.Reflection; - +using System.Collections; +using System.Collections.Generic; using System.Xml; -using Nwc.XmlRpc; -using OpenSim.Framework.Sims; -using OpenSim.Framework.Inventory; -using OpenSim.Framework.Utilities; +using System.Xml.Serialization; +using libsecondlife; -using System.Security.Cryptography; +using OpenSim.Framework; +using OpenSim.Framework.Console; +using OpenSim.Framework.Servers; -namespace OpenGridServices.InventoryServer +namespace OpenSim.Grid.InventoryServer { - class InventoryManager + + public class InventoryManager { - Dictionary _plugins = new Dictionary(); + IInventoryData _databasePlugin; /// /// Adds a new inventory server plugin - user servers will be requested in the order they were loaded. /// /// The filename to the inventory server plugin DLL - public void AddPlugin(string FileName) + public void AddDatabasePlugin(string FileName) { - OpenSim.Framework.Console.MainConsole.Instance.Verbose( "Invenstorage: Attempting to load " + FileName); + MainLog.Instance.Verbose(OpenInventory_Main.LogName, "Invenstorage: Attempting to load " + FileName); Assembly pluginAssembly = Assembly.LoadFrom(FileName); - OpenSim.Framework.Console.MainConsole.Instance.Verbose( "Invenstorage: Found " + pluginAssembly.GetTypes().Length + " interfaces."); + MainLog.Instance.Verbose(OpenInventory_Main.LogName, "Invenstorage: Found " + pluginAssembly.GetTypes().Length + " interfaces."); foreach (Type pluginType in pluginAssembly.GetTypes()) { if (!pluginType.IsAbstract) @@ -67,8 +66,9 @@ namespace OpenGridServices.InventoryServer { IInventoryData plug = (IInventoryData)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); plug.Initialise(); - this._plugins.Add(plug.getName(), plug); - OpenSim.Framework.Console.MainConsole.Instance.Verbose( "Invenstorage: Added IUserData Interface"); + _databasePlugin = plug; + MainLog.Instance.Verbose(OpenInventory_Main.LogName, "Invenstorage: Added IInventoryData Interface"); + break; } typeInterface = null; @@ -78,48 +78,132 @@ namespace OpenGridServices.InventoryServer pluginAssembly = null; } - public List getRootFolders(LLUUID user) + protected static SerializableInventory loadInventoryFromXmlFile(string fileName) + { + FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); + XmlReader reader = new XmlTextReader(fs); + XmlSerializer x = new XmlSerializer(typeof(SerializableInventory)); + SerializableInventory inventory = (SerializableInventory)x.Deserialize(reader); + fs.Close(); + fs.Dispose(); + return inventory; + } + + protected static void saveInventoryToStream(SerializableInventory inventory, Stream s) { - foreach (KeyValuePair kvp in _plugins) + XmlTextWriter writer = new XmlTextWriter(s, Encoding.UTF8); + writer.Formatting = Formatting.Indented; + XmlSerializer x = new XmlSerializer(typeof(SerializableInventory)); + x.Serialize(writer, inventory); + } + + protected static bool fixupFolder(SerializableInventory.SerializableFolder f, SerializableInventory.SerializableFolder parent) + { + bool modified = false; + + // ensure we have a valid folder id + if (f.folderID == LLUUID.Zero) { - try - { - return kvp.Value.getUserRootFolders(user); - } - catch (Exception e) - { - OpenSim.Framework.Console.MainConsole.Instance.Notice("Unable to get root folders via " + kvp.Key + " (" + e.ToString() + ")"); - } + f.folderID = LLUUID.Random(); + modified = true; + } + + // ensure we have valid agent id + if (f.agentID == LLUUID.Zero) + { + if (parent != null) + f.agentID = parent.agentID; + else + f.agentID = f.folderID; + modified = true; + } + + if (f.parentID == LLUUID.Zero && parent != null) + { + f.parentID = parent.folderID; + modified = true; } - return null; + + + foreach (SerializableInventory.SerializableFolder child in f.SubFolders) + { + modified |= fixupFolder(child, f); + } + + return modified; } - public XmlRpcResponse XmlRpcInventoryRequest(XmlRpcRequest request) + protected static bool fixupInventory(SerializableInventory inventory) { - XmlRpcResponse response = new XmlRpcResponse(); - Hashtable requestData = (Hashtable)request.Params[0]; + return fixupFolder(inventory.root, null); + } - Hashtable responseData = new Hashtable(); + public class GetInventory : BaseStreamHandler + { + private SerializableInventory _inventory; + private InventoryManager _manager; + public GetInventory(InventoryManager manager) + : base("GET", "/inventory") + { + _manager = manager; - // Stuff happens here + _inventory = loadInventoryFromXmlFile("Inventory_Library.xml"); + if (fixupInventory(_inventory)) + { + FileStream fs = new FileStream("Inventory_Library.xml", FileMode.Truncate, FileAccess.Write); + saveInventoryToStream(_inventory, fs); + fs.Flush(); + fs.Close(); + MainLog.Instance.Debug(OpenInventory_Main.LogName, "Modified"); + } + } - if (requestData.ContainsKey("Access-type")) + private void CreateDefaultInventory(LLUUID userID) { - if (requestData["access-type"] == "rootfolders") + } + + private byte[] GetUserInventory(LLUUID userID) + { + MainLog.Instance.Notice(OpenInventory_Main.LogName, "Getting Inventory for user {0}", userID.ToStringHyphenated()); + byte[] result = new byte[] { }; + + InventoryFolderBase fb = _manager._databasePlugin.getUserRootFolder(userID); + if (fb == null) { -// responseData["rootfolders"] = + MainLog.Instance.Notice(OpenInventory_Main.LogName, "Inventory not found for user {0}, creating new", userID.ToStringHyphenated()); + CreateDefaultInventory(userID); } + + return result; } - else + + override public byte[] Handle(string path, Stream request) { - responseData["error"] = "No access-type specified."; - } + byte[] result = new byte[] { }; + string[] parms = path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); + if (parms.Length >= 1) + { + if (string.Compare(parms[1], "library", true) == 0) + { - // Stuff stops happening here + MemoryStream ms = new MemoryStream(); + saveInventoryToStream(_inventory, ms); - response.Value = responseData; - return response; + result = ms.GetBuffer(); + Array.Resize(ref result, (int)ms.Length); + } + else if (string.Compare(parms[1], "user", true) == 0) + { + if (parms.Length >= 2) + { + result = GetUserInventory(new LLUUID(parms[2])); + } + } + } + return result; + } } + } } diff --git a/OpenSim/Grid/InventoryServer/Main.cs b/OpenSim/Grid/InventoryServer/Main.cs index 97addb0..1bc396b 100644 --- a/OpenSim/Grid/InventoryServer/Main.cs +++ b/OpenSim/Grid/InventoryServer/Main.cs @@ -31,48 +31,71 @@ using System.Collections.Generic; using System.Reflection; using System.IO; using System.Text; + using libsecondlife; -using OpenSim.Framework.User; -using OpenSim.Framework.Sims; -using OpenSim.Framework.Inventory; -using OpenSim.Framework.Interfaces; + +using OpenSim.Framework; using OpenSim.Framework.Console; -using OpenSim.Servers; -using OpenSim.Framework.Utilities; +using OpenSim.Framework.Servers; + +using InventoryManager = OpenSim.Grid.InventoryServer.InventoryManager; -namespace OpenGridServices.InventoryServer +namespace OpenSim.Grid.InventoryServer { - public class OpenInventory_Main : BaseServer, conscmd_callback + public class OpenInventory_Main : conscmd_callback { - ConsoleBase m_console; + LogBase m_console; InventoryManager m_inventoryManager; + InventoryConfig m_config; + public const string LogName = "INVENTORY"; + + [STAThread] public static void Main(string[] args) { + OpenInventory_Main theServer = new OpenInventory_Main(); + theServer.Startup(); + + theServer.Work(); } public OpenInventory_Main() { - m_console = new ConsoleBase("opengrid-inventory-console.log", "OpenInventory", this, false); - MainConsole.Instance = m_console; + m_console = new LogBase("opengrid-inventory-console.log", LogName, this, true); + MainLog.Instance = m_console; } public void Startup() { - MainConsole.Instance.Notice("Initialising inventory manager..."); + MainLog.Instance.Notice("Initialising inventory manager..."); + m_config = new InventoryConfig(LogName, (Path.Combine(Util.configDir(), "InventoryServer_Config.xml"))); + m_inventoryManager = new InventoryManager(); + m_inventoryManager.AddDatabasePlugin(m_config.DatabaseProvider); + MainLog.Instance.Notice(LogName, "Starting HTTP server ..."); + BaseHttpServer httpServer = new BaseHttpServer(m_config.HttpPort); - MainConsole.Instance.Notice("Starting HTTP server"); - BaseHttpServer httpServer = new BaseHttpServer(8004); + httpServer.AddStreamHandler(new InventoryManager.GetInventory(m_inventoryManager)); - httpServer.AddXmlRPCHandler("rootfolders", m_inventoryManager.XmlRpcInventoryRequest); - //httpServer.AddRestHandler("GET","/rootfolders/",Rest + httpServer.Start(); + MainLog.Instance.Notice(LogName, "Started HTTP server"); + } + + private void Work() + { + m_console.Notice("Enter help for a list of commands\n"); + + while (true) + { + m_console.MainLogPrompt(); + } } public void RunCmd(string cmd, string[] cmdparams) { switch (cmd) { + case "quit": case "shutdown": m_console.Close(); Environment.Exit(0); -- cgit v1.1