diff options
Diffstat (limited to 'OpenSim/Region/Capabilities/Caps.cs')
-rw-r--r-- | OpenSim/Region/Capabilities/Caps.cs | 279 |
1 files changed, 279 insertions, 0 deletions
diff --git a/OpenSim/Region/Capabilities/Caps.cs b/OpenSim/Region/Capabilities/Caps.cs new file mode 100644 index 0000000..416a6bc --- /dev/null +++ b/OpenSim/Region/Capabilities/Caps.cs | |||
@@ -0,0 +1,279 @@ | |||
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 System.IO; | ||
33 | using System.Xml; | ||
34 | using OpenSim.Framework.Servers; | ||
35 | using OpenSim.Framework; | ||
36 | using OpenSim.Framework.Utilities; | ||
37 | using OpenSim.Framework.Types; | ||
38 | using OpenSim.Region.Caches; | ||
39 | using libsecondlife; | ||
40 | |||
41 | namespace OpenSim.Region.Capabilities | ||
42 | { | ||
43 | public delegate void UpLoadedTexture(LLUUID assetID, LLUUID inventoryItem, byte[] data); | ||
44 | |||
45 | public class Caps | ||
46 | { | ||
47 | private string httpListenerAddress; | ||
48 | private uint httpListenPort; | ||
49 | private string capsObjectPath = "00001-"; | ||
50 | private string requestPath = "0000/"; | ||
51 | private string mapLayerPath = "0001/"; | ||
52 | private string newInventory = "0002/"; | ||
53 | private string requestTexture = "0003/"; | ||
54 | private string eventQueue = "0100/"; | ||
55 | private BaseHttpServer httpListener; | ||
56 | private LLUUID agentID; | ||
57 | private AssetCache assetCache; | ||
58 | private int eventQueueCount = 1; | ||
59 | private Queue<string> CapsEventQueue = new Queue<string>(); | ||
60 | |||
61 | public Caps(AssetCache assetCach, BaseHttpServer httpServer, string httpListen, uint httpPort, string capsPath, LLUUID agent) | ||
62 | { | ||
63 | assetCache = assetCach; | ||
64 | capsObjectPath = capsPath; | ||
65 | httpListener = httpServer; | ||
66 | httpListenerAddress = httpListen; | ||
67 | httpListenPort = httpPort; | ||
68 | agentID = agent; | ||
69 | } | ||
70 | |||
71 | /// <summary> | ||
72 | /// | ||
73 | /// </summary> | ||
74 | public void RegisterHandlers() | ||
75 | { | ||
76 | Console.WriteLine("registering CAPS handlers"); | ||
77 | httpListener.AddRestHandler("POST", "/CAPS/" + capsObjectPath + requestPath, CapsRequest); | ||
78 | httpListener.AddRestHandler("POST", "/CAPS/" + capsObjectPath + mapLayerPath, MapLayer); | ||
79 | httpListener.AddRestHandler("POST", "/CAPS/" + capsObjectPath + newInventory, NewAgentInventory); | ||
80 | httpListener.AddRestHandler("POST", "/CAPS/" + capsObjectPath + eventQueue, ProcessEventQueue); | ||
81 | } | ||
82 | |||
83 | /// <summary> | ||
84 | /// | ||
85 | /// </summary> | ||
86 | /// <param name="request"></param> | ||
87 | /// <param name="path"></param> | ||
88 | /// <param name="param"></param> | ||
89 | /// <returns></returns> | ||
90 | public string CapsRequest(string request, string path, string param) | ||
91 | { | ||
92 | // Console.WriteLine("Caps Request " + request); | ||
93 | string result = ""; | ||
94 | result = LLSDHelpers.SerialiseLLSDReply(this.GetCapabilities()); | ||
95 | return result; | ||
96 | } | ||
97 | |||
98 | /// <summary> | ||
99 | /// | ||
100 | /// </summary> | ||
101 | /// <returns></returns> | ||
102 | protected LLSDCapsDetails GetCapabilities() | ||
103 | { | ||
104 | /* string capURLS = ""; | ||
105 | capURLS += "<key>MapLayer</key><string>http://" + httpListenerAddress + ":" + httpListenPort.ToString() + "/CAPS/" + capsObjectPath + mapLayerPath + "</string>"; | ||
106 | capURLS += "<key>NewFileAgentInventory</key><string>http://" + httpListenerAddress + ":" + httpListenPort.ToString() + "/CAPS/" + capsObjectPath + newInventory + "</string>"; | ||
107 | //capURLS += "<key>RequestTextureDownload</key><string>http://" + httpListenerAddress + ":" + httpListenPort.ToString() + "/CAPS/" + capsObjectPath + requestTexture + "</string>"; | ||
108 | //capURLS += "<key>EventQueueGet</key><string>http://" + httpListenerAddress + ":" + httpListenPort.ToString() + "/CAPS/" + capsObjectPath + eventQueue + "</string>"; | ||
109 | return capURLS;*/ | ||
110 | |||
111 | LLSDCapsDetails caps = new LLSDCapsDetails(); | ||
112 | caps.MapLayer = "http://" + httpListenerAddress + ":" + httpListenPort.ToString() + "/CAPS/" + capsObjectPath + mapLayerPath; | ||
113 | caps.NewFileAgentInventory = "http://" + httpListenerAddress + ":" + httpListenPort.ToString() + "/CAPS/" + capsObjectPath + newInventory; | ||
114 | return caps; | ||
115 | } | ||
116 | |||
117 | /// <summary> | ||
118 | /// | ||
119 | /// </summary> | ||
120 | /// <param name="request"></param> | ||
121 | /// <param name="path"></param> | ||
122 | /// <param name="param"></param> | ||
123 | /// <returns></returns> | ||
124 | public string MapLayer(string request, string path, string param) | ||
125 | { | ||
126 | Encoding _enc = System.Text.Encoding.UTF8; | ||
127 | Hashtable hash =(Hashtable) LLSD.LLSDDeserialize(_enc.GetBytes(request)); | ||
128 | LLSDMapRequest mapReq = new LLSDMapRequest(); | ||
129 | LLSDHelpers.DeserialiseLLSDMap(hash, mapReq ); | ||
130 | |||
131 | LLSDMapLayerResponse mapResponse= new LLSDMapLayerResponse(); | ||
132 | mapResponse.LayerData.Array.Add(this.BuildLLSDMapLayerResponse()); | ||
133 | string res = LLSDHelpers.SerialiseLLSDReply(mapResponse); | ||
134 | |||
135 | //Console.WriteLine(" Maplayer response is " + res); | ||
136 | |||
137 | return res; | ||
138 | } | ||
139 | |||
140 | /// <summary> | ||
141 | /// | ||
142 | /// </summary> | ||
143 | /// <returns></returns> | ||
144 | protected LLSDMapLayer BuildLLSDMapLayerResponse() | ||
145 | { | ||
146 | LLSDMapLayer mapLayer = new LLSDMapLayer(); | ||
147 | mapLayer.Right = 5000; | ||
148 | mapLayer.Top = 5000; | ||
149 | mapLayer.ImageID = new LLUUID("00000000-0000-0000-9999-000000000006"); | ||
150 | |||
151 | return mapLayer; | ||
152 | } | ||
153 | |||
154 | public string ProcessEventQueue(string request, string path, string param) | ||
155 | { | ||
156 | string res = ""; | ||
157 | |||
158 | if (this.CapsEventQueue.Count > 0) | ||
159 | { | ||
160 | lock (this.CapsEventQueue) | ||
161 | { | ||
162 | string item = CapsEventQueue.Dequeue(); | ||
163 | res = item; | ||
164 | } | ||
165 | } | ||
166 | else | ||
167 | { | ||
168 | res = this.CreateEmptyEventResponse(); | ||
169 | } | ||
170 | return res; | ||
171 | } | ||
172 | |||
173 | public string CreateEstablishAgentComms(string caps, string ipAddressPort) | ||
174 | { | ||
175 | string res = "<llsd><map><key>id</key><integer>" + eventQueueCount + "</integer>"; | ||
176 | res += "<key>events</key><array><map>"; | ||
177 | res += "<key>message</key><string>EstablishAgentCommunication</string>"; | ||
178 | res += "<key>body</key><map>"; | ||
179 | res += "<key>sim-ip-and-port</key><string>" + ipAddressPort + "</string>"; | ||
180 | res += "<key>seed-capability</key><string>" + caps + "</string>"; | ||
181 | res += "<key>agent-id</key><uuid>" + this.agentID.ToStringHyphenated() + "</uuid>"; | ||
182 | res += "</map>"; | ||
183 | res += "</map></array>"; | ||
184 | res += "</map></llsd>"; | ||
185 | eventQueueCount++; | ||
186 | this.CapsEventQueue.Enqueue(res); | ||
187 | return res; | ||
188 | } | ||
189 | |||
190 | public string CreateEmptyEventResponse() | ||
191 | { | ||
192 | string res = "<llsd><map><key>id</key><integer>" + eventQueueCount + "</integer>"; | ||
193 | res += "<key>events</key><array><map>"; | ||
194 | res += "</map></array>"; | ||
195 | res += "</map></llsd>"; | ||
196 | eventQueueCount++; | ||
197 | return res; | ||
198 | } | ||
199 | |||
200 | public string NewAgentInventory(string request, string path, string param) | ||
201 | { | ||
202 | //Console.WriteLine("received upload request:"+ request); | ||
203 | string res = ""; | ||
204 | LLUUID newAsset = LLUUID.Random(); | ||
205 | LLUUID newInvItem = LLUUID.Random(); | ||
206 | string uploaderPath = capsObjectPath + Util.RandomClass.Next(5000, 8000).ToString("0000"); | ||
207 | AssetUploader uploader = new AssetUploader(newAsset, newInvItem, uploaderPath, this.httpListener); | ||
208 | httpListener.AddRestHandler("POST", "/CAPS/" + uploaderPath, uploader.uploaderCaps); | ||
209 | string uploaderURL = "http://" + httpListenerAddress + ":" + httpListenPort.ToString() + "/CAPS/" + uploaderPath; | ||
210 | //Console.WriteLine("uploader url is " + uploaderURL); | ||
211 | res += "<llsd><map>"; | ||
212 | res += "<key>uploader</key><string>" + uploaderURL + "</string>"; | ||
213 | //res += "<key>success</key><boolean>true</boolean>"; | ||
214 | res += "<key>state</key><string>upload</string>"; | ||
215 | res += "</map></llsd>"; | ||
216 | uploader.OnUpLoad += this.UploadHandler; | ||
217 | return res; | ||
218 | } | ||
219 | |||
220 | public void UploadHandler(LLUUID assetID, LLUUID inventoryItem, byte[] data) | ||
221 | { | ||
222 | // Console.WriteLine("upload handler called"); | ||
223 | AssetBase asset; | ||
224 | asset = new AssetBase(); | ||
225 | asset.FullID = assetID; | ||
226 | asset.Type = 0; | ||
227 | asset.InvType = 0; | ||
228 | asset.Name = "UploadedTexture" + Util.RandomClass.Next(1, 1000).ToString("000"); | ||
229 | asset.Data = data; | ||
230 | this.assetCache.AddAsset(asset); | ||
231 | } | ||
232 | |||
233 | public class AssetUploader | ||
234 | { | ||
235 | public event UpLoadedTexture OnUpLoad; | ||
236 | |||
237 | private string uploaderPath = ""; | ||
238 | private LLUUID newAssetID; | ||
239 | private LLUUID inventoryItemID; | ||
240 | private BaseHttpServer httpListener; | ||
241 | public AssetUploader(LLUUID assetID, LLUUID inventoryItem, string path, BaseHttpServer httpServer) | ||
242 | { | ||
243 | newAssetID = assetID; | ||
244 | inventoryItemID = inventoryItem; | ||
245 | uploaderPath = path; | ||
246 | httpListener = httpServer; | ||
247 | |||
248 | } | ||
249 | |||
250 | public string uploaderCaps(string request, string path, string param) | ||
251 | { | ||
252 | Encoding _enc = System.Text.Encoding.UTF8; | ||
253 | byte[] data = _enc.GetBytes(request); | ||
254 | //Console.WriteLine("recieved upload " + Util.FieldToString(data)); | ||
255 | LLUUID inv = this.inventoryItemID; | ||
256 | string res = ""; | ||
257 | res += "<llsd><map>"; | ||
258 | res += "<key>new_asset</key><string>" + newAssetID.ToStringHyphenated() + "</string>"; | ||
259 | res += "<key>new_inventory_item</key><uuid>" + inv.ToStringHyphenated() + "</uuid>"; | ||
260 | res += "<key>state</key><string>complete</string>"; | ||
261 | res += "</map></llsd>"; | ||
262 | |||
263 | // Console.WriteLine("asset " + newAssetID.ToStringHyphenated() + " , inventory item " + inv.ToStringHyphenated()); | ||
264 | httpListener.RemoveRestHandler("POST", "/CAPS/" + uploaderPath); | ||
265 | if (OnUpLoad != null) | ||
266 | { | ||
267 | OnUpLoad(newAssetID, inv, data); | ||
268 | } | ||
269 | |||
270 | /*FileStream fs = File.Create("upload.jp2"); | ||
271 | BinaryWriter bw = new BinaryWriter(fs); | ||
272 | bw.Write(data); | ||
273 | bw.Close(); | ||
274 | fs.Close();*/ | ||
275 | return res; | ||
276 | } | ||
277 | } | ||
278 | } | ||
279 | } | ||