Main.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 OpenSim 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 System;
  29. using System.IO;
  30. using System.Reflection;
  31. using libsecondlife;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.AssetLoader.Filesystem;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Framework.Servers;
  36. using OpenSim.Framework.Statistics;
  37. namespace OpenSim.Grid.AssetServer
  38. {
  39. /// <summary>
  40. /// An asset server
  41. /// </summary>
  42. public class OpenAsset_Main : BaseOpenSimServer, conscmd_callback
  43. {
  44. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  45. public AssetConfig m_config;
  46. public static OpenAsset_Main assetserver;
  47. // Temporarily hardcoded - should be a plugin
  48. protected IAssetLoader assetLoader = new AssetLoaderFileSystem();
  49. private IAssetProvider m_assetProvider;
  50. [STAThread]
  51. public static void Main(string[] args)
  52. {
  53. log4net.Config.XmlConfigurator.Configure();
  54. m_log.Info("Starting...\n");
  55. assetserver = new OpenAsset_Main();
  56. assetserver.Startup();
  57. assetserver.Work();
  58. }
  59. private void Work()
  60. {
  61. m_console.Notice("Enter help for a list of commands");
  62. while (true)
  63. {
  64. m_console.Prompt();
  65. }
  66. }
  67. private OpenAsset_Main()
  68. {
  69. m_console = new ConsoleBase("OpenAsset", this);
  70. MainConsole.Instance = m_console;
  71. }
  72. public void Startup()
  73. {
  74. m_config = new AssetConfig("ASSET SERVER", (Path.Combine(Util.configDir(), "AssetServer_Config.xml")));
  75. m_log.Info("[ASSET]: Setting up asset DB");
  76. setupDB(m_config);
  77. m_log.Info("[ASSET]: Loading default asset set..");
  78. LoadDefaultAssets();
  79. m_log.Info("[ASSET]: Starting HTTP process");
  80. BaseHttpServer httpServer = new BaseHttpServer(m_config.HttpPort);
  81. StatsManager.StartCollectingAssetStats();
  82. httpServer.AddStreamHandler(new GetAssetStreamHandler(this, m_assetProvider));
  83. httpServer.AddStreamHandler(new PostAssetStreamHandler(this, m_assetProvider));
  84. httpServer.Start();
  85. }
  86. public byte[] GetAssetData(LLUUID assetID, bool isTexture)
  87. {
  88. return null;
  89. }
  90. public IAssetProvider LoadDatabasePlugin(string FileName)
  91. {
  92. m_log.Info("[ASSET SERVER]: LoadDatabasePlugin: Attempting to load " + FileName);
  93. Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  94. IAssetProvider assetPlugin = null;
  95. foreach (Type pluginType in pluginAssembly.GetTypes())
  96. {
  97. if (!pluginType.IsAbstract)
  98. {
  99. Type typeInterface = pluginType.GetInterface("IAssetProvider", true);
  100. if (typeInterface != null)
  101. {
  102. IAssetProvider plug =
  103. (IAssetProvider) Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  104. assetPlugin = plug;
  105. assetPlugin.Initialise();
  106. m_log.Info("[ASSET SERVER]: Added " + assetPlugin.Name + " " + assetPlugin.Version);
  107. break;
  108. }
  109. typeInterface = null;
  110. }
  111. }
  112. pluginAssembly = null;
  113. return assetPlugin;
  114. }
  115. public void setupDB(AssetConfig config)
  116. {
  117. try
  118. {
  119. m_assetProvider = LoadDatabasePlugin(config.DatabaseProvider);
  120. if (m_assetProvider == null)
  121. {
  122. m_log.Error("[ASSET]: Failed to load a database plugin, server halting");
  123. Environment.Exit(-1);
  124. }
  125. }
  126. catch (Exception e)
  127. {
  128. m_log.Warn("[ASSET]: setupDB() - Exception occured");
  129. m_log.Warn("[ASSET]: " + e.ToString());
  130. }
  131. }
  132. public void LoadDefaultAssets()
  133. {
  134. assetLoader.ForEachDefaultXmlAsset(StoreAsset);
  135. }
  136. protected void StoreAsset(AssetBase asset)
  137. {
  138. m_assetProvider.CreateAsset(asset);
  139. }
  140. public override void RunCmd(string cmd, string[] cmdparams)
  141. {
  142. base.RunCmd(cmd, cmdparams);
  143. switch (cmd)
  144. {
  145. case "help":
  146. m_console.Notice(
  147. @"shutdown - shutdown this asset server (USE CAUTION!)
  148. stats - statistical information for this server");
  149. break;
  150. case "stats":
  151. m_console.Notice("STATS", Environment.NewLine + StatsManager.AssetStats.Report());
  152. break;
  153. case "shutdown":
  154. m_console.Close();
  155. Environment.Exit(0);
  156. break;
  157. }
  158. }
  159. }
  160. }