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