Main.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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.Framework.Utilities;
  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(OpenSim.Framework.Console.LogPriority.HIGH, "\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(OpenSim.Framework.Console.LogPriority.LOW, "Main.cs:Startup() - Setting up asset DB");
  77. setupDB();
  78. m_console.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "Main.cs:Startup() - Starting HTTP process");
  79. AssetHttpServer httpServer = new AssetHttpServer(8003);
  80. httpServer.AddRestHandler("GET", "/assets/", this.assetGetMethod);
  81. httpServer.AddRestHandler("POST", "/assets/", this.assetPostMethod);
  82. httpServer.Start();
  83. }
  84. public string assetPostMethod(string requestBody, string path, string param)
  85. {
  86. AssetBase asset = new AssetBase();
  87. asset.Name = "";
  88. asset.FullID = new LLUUID(param);
  89. Encoding Windows1252Encoding = Encoding.GetEncoding(1252);
  90. byte[] buffer = Windows1252Encoding.GetBytes(requestBody);
  91. asset.Data = buffer;
  92. AssetStorage store = new AssetStorage();
  93. store.Data = asset.Data;
  94. store.Name = asset.Name;
  95. store.UUID = asset.FullID;
  96. db.Set(store);
  97. db.Commit();
  98. return "";
  99. }
  100. public string assetGetMethod(string request, string path, string param)
  101. {
  102. Console.WriteLine("got a request " +param);
  103. byte[] assetdata = getAssetData(new LLUUID(param), false);
  104. if (assetdata != null)
  105. {
  106. Encoding Windows1252Encoding = Encoding.GetEncoding(1252);
  107. string ret = Windows1252Encoding.GetString(assetdata);
  108. //string ret = System.Text.Encoding.Unicode.GetString(assetdata);
  109. return ret;
  110. }
  111. else
  112. {
  113. return "";
  114. }
  115. }
  116. public byte[] getAssetData(LLUUID assetID, bool isTexture)
  117. {
  118. bool found = false;
  119. AssetStorage foundAsset = null;
  120. IObjectSet result = db.Get(new AssetStorage(assetID));
  121. if (result.Count > 0)
  122. {
  123. foundAsset = (AssetStorage)result.Next();
  124. found = true;
  125. }
  126. if (found)
  127. {
  128. return foundAsset.Data;
  129. }
  130. else
  131. {
  132. return null;
  133. }
  134. }
  135. public void setupDB()
  136. {
  137. bool yapfile = System.IO.File.Exists("assets.yap");
  138. try
  139. {
  140. db = Db4oFactory.OpenFile("assets.yap");
  141. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "Main.cs:setupDB() - creation");
  142. }
  143. catch (Exception e)
  144. {
  145. db.Close();
  146. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, "Main.cs:setupDB() - Exception occured");
  147. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, e.ToString());
  148. }
  149. if (!yapfile)
  150. {
  151. this.LoadDB();
  152. }
  153. }
  154. public void LoadDB()
  155. {
  156. try
  157. {
  158. Console.WriteLine("setting up Asset database");
  159. AssetBase Image = new AssetBase();
  160. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000001");
  161. Image.Name = "Bricks";
  162. this.LoadAsset(Image, true, "bricks.jp2");
  163. AssetStorage store = new AssetStorage();
  164. store.Data = Image.Data;
  165. store.Name = Image.Name;
  166. store.UUID = Image.FullID;
  167. db.Set(store);
  168. db.Commit();
  169. Image = new AssetBase();
  170. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000002");
  171. Image.Name = "Plywood";
  172. this.LoadAsset(Image, true, "plywood.jp2");
  173. store = new AssetStorage();
  174. store.Data = Image.Data;
  175. store.Name = Image.Name;
  176. store.UUID = Image.FullID;
  177. db.Set(store);
  178. db.Commit();
  179. Image = new AssetBase();
  180. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000003");
  181. Image.Name = "Rocks";
  182. this.LoadAsset(Image, true, "rocks.jp2");
  183. store = new AssetStorage();
  184. store.Data = Image.Data;
  185. store.Name = Image.Name;
  186. store.UUID = Image.FullID;
  187. db.Set(store);
  188. db.Commit();
  189. Image = new AssetBase();
  190. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000004");
  191. Image.Name = "Granite";
  192. this.LoadAsset(Image, true, "granite.jp2");
  193. store = new AssetStorage();
  194. store.Data = Image.Data;
  195. store.Name = Image.Name;
  196. store.UUID = Image.FullID;
  197. db.Set(store);
  198. db.Commit();
  199. Image = new AssetBase();
  200. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000005");
  201. Image.Name = "Hardwood";
  202. this.LoadAsset(Image, true, "hardwood.jp2");
  203. store = new AssetStorage();
  204. store.Data = Image.Data;
  205. store.Name = Image.Name;
  206. store.UUID = Image.FullID;
  207. db.Set(store);
  208. db.Commit();
  209. Image = new AssetBase();
  210. Image.FullID = new LLUUID("00000000-0000-0000-5005-000000000005");
  211. Image.Name = "Prim Base Texture";
  212. this.LoadAsset(Image, true, "plywood.jp2");
  213. store = new AssetStorage();
  214. store.Data = Image.Data;
  215. store.Name = Image.Name;
  216. store.UUID = Image.FullID;
  217. db.Set(store);
  218. db.Commit();
  219. Image = new AssetBase();
  220. Image.FullID = new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73");
  221. Image.Name = "Shape";
  222. this.LoadAsset(Image, false, "base_shape.dat");
  223. store = new AssetStorage();
  224. store.Data = Image.Data;
  225. store.Name = Image.Name;
  226. store.UUID = Image.FullID;
  227. db.Set(store);
  228. db.Commit();
  229. }
  230. catch (Exception e)
  231. {
  232. Console.WriteLine(e.Message);
  233. }
  234. }
  235. private void LoadAsset(AssetBase info, bool image, string filename)
  236. {
  237. string dataPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "assets"); //+ folder;
  238. string fileName = Path.Combine(dataPath, filename);
  239. FileInfo fInfo = new FileInfo(fileName);
  240. long numBytes = fInfo.Length;
  241. FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  242. byte[] idata = new byte[numBytes];
  243. BinaryReader br = new BinaryReader(fStream);
  244. idata = br.ReadBytes((int)numBytes);
  245. br.Close();
  246. fStream.Close();
  247. info.Data = idata;
  248. //info.loaded=true;
  249. }
  250. /*private GridConfig LoadConfigDll(string dllName)
  251. {
  252. Assembly pluginAssembly = Assembly.LoadFrom(dllName);
  253. GridConfig config = null;
  254. foreach (Type pluginType in pluginAssembly.GetTypes())
  255. {
  256. if (pluginType.IsPublic)
  257. {
  258. if (!pluginType.IsAbstract)
  259. {
  260. Type typeInterface = pluginType.GetInterface("IGridConfig", true);
  261. if (typeInterface != null)
  262. {
  263. IGridConfig plug = (IGridConfig)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  264. config = plug.GetConfigObject();
  265. break;
  266. }
  267. typeInterface = null;
  268. }
  269. }
  270. }
  271. pluginAssembly = null;
  272. return config;
  273. }*/
  274. public void RunCmd(string cmd, string[] cmdparams)
  275. {
  276. switch (cmd)
  277. {
  278. case "help":
  279. m_console.WriteLine(OpenSim.Framework.Console.LogPriority.HIGH, "shutdown - shutdown this asset server (USE CAUTION!)");
  280. break;
  281. case "shutdown":
  282. m_console.Close();
  283. Environment.Exit(0);
  284. break;
  285. }
  286. }
  287. public void Show(string ShowWhat)
  288. {
  289. }
  290. }
  291. }