diff options
Diffstat (limited to 'OpenSim/Region/Caps')
-rw-r--r-- | OpenSim/Region/Caps/Caps.cs | 258 | ||||
-rw-r--r-- | OpenSim/Region/Caps/LLSDHelpers.cs | 246 |
2 files changed, 504 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 @@ | |||
1 | using System; | ||
2 | using System.Collections; | ||
3 | using System.Collections.Generic; | ||
4 | using System.Text; | ||
5 | using System.IO; | ||
6 | using System.Xml; | ||
7 | using OpenSim.Servers; | ||
8 | using OpenSim.Framework; | ||
9 | using OpenSim.Framework.Utilities; | ||
10 | using OpenSim.Framework.Types; | ||
11 | using OpenSim.Caches; | ||
12 | using libsecondlife; | ||
13 | |||
14 | namespace 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 | } | ||
diff --git a/OpenSim/Region/Caps/LLSDHelpers.cs b/OpenSim/Region/Caps/LLSDHelpers.cs new file mode 100644 index 0000000..051520c --- /dev/null +++ b/OpenSim/Region/Caps/LLSDHelpers.cs | |||
@@ -0,0 +1,246 @@ | |||
1 | using System; | ||
2 | using System.Collections; | ||
3 | using System.Collections.Generic; | ||
4 | using System.Text; | ||
5 | using System.IO; | ||
6 | using System.Xml; | ||
7 | using libsecondlife; | ||
8 | |||
9 | namespace OpenSim.Framework | ||
10 | { | ||
11 | public class LLSDHelpers | ||
12 | { | ||
13 | public static string SerialiseLLSDReply(object obj) | ||
14 | { | ||
15 | StringWriter sw = new StringWriter(); | ||
16 | XmlTextWriter writer = new XmlTextWriter(sw); | ||
17 | writer.Formatting = Formatting.None; | ||
18 | writer.WriteStartElement(String.Empty, "llsd", String.Empty); | ||
19 | LLSDHelpers.SerializeLLSDType(writer, obj); | ||
20 | writer.WriteEndElement(); | ||
21 | writer.Close(); | ||
22 | return sw.ToString(); | ||
23 | } | ||
24 | |||
25 | public static void SerializeLLSDType(XmlTextWriter writer, object obj) | ||
26 | { | ||
27 | Type myType = obj.GetType(); | ||
28 | LLSDType[] llsdattributes = (LLSDType[])myType.GetCustomAttributes(typeof(LLSDType), false); | ||
29 | if (llsdattributes.Length > 0) | ||
30 | { | ||
31 | switch (llsdattributes[0].ObjectType) | ||
32 | { | ||
33 | case "MAP": | ||
34 | writer.WriteStartElement(String.Empty, "map", String.Empty); | ||
35 | System.Reflection.FieldInfo[] fields = myType.GetFields(); | ||
36 | for (int i = 0; i < fields.Length; i++) | ||
37 | { | ||
38 | object fieldValue = fields[i].GetValue(obj); | ||
39 | LLSDType[] fieldAttributes = (LLSDType[])fieldValue.GetType().GetCustomAttributes(typeof(LLSDType), false); | ||
40 | if (fieldAttributes.Length > 0) | ||
41 | { | ||
42 | writer.WriteStartElement(String.Empty, "key", String.Empty); | ||
43 | writer.WriteString(fields[i].Name); | ||
44 | writer.WriteEndElement(); | ||
45 | SerializeLLSDType(writer, fieldValue); | ||
46 | } | ||
47 | else | ||
48 | { | ||
49 | //Console.WriteLine("LLSD field name" + fields[i].Name + " , " + fields[i].GetValue(obj).GetType()); | ||
50 | writer.WriteStartElement(String.Empty, "key", String.Empty); | ||
51 | writer.WriteString(fields[i].Name); | ||
52 | writer.WriteEndElement(); | ||
53 | LLSD.LLSDWriteOne(writer, fieldValue); | ||
54 | } | ||
55 | } | ||
56 | writer.WriteEndElement(); | ||
57 | break; | ||
58 | case "ARRAY": | ||
59 | // LLSDArray arrayObject = obj as LLSDArray; | ||
60 | // ArrayList a = arrayObject.Array; | ||
61 | ArrayList a = (ArrayList)obj.GetType().GetField("Array").GetValue(obj); | ||
62 | writer.WriteStartElement(String.Empty, "array", String.Empty); | ||
63 | foreach (object item in a) | ||
64 | { | ||
65 | SerializeLLSDType(writer, item); | ||
66 | } | ||
67 | writer.WriteEndElement(); | ||
68 | break; | ||
69 | } | ||
70 | } | ||
71 | else | ||
72 | { | ||
73 | LLSD.LLSDWriteOne(writer, obj); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | public static object DeserialiseLLSDMap(Hashtable llsd, object obj) | ||
78 | { | ||
79 | Type myType = obj.GetType(); | ||
80 | LLSDType[] llsdattributes = (LLSDType[])myType.GetCustomAttributes(typeof(LLSDType), false); | ||
81 | if (llsdattributes.Length > 0) | ||
82 | { | ||
83 | switch (llsdattributes[0].ObjectType) | ||
84 | { | ||
85 | case "MAP": | ||
86 | IDictionaryEnumerator enumerator = llsd.GetEnumerator(); | ||
87 | while (enumerator.MoveNext()) | ||
88 | { | ||
89 | System.Reflection.FieldInfo field = myType.GetField((string)enumerator.Key); | ||
90 | if (field != null) | ||
91 | { | ||
92 | if (enumerator.Value is Hashtable) | ||
93 | { | ||
94 | object fieldValue = field.GetValue(obj); | ||
95 | DeserialiseLLSDMap((Hashtable) enumerator.Value, fieldValue); | ||
96 | } | ||
97 | else if (enumerator.Value is ArrayList) | ||
98 | { | ||
99 | object fieldValue = field.GetValue(obj); | ||
100 | fieldValue.GetType().GetField("Array").SetValue(fieldValue, enumerator.Value); | ||
101 | //TODO | ||
102 | // the LLSD map/array types in the array need to be deserialised | ||
103 | // but first we need to know the right class to deserialise them into. | ||
104 | } | ||
105 | else | ||
106 | { | ||
107 | field.SetValue(obj, enumerator.Value); | ||
108 | } | ||
109 | } | ||
110 | } | ||
111 | break; | ||
112 | } | ||
113 | } | ||
114 | return obj; | ||
115 | } | ||
116 | } | ||
117 | |||
118 | [LLSDType("MAP")] | ||
119 | public class LLSDMapLayerResponse | ||
120 | { | ||
121 | public LLSDMapRequest AgentData = new LLSDMapRequest(); | ||
122 | public LLSDArray LayerData = new LLSDArray(); | ||
123 | |||
124 | public LLSDMapLayerResponse() | ||
125 | { | ||
126 | |||
127 | } | ||
128 | } | ||
129 | |||
130 | [LLSDType("MAP")] | ||
131 | public class LLSDCapsDetails | ||
132 | { | ||
133 | public string MapLayer = ""; | ||
134 | public string NewFileAgentInventory = ""; | ||
135 | //public string EventQueueGet = ""; | ||
136 | |||
137 | public LLSDCapsDetails() | ||
138 | { | ||
139 | |||
140 | } | ||
141 | } | ||
142 | |||
143 | [LLSDType("MAP")] | ||
144 | public class LLSDMapLayer | ||
145 | { | ||
146 | public int Left = 0; | ||
147 | public int Right = 0; | ||
148 | public int Top = 0; | ||
149 | public int Bottom = 0; | ||
150 | public LLUUID ImageID = LLUUID.Zero; | ||
151 | |||
152 | public LLSDMapLayer() | ||
153 | { | ||
154 | |||
155 | } | ||
156 | } | ||
157 | |||
158 | [LLSDType("ARRAY")] | ||
159 | public class LLSDArray | ||
160 | { | ||
161 | public ArrayList Array = new ArrayList(); | ||
162 | |||
163 | public LLSDArray() | ||
164 | { | ||
165 | |||
166 | } | ||
167 | } | ||
168 | |||
169 | [LLSDType("MAP")] | ||
170 | public class LLSDMapRequest | ||
171 | { | ||
172 | public int Flags = 0; | ||
173 | |||
174 | public LLSDMapRequest() | ||
175 | { | ||
176 | |||
177 | } | ||
178 | } | ||
179 | |||
180 | [LLSDType("MAP")] | ||
181 | public class LLSDUploadReply | ||
182 | { | ||
183 | public string new_asset = ""; | ||
184 | public LLUUID new_inventory_item = LLUUID.Zero; | ||
185 | public string state = ""; | ||
186 | |||
187 | public LLSDUploadReply() | ||
188 | { | ||
189 | |||
190 | } | ||
191 | } | ||
192 | |||
193 | [LLSDType("MAP")] | ||
194 | public class LLSDCapEvent | ||
195 | { | ||
196 | public int id = 0; | ||
197 | public LLSDArray events = new LLSDArray(); | ||
198 | |||
199 | public LLSDCapEvent() | ||
200 | { | ||
201 | |||
202 | } | ||
203 | } | ||
204 | |||
205 | [LLSDType("MAP")] | ||
206 | public class LLSDEmpty | ||
207 | { | ||
208 | public LLSDEmpty() | ||
209 | { | ||
210 | |||
211 | } | ||
212 | } | ||
213 | |||
214 | [LLSDType("MAP")] | ||
215 | public class LLSDTest | ||
216 | { | ||
217 | public int Test1 = 20; | ||
218 | public int Test2 = 10; | ||
219 | |||
220 | public LLSDTest() | ||
221 | { | ||
222 | |||
223 | } | ||
224 | } | ||
225 | |||
226 | |||
227 | [AttributeUsage(AttributeTargets.Class)] | ||
228 | public class LLSDType : Attribute | ||
229 | { | ||
230 | private string myType; | ||
231 | |||
232 | public LLSDType(string type) | ||
233 | { | ||
234 | myType = type; | ||
235 | |||
236 | } | ||
237 | |||
238 | public string ObjectType | ||
239 | { | ||
240 | get | ||
241 | { | ||
242 | return myType; | ||
243 | } | ||
244 | } | ||
245 | } | ||
246 | } | ||