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