Main.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. Copyright (c) OpenSim project, http://osgrid.org/
  3. * All rights reserved.
  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 <organization> 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 <copyright holder> ``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 <copyright holder> 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. using System;
  28. using System.IO;
  29. using System.Text;
  30. using System.Timers;
  31. using System.Net;
  32. using System.Reflection;
  33. using System.Threading;
  34. using libsecondlife;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Sims;
  37. using OpenSim.Framework.Console;
  38. using OpenSim.Framework.Types;
  39. using OpenSim.Framework.Interfaces;
  40. using OpenSim.GridInterfaces.Local; // REFACTORING IS NEEDED!!!!!!!!!!!
  41. using OpenSim.Servers;
  42. using Db4objects.Db4o;
  43. using Db4objects.Db4o.Query;
  44. namespace OpenGridServices.AssetServer
  45. {
  46. /// <summary>
  47. /// </summary>
  48. public class OpenAsset_Main : BaseServer, conscmd_callback
  49. {
  50. private IObjectContainer db;
  51. public static OpenAsset_Main assetserver;
  52. private ConsoleBase m_console;
  53. [STAThread]
  54. public static void Main(string[] args)
  55. {
  56. Console.WriteLine("Starting...\n");
  57. assetserver = new OpenAsset_Main();
  58. assetserver.Startup();
  59. assetserver.Work();
  60. }
  61. private void Work()
  62. {
  63. m_console.WriteLine("\nEnter help for a list of commands\n");
  64. while (true)
  65. {
  66. m_console.MainConsolePrompt();
  67. }
  68. }
  69. private OpenAsset_Main()
  70. {
  71. m_console = new ConsoleBase("opengrid-AssetServer-console.log", "OpenGrid", this, false);
  72. MainConsole.Instance = m_console;
  73. }
  74. public void Startup()
  75. {
  76. m_console.WriteLine("Main.cs:Startup() - Setting up asset DB");
  77. setupDB();
  78. m_console.WriteLine("Main.cs:Startup() - Starting HTTP process");
  79. BaseHttpServer httpServer = new BaseHttpServer(8003);
  80. httpServer.AddRestHandler("GET", "/assets/", this.assetGetMethod);
  81. httpServer.Start();
  82. }
  83. public string assetGetMethod(string request, string path, string param) {
  84. byte[] assetdata=getAssetData(new LLUUID(param),false);
  85. if(assetdata!=null) {
  86. return System.Text.Encoding.ASCII.GetString(assetdata);
  87. } else {
  88. return "";
  89. }
  90. }
  91. public byte[] getAssetData(LLUUID assetID, bool isTexture) {
  92. byte[] idata = null;
  93. bool found = false;
  94. AssetStorage foundAsset = null;
  95. IObjectSet result = db.Get(new AssetStorage(assetID));
  96. if (result.Count > 0)
  97. {
  98. foundAsset = (AssetStorage)result.Next();
  99. found = true;
  100. }
  101. if (found)
  102. {
  103. return foundAsset.Data;
  104. }
  105. else
  106. {
  107. return null;
  108. }
  109. }
  110. public void setupDB() {
  111. bool yapfile=System.IO.File.Exists("assets.yap");
  112. try
  113. {
  114. db = Db4oFactory.OpenFile("assets.yap");
  115. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Main.cs:setupDB() - creation");
  116. }
  117. catch (Exception e)
  118. {
  119. db.Close();
  120. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Main.cs:setupDB() - Exception occured");
  121. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(e.ToString());
  122. }
  123. if (!yapfile)
  124. {
  125. this.LoadDB();
  126. }
  127. }
  128. public void LoadDB() {
  129. try
  130. {
  131. Console.WriteLine("setting up Asset database");
  132. AssetBase Image = new AssetBase();
  133. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000001");
  134. Image.Name = "Bricks";
  135. this.LoadAsset(Image, true, "bricks.jp2");
  136. AssetStorage store = new AssetStorage();
  137. store.Data = Image.Data;
  138. store.Name = Image.Name;
  139. store.UUID = Image.FullID;
  140. db.Set(store);
  141. db.Commit();
  142. Image = new AssetBase();
  143. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000002");
  144. Image.Name = "Plywood";
  145. this.LoadAsset(Image, true, "plywood.jp2");
  146. store = new AssetStorage();
  147. store.Data = Image.Data;
  148. store.Name = Image.Name;
  149. store.UUID = Image.FullID;
  150. db.Set(store);
  151. db.Commit();
  152. Image = new AssetBase();
  153. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000003");
  154. Image.Name = "Rocks";
  155. this.LoadAsset(Image, true, "rocks.jp2");
  156. store = new AssetStorage();
  157. store.Data = Image.Data;
  158. store.Name = Image.Name;
  159. store.UUID = Image.FullID;
  160. db.Set(store);
  161. db.Commit();
  162. Image = new AssetBase();
  163. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000004");
  164. Image.Name = "Granite";
  165. this.LoadAsset(Image, true, "granite.jp2");
  166. store = new AssetStorage();
  167. store.Data = Image.Data;
  168. store.Name = Image.Name;
  169. store.UUID = Image.FullID;
  170. db.Set(store);
  171. db.Commit();
  172. Image = new AssetBase();
  173. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000005");
  174. Image.Name = "Hardwood";
  175. this.LoadAsset(Image, true, "hardwood.jp2");
  176. store = new AssetStorage();
  177. store.Data = Image.Data;
  178. store.Name = Image.Name;
  179. store.UUID = Image.FullID;
  180. db.Set(store);
  181. db.Commit();
  182. Image = new AssetBase();
  183. Image.FullID = new LLUUID("00000000-0000-0000-5005-000000000005");
  184. Image.Name = "Prim Base Texture";
  185. this.LoadAsset(Image, true, "plywood.jp2");
  186. store = new AssetStorage();
  187. store.Data = Image.Data;
  188. store.Name = Image.Name;
  189. store.UUID = Image.FullID;
  190. db.Set(store);
  191. db.Commit();
  192. Image = new AssetBase();
  193. Image.FullID = new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73");
  194. Image.Name = "Shape";
  195. this.LoadAsset(Image, false, "base_shape.dat");
  196. store = new AssetStorage();
  197. store.Data = Image.Data;
  198. store.Name = Image.Name;
  199. store.UUID = Image.FullID;
  200. db.Set(store);
  201. db.Commit();
  202. }
  203. catch (Exception e)
  204. {
  205. Console.WriteLine(e.Message);
  206. }
  207. }
  208. private void LoadAsset(AssetBase info, bool image, string filename)
  209. {
  210. string dataPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "assets"); //+ folder;
  211. string fileName = Path.Combine(dataPath, filename);
  212. FileInfo fInfo = new FileInfo(fileName);
  213. long numBytes = fInfo.Length;
  214. FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  215. byte[] idata = new byte[numBytes];
  216. BinaryReader br = new BinaryReader(fStream);
  217. idata = br.ReadBytes((int)numBytes);
  218. br.Close();
  219. fStream.Close();
  220. info.Data = idata;
  221. //info.loaded=true;
  222. }
  223. /*private GridConfig LoadConfigDll(string dllName)
  224. {
  225. Assembly pluginAssembly = Assembly.LoadFrom(dllName);
  226. GridConfig config = null;
  227. foreach (Type pluginType in pluginAssembly.GetTypes())
  228. {
  229. if (pluginType.IsPublic)
  230. {
  231. if (!pluginType.IsAbstract)
  232. {
  233. Type typeInterface = pluginType.GetInterface("IGridConfig", true);
  234. if (typeInterface != null)
  235. {
  236. IGridConfig plug = (IGridConfig)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  237. config = plug.GetConfigObject();
  238. break;
  239. }
  240. typeInterface = null;
  241. }
  242. }
  243. }
  244. pluginAssembly = null;
  245. return config;
  246. }*/
  247. public void RunCmd(string cmd, string[] cmdparams)
  248. {
  249. switch (cmd)
  250. {
  251. case "help":
  252. m_console.WriteLine("shutdown - shutdown this asset server (USE CAUTION!)");
  253. break;
  254. case "shutdown":
  255. m_console.Close();
  256. Environment.Exit(0);
  257. break;
  258. }
  259. }
  260. public void Show(string ShowWhat)
  261. {
  262. }
  263. }
  264. }