Caps.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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.Collections;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using libsecondlife;
  33. using OpenSim.Framework.Servers;
  34. using OpenSim.Framework.Types;
  35. using OpenSim.Framework.Utilities;
  36. using OpenSim.Region.Caches;
  37. namespace OpenSim.Region.Capabilities
  38. {
  39. public delegate void UpLoadedTexture(LLUUID assetID, LLUUID inventoryItem, byte[] data);
  40. public class Caps
  41. {
  42. private string m_httpListenerHostName;
  43. private int m_httpListenPort;
  44. private string m_capsObjectPath = "00001-";
  45. private string m_requestPath = "0000/";
  46. private string m_mapLayerPath = "0001/";
  47. private string m_newInventory = "0002/";
  48. private string m_requestTexture = "0003/";
  49. private string eventQueue = "0100/";
  50. private BaseHttpServer httpListener;
  51. private LLUUID agentID;
  52. private AssetCache assetCache;
  53. private int eventQueueCount = 1;
  54. private Queue<string> CapsEventQueue = new Queue<string>();
  55. public Caps(AssetCache assetCach, BaseHttpServer httpServer, string httpListen, int httpPort, string capsPath, LLUUID agent)
  56. {
  57. assetCache = assetCach;
  58. m_capsObjectPath = capsPath;
  59. httpListener = httpServer;
  60. m_httpListenerHostName = httpListen;
  61. m_httpListenPort = httpPort;
  62. agentID = agent;
  63. }
  64. /// <summary>
  65. ///
  66. /// </summary>
  67. public void RegisterHandlers()
  68. {
  69. Console.WriteLine("registering CAPS handlers");
  70. string capsBase = "/CAPS/" + m_capsObjectPath;
  71. //AddLegacyCapsHandler( httpListener, m_mapLayerPath, MapLayer);
  72. httpListener.AddStreamHandler(
  73. new LLSDStreamhandler<LLSDMapRequest, LLSDMapLayerResponse>("POST", capsBase + m_mapLayerPath, this.GetMapLayer ));
  74. AddLegacyCapsHandler(httpListener, m_requestPath, CapsRequest);
  75. AddLegacyCapsHandler(httpListener, m_newInventory, NewAgentInventory);
  76. AddLegacyCapsHandler( httpListener, eventQueue, ProcessEventQueue);
  77. AddLegacyCapsHandler( httpListener, m_requestTexture, RequestTexture);
  78. }
  79. [Obsolete("Use BaseHttpServer.AddStreamHandler(new LLSDStreamHandler( LLSDMethod delegate )) instead.")]
  80. private void AddLegacyCapsHandler(BaseHttpServer httpListener, string path, RestMethod restMethod)
  81. {
  82. string capsBase = "/CAPS/" + m_capsObjectPath;
  83. httpListener.AddStreamHandler(new RestStreamHandler("POST", capsBase + path, restMethod));
  84. }
  85. /// <summary>
  86. ///
  87. /// </summary>
  88. /// <param name="request"></param>
  89. /// <param name="path"></param>
  90. /// <param name="param"></param>
  91. /// <returns></returns>
  92. public string CapsRequest(string request, string path, string param)
  93. {
  94. // Console.WriteLine("Caps Request " + request);
  95. string result = "";
  96. result = LLSDHelpers.SerialiseLLSDReply(this.GetCapabilities());
  97. return result;
  98. }
  99. /// <summary>
  100. ///
  101. /// </summary>
  102. /// <returns></returns>
  103. protected LLSDCapsDetails GetCapabilities()
  104. {
  105. LLSDCapsDetails caps = new LLSDCapsDetails();
  106. string capsBaseUrl = "http://" + m_httpListenerHostName + ":" + m_httpListenPort.ToString() + "/CAPS/" + m_capsObjectPath;
  107. caps.MapLayer = capsBaseUrl + m_mapLayerPath;
  108. // caps.NewFileAgentInventory = capsBaseUrl + m_newInventory;
  109. return caps;
  110. }
  111. /// <summary>
  112. ///
  113. /// </summary>
  114. /// <param name="request"></param>
  115. /// <param name="path"></param>
  116. /// <param name="param"></param>
  117. /// <returns></returns>
  118. public string MapLayer(string request, string path, string param)
  119. {
  120. Encoding _enc = Encoding.UTF8;
  121. Hashtable hash = (Hashtable)LLSD.LLSDDeserialize(_enc.GetBytes(request));
  122. LLSDMapRequest mapReq = new LLSDMapRequest();
  123. LLSDHelpers.DeserialiseLLSDMap(hash, mapReq);
  124. LLSDMapLayerResponse mapResponse = new LLSDMapLayerResponse();
  125. mapResponse.LayerData.Array.Add(this.BuildLLSDMapLayerResponse());
  126. string res = LLSDHelpers.SerialiseLLSDReply(mapResponse);
  127. return res;
  128. }
  129. public LLSDMapLayerResponse GetMapLayer(LLSDMapRequest mapReq)
  130. {
  131. LLSDMapLayerResponse mapResponse = new LLSDMapLayerResponse();
  132. mapResponse.LayerData.Array.Add(this.BuildLLSDMapLayerResponse());
  133. return mapResponse;
  134. }
  135. /// <summary>
  136. ///
  137. /// </summary>
  138. /// <returns></returns>
  139. protected LLSDMapLayer BuildLLSDMapLayerResponse()
  140. {
  141. LLSDMapLayer mapLayer = new LLSDMapLayer();
  142. mapLayer.Right = 5000;
  143. mapLayer.Top = 5000;
  144. mapLayer.ImageID = new LLUUID("00000000-0000-0000-9999-000000000006");
  145. return mapLayer;
  146. }
  147. /// <summary>
  148. ///
  149. /// </summary>
  150. /// <param name="request"></param>
  151. /// <param name="path"></param>
  152. /// <param name="param"></param>
  153. /// <returns></returns>
  154. public string RequestTexture(string request, string path, string param)
  155. {
  156. // Needs implementing (added to remove compiler warning)
  157. return "";
  158. }
  159. /// <summary>
  160. ///
  161. /// </summary>
  162. /// <param name="request"></param>
  163. /// <param name="path"></param>
  164. /// <param name="param"></param>
  165. /// <returns></returns>
  166. public string ProcessEventQueue(string request, string path, string param)
  167. {
  168. string res = "";
  169. if (this.CapsEventQueue.Count > 0)
  170. {
  171. lock (this.CapsEventQueue)
  172. {
  173. string item = CapsEventQueue.Dequeue();
  174. res = item;
  175. }
  176. }
  177. else
  178. {
  179. res = this.CreateEmptyEventResponse();
  180. }
  181. return res;
  182. }
  183. /// <summary>
  184. ///
  185. /// </summary>
  186. /// <param name="caps"></param>
  187. /// <param name="ipAddressPort"></param>
  188. /// <returns></returns>
  189. public string CreateEstablishAgentComms(string caps, string ipAddressPort)
  190. {
  191. LLSDCapEvent eventItem = new LLSDCapEvent();
  192. eventItem.id = eventQueueCount;
  193. //should be creating a EstablishAgentComms item, but there isn't a class for it yet
  194. eventItem.events.Array.Add(new LLSDEmpty());
  195. string res = LLSDHelpers.SerialiseLLSDReply(eventItem);
  196. eventQueueCount++;
  197. this.CapsEventQueue.Enqueue(res);
  198. return res;
  199. }
  200. /// <summary>
  201. ///
  202. /// </summary>
  203. /// <returns></returns>
  204. public string CreateEmptyEventResponse()
  205. {
  206. LLSDCapEvent eventItem = new LLSDCapEvent();
  207. eventItem.id = eventQueueCount;
  208. eventItem.events.Array.Add(new LLSDEmpty());
  209. string res = LLSDHelpers.SerialiseLLSDReply(eventItem);
  210. eventQueueCount++;
  211. return res;
  212. }
  213. /// <summary>
  214. ///
  215. /// </summary>
  216. /// <param name="request"></param>
  217. /// <param name="path"></param>
  218. /// <param name="param"></param>
  219. /// <returns></returns>
  220. public string NewAgentInventory(string request, string path, string param)
  221. {
  222. //Console.WriteLine("received upload request:"+ request);
  223. string res = "";
  224. LLUUID newAsset = LLUUID.Random();
  225. LLUUID newInvItem = LLUUID.Random();
  226. string uploaderPath = Util.RandomClass.Next(5000, 8000).ToString("0000");
  227. AssetUploader uploader = new AssetUploader(newAsset, newInvItem, uploaderPath, this.httpListener);
  228. AddLegacyCapsHandler( httpListener, uploaderPath, uploader.uploaderCaps);
  229. string uploaderURL = "http://" + m_httpListenerHostName + ":" + m_httpListenPort.ToString() + "/CAPS/" + uploaderPath;
  230. //Console.WriteLine("uploader url is " + uploaderURL);
  231. res += "<llsd><map>";
  232. res += "<key>uploader</key><string>" + uploaderURL + "</string>";
  233. //res += "<key>success</key><boolean>true</boolean>";
  234. res += "<key>state</key><string>upload</string>";
  235. res += "</map></llsd>";
  236. uploader.OnUpLoad += this.UploadHandler;
  237. return res;
  238. }
  239. /// <summary>
  240. ///
  241. /// </summary>
  242. /// <param name="assetID"></param>
  243. /// <param name="inventoryItem"></param>
  244. /// <param name="data"></param>
  245. public void UploadHandler(LLUUID assetID, LLUUID inventoryItem, byte[] data)
  246. {
  247. // Console.WriteLine("upload handler called");
  248. AssetBase asset;
  249. asset = new AssetBase();
  250. asset.FullID = assetID;
  251. asset.Type = 0;
  252. asset.InvType = 0;
  253. asset.Name = "UploadedTexture" + Util.RandomClass.Next(1, 1000).ToString("000");
  254. asset.Data = data;
  255. this.assetCache.AddAsset(asset);
  256. }
  257. public class AssetUploader
  258. {
  259. public event UpLoadedTexture OnUpLoad;
  260. private string uploaderPath = "";
  261. private LLUUID newAssetID;
  262. private LLUUID inventoryItemID;
  263. private BaseHttpServer httpListener;
  264. public AssetUploader(LLUUID assetID, LLUUID inventoryItem, string path, BaseHttpServer httpServer)
  265. {
  266. newAssetID = assetID;
  267. inventoryItemID = inventoryItem;
  268. uploaderPath = path;
  269. httpListener = httpServer;
  270. }
  271. public string uploaderCaps(string request, string path, string param)
  272. {
  273. Encoding _enc = Encoding.UTF8;
  274. byte[] data = _enc.GetBytes(request);
  275. //Console.WriteLine("recieved upload " + Util.FieldToString(data));
  276. LLUUID inv = this.inventoryItemID;
  277. string res = "";
  278. res += "<llsd><map>";
  279. res += "<key>new_asset</key><string>" + newAssetID.ToStringHyphenated() + "</string>";
  280. res += "<key>new_inventory_item</key><uuid>" + inv.ToStringHyphenated() + "</uuid>";
  281. res += "<key>state</key><string>complete</string>";
  282. res += "</map></llsd>";
  283. // Console.WriteLine("asset " + newAssetID.ToStringHyphenated() + " , inventory item " + inv.ToStringHyphenated());
  284. httpListener.RemoveStreamHandler("POST", "/CAPS/" + uploaderPath);
  285. if (OnUpLoad != null)
  286. {
  287. OnUpLoad(newAssetID, inv, data);
  288. }
  289. /*FileStream fs = File.Create("upload.jp2");
  290. BinaryWriter bw = new BinaryWriter(fs);
  291. bw.Write(data);
  292. bw.Close();
  293. fs.Close();*/
  294. return res;
  295. }
  296. }
  297. }
  298. }