LocalAssetServer.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5. using System.IO;
  6. using OpenSim.Framework.Interfaces;
  7. using OpenSim.Framework.Types;
  8. using OpenSim.Framework.Utilities;
  9. using OpenSim.Framework.Console;
  10. using libsecondlife;
  11. using Db4objects.Db4o;
  12. using Db4objects.Db4o.Query;
  13. namespace OpenSim.GridInterfaces.Local
  14. {
  15. public class LocalAssetPlugin : IAssetPlugin
  16. {
  17. public LocalAssetPlugin()
  18. {
  19. }
  20. public IAssetServer GetAssetServer()
  21. {
  22. return (new LocalAssetServer());
  23. }
  24. }
  25. public class LocalAssetServer : IAssetServer
  26. {
  27. private IAssetReceiver _receiver;
  28. private BlockingQueue<ARequest> _assetRequests;
  29. private IObjectContainer db;
  30. private Thread _localAssetServerThread;
  31. public LocalAssetServer()
  32. {
  33. bool yapfile;
  34. this._assetRequests = new BlockingQueue<ARequest>();
  35. yapfile = System.IO.File.Exists("assets.yap");
  36. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.VERBOSE,"Local Asset Server class created");
  37. try
  38. {
  39. db = Db4oFactory.OpenFile("assets.yap");
  40. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.VERBOSE,"Db4 Asset database creation");
  41. }
  42. catch (Exception e)
  43. {
  44. db.Close();
  45. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.MEDIUM,"Db4 Asset server :Constructor - Exception occured");
  46. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, e.ToString());
  47. }
  48. if (!yapfile)
  49. {
  50. this.SetUpAssetDatabase();
  51. }
  52. this._localAssetServerThread = new Thread(new ThreadStart(RunRequests));
  53. this._localAssetServerThread.IsBackground = true;
  54. this._localAssetServerThread.Start();
  55. }
  56. public void SetReceiver(IAssetReceiver receiver)
  57. {
  58. this._receiver = receiver;
  59. }
  60. public void RequestAsset(LLUUID assetID, bool isTexture)
  61. {
  62. ARequest req = new ARequest();
  63. req.AssetID = assetID;
  64. req.IsTexture = isTexture;
  65. this._assetRequests.Enqueue(req);
  66. }
  67. public void UpdateAsset(AssetBase asset)
  68. {
  69. }
  70. public void UploadNewAsset(AssetBase asset)
  71. {
  72. AssetStorage store = new AssetStorage();
  73. store.Data = asset.Data;
  74. store.Name = asset.Name;
  75. store.UUID = asset.FullID;
  76. db.Set(store);
  77. db.Commit();
  78. }
  79. public void SetServerInfo(string ServerUrl, string ServerKey)
  80. {
  81. }
  82. public void Close()
  83. {
  84. if (db != null)
  85. {
  86. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.VERBOSE, "Closing local asset server database");
  87. db.Close();
  88. }
  89. }
  90. private void RunRequests()
  91. {
  92. while (true)
  93. {
  94. byte[] idata = null;
  95. bool found = false;
  96. AssetStorage foundAsset = null;
  97. ARequest req = this._assetRequests.Dequeue();
  98. IObjectSet result = db.Query(new AssetUUIDQuery(req.AssetID));
  99. if (result.Count > 0)
  100. {
  101. foundAsset = (AssetStorage)result.Next();
  102. found = true;
  103. }
  104. AssetBase asset = new AssetBase();
  105. if (found)
  106. {
  107. asset.FullID = foundAsset.UUID;
  108. asset.Type = foundAsset.Type;
  109. asset.InvType = foundAsset.Type;
  110. asset.Name = foundAsset.Name;
  111. idata = foundAsset.Data;
  112. }
  113. else
  114. {
  115. asset.FullID = LLUUID.Zero;
  116. }
  117. asset.Data = idata;
  118. _receiver.AssetReceived(asset, req.IsTexture);
  119. }
  120. }
  121. private void SetUpAssetDatabase()
  122. {
  123. try
  124. {
  125. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(LogPriority.VERBOSE, "Setting up asset database");
  126. AssetBase Image = new AssetBase();
  127. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000001");
  128. Image.Name = "Bricks";
  129. this.LoadAsset(Image, true, "bricks.jp2");
  130. AssetStorage store = new AssetStorage();
  131. store.Data = Image.Data;
  132. store.Name = Image.Name;
  133. store.UUID = Image.FullID;
  134. db.Set(store);
  135. db.Commit();
  136. Image = new AssetBase();
  137. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000002");
  138. Image.Name = "Plywood";
  139. this.LoadAsset(Image, true, "plywood.jp2");
  140. store = new AssetStorage();
  141. store.Data = Image.Data;
  142. store.Name = Image.Name;
  143. store.UUID = Image.FullID;
  144. db.Set(store);
  145. db.Commit();
  146. Image = new AssetBase();
  147. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000003");
  148. Image.Name = "Rocks";
  149. this.LoadAsset(Image, true, "rocks.jp2");
  150. store = new AssetStorage();
  151. store.Data = Image.Data;
  152. store.Name = Image.Name;
  153. store.UUID = Image.FullID;
  154. db.Set(store);
  155. db.Commit();
  156. Image = new AssetBase();
  157. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000004");
  158. Image.Name = "Granite";
  159. this.LoadAsset(Image, true, "granite.jp2");
  160. store = new AssetStorage();
  161. store.Data = Image.Data;
  162. store.Name = Image.Name;
  163. store.UUID = Image.FullID;
  164. db.Set(store);
  165. db.Commit();
  166. Image = new AssetBase();
  167. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000005");
  168. Image.Name = "Hardwood";
  169. this.LoadAsset(Image, true, "hardwood.jp2");
  170. store = new AssetStorage();
  171. store.Data = Image.Data;
  172. store.Name = Image.Name;
  173. store.UUID = Image.FullID;
  174. db.Set(store);
  175. db.Commit();
  176. Image = new AssetBase();
  177. Image.FullID = new LLUUID("00000000-0000-0000-5005-000000000005");
  178. Image.Name = "Prim Base Texture";
  179. this.LoadAsset(Image, true, "plywood.jp2");
  180. store = new AssetStorage();
  181. store.Data = Image.Data;
  182. store.Name = Image.Name;
  183. store.UUID = Image.FullID;
  184. db.Set(store);
  185. db.Commit();
  186. Image = new AssetBase();
  187. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000006");
  188. Image.Name = "Map Base Texture";
  189. this.LoadAsset(Image, true, "map_base.jp2");
  190. store = new AssetStorage();
  191. store.Data = Image.Data;
  192. store.Name = Image.Name;
  193. store.UUID = Image.FullID;
  194. db.Set(store);
  195. db.Commit();
  196. Image = new AssetBase();
  197. Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000007");
  198. Image.Name = "Map Texture";
  199. this.LoadAsset(Image, true, "map1.jp2");
  200. store = new AssetStorage();
  201. store.Data = Image.Data;
  202. store.Name = Image.Name;
  203. store.UUID = Image.FullID;
  204. db.Set(store);
  205. db.Commit();
  206. Image = new AssetBase();
  207. Image.FullID = new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73");
  208. Image.Name = "Shape";
  209. this.LoadAsset(Image, false, "base_shape.dat");
  210. store = new AssetStorage();
  211. store.Data = Image.Data;
  212. store.Name = Image.Name;
  213. store.UUID = Image.FullID;
  214. db.Set(store);
  215. db.Commit();
  216. }
  217. catch (Exception e)
  218. {
  219. Console.WriteLine(e.Message);
  220. }
  221. }
  222. private void LoadAsset(AssetBase info, bool image, string filename)
  223. {
  224. //should request Asset from storage manager
  225. //but for now read from file
  226. string dataPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "assets"); //+ folder;
  227. string fileName = Path.Combine(dataPath, filename);
  228. FileInfo fInfo = new FileInfo(fileName);
  229. long numBytes = fInfo.Length;
  230. FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
  231. byte[] idata = new byte[numBytes];
  232. BinaryReader br = new BinaryReader(fStream);
  233. idata = br.ReadBytes((int)numBytes);
  234. br.Close();
  235. fStream.Close();
  236. info.Data = idata;
  237. //info.loaded=true;
  238. }
  239. }
  240. }