aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenGridServices/OpenGridServices.GridServer/GridManager.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenGridServices/OpenGridServices.GridServer/GridManager.cs')
-rw-r--r--OpenGridServices/OpenGridServices.GridServer/GridManager.cs474
1 files changed, 474 insertions, 0 deletions
diff --git a/OpenGridServices/OpenGridServices.GridServer/GridManager.cs b/OpenGridServices/OpenGridServices.GridServer/GridManager.cs
new file mode 100644
index 0000000..54e4bb7
--- /dev/null
+++ b/OpenGridServices/OpenGridServices.GridServer/GridManager.cs
@@ -0,0 +1,474 @@
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Text;
5using System.Reflection;
6using OpenGrid.Framework.Data;
7using OpenSim.Framework.Utilities;
8using OpenSim.Framework.Console;
9using OpenSim.Framework.Sims;
10using libsecondlife;
11using Nwc.XmlRpc;
12using System.Xml;
13
14namespace OpenGridServices.GridServer
15{
16 class GridManager
17 {
18 Dictionary<string, IGridData> _plugins = new Dictionary<string, IGridData>();
19 public OpenSim.Framework.Interfaces.GridConfig config;
20
21 /// <summary>
22 /// Adds a new grid server plugin - grid servers will be requested in the order they were loaded.
23 /// </summary>
24 /// <param name="FileName">The filename to the grid server plugin DLL</param>
25 public void AddPlugin(string FileName)
26 {
27 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"Storage: Attempting to load " + FileName);
28 Assembly pluginAssembly = Assembly.LoadFrom(FileName);
29
30 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"Storage: Found " + pluginAssembly.GetTypes().Length + " interfaces.");
31 foreach (Type pluginType in pluginAssembly.GetTypes())
32 {
33 if (!pluginType.IsAbstract)
34 {
35 Type typeInterface = pluginType.GetInterface("IGridData", true);
36
37 if (typeInterface != null)
38 {
39 IGridData plug = (IGridData)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
40 plug.Initialise();
41 this._plugins.Add(plug.getName(), plug);
42 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"Storage: Added IGridData Interface");
43 }
44
45 typeInterface = null;
46 }
47 }
48
49 pluginAssembly = null;
50 }
51
52 /// <summary>
53 /// Returns a region by argument
54 /// </summary>
55 /// <param name="uuid">A UUID key of the region to return</param>
56 /// <returns>A SimProfileData for the region</returns>
57 public SimProfileData getRegion(libsecondlife.LLUUID uuid)
58 {
59 foreach(KeyValuePair<string,IGridData> kvp in _plugins) {
60 try
61 {
62 return kvp.Value.GetProfileByLLUUID(uuid);
63 }
64 catch (Exception e)
65 {
66 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.NORMAL,"Storage: Unable to find region " + uuid.ToStringHyphenated() + " via " + kvp.Key);
67 }
68 }
69 return null;
70 }
71
72 /// <summary>
73 /// Returns a region by argument
74 /// </summary>
75 /// <param name="uuid">A regionHandle of the region to return</param>
76 /// <returns>A SimProfileData for the region</returns>
77 public SimProfileData getRegion(ulong handle)
78 {
79 foreach (KeyValuePair<string, IGridData> kvp in _plugins)
80 {
81 try
82 {
83 return kvp.Value.GetProfileByHandle(handle);
84 }
85 catch (Exception e)
86 {
87 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.NORMAL,"Storage: Unable to find region " + handle.ToString() + " via " + kvp.Key);
88 }
89 }
90 return null;
91 }
92
93 public Dictionary<ulong, SimProfileData> getRegions(uint xmin, uint ymin, uint xmax, uint ymax)
94 {
95 Dictionary<ulong, SimProfileData> regions = new Dictionary<ulong, SimProfileData>();
96
97 SimProfileData[] neighbours;
98
99 foreach (KeyValuePair<string, IGridData> kvp in _plugins)
100 {
101 try
102 {
103 neighbours = kvp.Value.GetProfilesInRange(xmin, ymin, xmax, ymax);
104 foreach (SimProfileData neighbour in neighbours)
105 {
106 regions[neighbour.regionHandle] = neighbour;
107 }
108 }
109 catch (Exception e)
110 {
111 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.NORMAL, "Storage: Unable to query regionblock via " + kvp.Key);
112 }
113 }
114
115 return regions;
116 }
117
118 /// <summary>
119 /// Returns a XML String containing a list of the neighbouring regions
120 /// </summary>
121 /// <param name="reqhandle">The regionhandle for the center sim</param>
122 /// <returns>An XML string containing neighbour entities</returns>
123 public string GetXMLNeighbours(ulong reqhandle)
124 {
125 string response = "";
126 SimProfileData central_region = getRegion(reqhandle);
127 SimProfileData neighbour;
128 for (int x = -1; x < 2; x++) for (int y = -1; y < 2; y++)
129 {
130 if (getRegion(Util.UIntsToLong((uint)((central_region.regionLocX + x) * 256), (uint)(central_region.regionLocY + y) * 256)) != null)
131 {
132 neighbour = getRegion(Util.UIntsToLong((uint)((central_region.regionLocX + x) * 256), (uint)(central_region.regionLocY + y) * 256));
133 response += "<neighbour>";
134 response += "<sim_ip>" + neighbour.serverIP + "</sim_ip>";
135 response += "<sim_port>" + neighbour.serverPort.ToString() + "</sim_port>";
136 response += "<locx>" + neighbour.regionLocX.ToString() + "</locx>";
137 response += "<locy>" + neighbour.regionLocY.ToString() + "</locy>";
138 response += "<regionhandle>" + neighbour.regionHandle.ToString() + "</regionhandle>";
139 response += "</neighbour>";
140
141 }
142 }
143 return response;
144 }
145
146 /// <summary>
147 /// Performed when a region connects to the grid server initially.
148 /// </summary>
149 /// <param name="request">The XMLRPC Request</param>
150 /// <returns>Startup parameters</returns>
151 public XmlRpcResponse XmlRpcLoginToSimulatorMethod(XmlRpcRequest request)
152 {
153 XmlRpcResponse response = new XmlRpcResponse();
154 Hashtable responseData = new Hashtable();
155 response.Value = responseData;
156
157 SimProfileData TheSim = null;
158 Hashtable requestData = (Hashtable)request.Params[0];
159
160 if (requestData.ContainsKey("UUID"))
161 {
162 TheSim = getRegion(new LLUUID((string)requestData["UUID"]));
163 }
164 else if (requestData.ContainsKey("region_handle"))
165 {
166 TheSim = getRegion((ulong)Convert.ToUInt64(requestData["region_handle"]));
167 }
168
169 if (TheSim == null)
170 {
171 responseData["error"] = "sim not found";
172 }
173 else
174 {
175
176 ArrayList SimNeighboursData = new ArrayList();
177
178 SimProfileData neighbour;
179 Hashtable NeighbourBlock;
180
181 bool fastMode = false; // Only compatible with MySQL right now
182
183 if (fastMode)
184 {
185 Dictionary<ulong, SimProfileData> neighbours = getRegions(TheSim.regionLocX - 1, TheSim.regionLocY - 1, TheSim.regionLocX + 1, TheSim.regionLocY + 1);
186
187 foreach (KeyValuePair<ulong, SimProfileData> aSim in neighbours)
188 {
189 NeighbourBlock = new Hashtable();
190 NeighbourBlock["sim_ip"] = aSim.Value.serverIP.ToString();
191 NeighbourBlock["sim_port"] = aSim.Value.serverPort.ToString();
192 NeighbourBlock["region_locx"] = aSim.Value.regionLocX.ToString();
193 NeighbourBlock["region_locy"] = aSim.Value.regionLocY.ToString();
194 NeighbourBlock["UUID"] = aSim.Value.UUID.ToString();
195
196 if (aSim.Value.UUID != TheSim.UUID)
197 SimNeighboursData.Add(NeighbourBlock);
198 }
199 }
200 else
201 {
202 for (int x = -1; x < 2; x++) for (int y = -1; y < 2; y++)
203 {
204 if (getRegion(Helpers.UIntsToLong((uint)((TheSim.regionLocX + x) * 256), (uint)(TheSim.regionLocY + y) * 256)) != null)
205 {
206 neighbour = getRegion(Helpers.UIntsToLong((uint)((TheSim.regionLocX + x) * 256), (uint)(TheSim.regionLocY + y) * 256));
207
208 NeighbourBlock = new Hashtable();
209 NeighbourBlock["sim_ip"] = neighbour.serverIP;
210 NeighbourBlock["sim_port"] = neighbour.serverPort.ToString();
211 NeighbourBlock["region_locx"] = neighbour.regionLocX.ToString();
212 NeighbourBlock["region_locy"] = neighbour.regionLocY.ToString();
213 NeighbourBlock["UUID"] = neighbour.UUID.ToString();
214
215 if (neighbour.UUID != TheSim.UUID) SimNeighboursData.Add(NeighbourBlock);
216 }
217 }
218 }
219
220 responseData["UUID"] = TheSim.UUID.ToString();
221 responseData["region_locx"] = TheSim.regionLocX.ToString();
222 responseData["region_locy"] = TheSim.regionLocY.ToString();
223 responseData["regionname"] = TheSim.regionName;
224 responseData["estate_id"] = "1";
225 responseData["neighbours"] = SimNeighboursData;
226
227 responseData["sim_ip"] = TheSim.serverIP;
228 responseData["sim_port"] = TheSim.serverPort.ToString();
229 responseData["asset_url"] = TheSim.regionAssetURI;
230 responseData["asset_sendkey"] = TheSim.regionAssetSendKey;
231 responseData["asset_recvkey"] = TheSim.regionAssetRecvKey;
232 responseData["user_url"] = TheSim.regionUserURI;
233 responseData["user_sendkey"] = TheSim.regionUserSendKey;
234 responseData["user_recvkey"] = TheSim.regionUserRecvKey;
235 responseData["authkey"] = TheSim.regionSecret;
236
237 // New! If set, use as URL to local sim storage (ie http://remotehost/region.yap)
238 responseData["data_uri"] = TheSim.regionDataURI;
239 }
240
241 return response;
242 }
243
244 public XmlRpcResponse XmlRpcMapBlockMethod(XmlRpcRequest request)
245 {
246 int xmin=980, ymin=980, xmax=1020, ymax=1020;
247
248 Hashtable requestData = (Hashtable)request.Params[0];
249 if (requestData.ContainsKey("xmin"))
250 {
251 xmin = (Int32)requestData["xmin"];
252 }
253 if (requestData.ContainsKey("ymin"))
254 {
255 ymin = (Int32)requestData["ymin"];
256 }
257 if (requestData.ContainsKey("xmax"))
258 {
259 xmax = (Int32)requestData["xmax"];
260 }
261 if (requestData.ContainsKey("ymax"))
262 {
263 ymax = (Int32)requestData["ymax"];
264 }
265
266 XmlRpcResponse response = new XmlRpcResponse();
267 Hashtable responseData = new Hashtable();
268 response.Value = responseData;
269 IList simProfileList = new ArrayList();
270
271 SimProfileData simProfile;
272 for (int x = xmin; x < xmax; x++)
273 {
274 for (int y = ymin; y < ymax; y++)
275 {
276 simProfile = getRegion(Helpers.UIntsToLong((uint)(x * 256), (uint)(y * 256)));
277 if (simProfile != null)
278 {
279 Hashtable simProfileBlock = new Hashtable();
280 simProfileBlock["x"] = x;
281 simProfileBlock["y"] = y;
282 simProfileBlock["name"] = simProfile.regionName;
283 simProfileBlock["access"] = 0;
284 simProfileBlock["region-flags"] = 0;
285 simProfileBlock["water-height"] = 20;
286 simProfileBlock["agents"] = 1;
287 simProfileBlock["map-image-id"] = simProfile.regionMapTextureID.ToString();
288
289 simProfileList.Add(simProfileBlock);
290 }
291 }
292 }
293
294 responseData["sim-profiles"] = simProfileList;
295
296 return response;
297 }
298
299
300
301 /// <summary>
302 /// Performs a REST Get Operation
303 /// </summary>
304 /// <param name="request"></param>
305 /// <param name="path"></param>
306 /// <param name="param"></param>
307 /// <returns></returns>
308 public string RestGetRegionMethod(string request, string path, string param)
309 {
310 return RestGetSimMethod("", "/sims/", param);
311 }
312
313 /// <summary>
314 /// Performs a REST Set Operation
315 /// </summary>
316 /// <param name="request"></param>
317 /// <param name="path"></param>
318 /// <param name="param"></param>
319 /// <returns></returns>
320 public string RestSetRegionMethod(string request, string path, string param)
321 {
322 return RestSetSimMethod("", "/sims/", param);
323 }
324
325 /// <summary>
326 /// Returns information about a sim via a REST Request
327 /// </summary>
328 /// <param name="request"></param>
329 /// <param name="path"></param>
330 /// <param name="param"></param>
331 /// <returns>Information about the sim in XML</returns>
332 public string RestGetSimMethod(string request, string path, string param)
333 {
334 string respstring = String.Empty;
335
336 SimProfileData TheSim;
337 LLUUID UUID = new LLUUID(param);
338 TheSim = getRegion(UUID);
339
340 if (!(TheSim == null))
341 {
342 respstring = "<Root>";
343 respstring += "<authkey>" + TheSim.regionSendKey + "</authkey>";
344 respstring += "<sim>";
345 respstring += "<uuid>" + TheSim.UUID.ToString() + "</uuid>";
346 respstring += "<regionname>" + TheSim.regionName + "</regionname>";
347 respstring += "<sim_ip>" + TheSim.serverIP + "</sim_ip>";
348 respstring += "<sim_port>" + TheSim.serverPort.ToString() + "</sim_port>";
349 respstring += "<region_locx>" + TheSim.regionLocX.ToString() + "</region_locx>";
350 respstring += "<region_locy>" + TheSim.regionLocY.ToString() + "</region_locy>";
351 respstring += "<estate_id>1</estate_id>";
352 respstring += "</sim>";
353 respstring += "</Root>";
354 }
355
356 return respstring;
357 }
358
359 /// <summary>
360 /// Creates or updates a sim via a REST Method Request
361 /// BROKEN with SQL Update
362 /// </summary>
363 /// <param name="request"></param>
364 /// <param name="path"></param>
365 /// <param name="param"></param>
366 /// <returns>"OK" or an error</returns>
367 public string RestSetSimMethod(string request, string path, string param)
368 {
369 Console.WriteLine("SimProfiles.cs:RestSetSimMethod() - processing request......");
370 SimProfileData TheSim;
371 TheSim = getRegion(new LLUUID(param));
372 if ((TheSim) == null)
373 {
374 TheSim = new SimProfileData();
375 LLUUID UUID = new LLUUID(param);
376 TheSim.UUID = UUID;
377 TheSim.regionRecvKey = config.SimRecvKey;
378 }
379
380 XmlDocument doc = new XmlDocument();
381 doc.LoadXml(request);
382 XmlNode rootnode = doc.FirstChild;
383 XmlNode authkeynode = rootnode.ChildNodes[0];
384 if (authkeynode.Name != "authkey")
385 {
386 return "ERROR! bad XML - expected authkey tag";
387 }
388
389 XmlNode simnode = rootnode.ChildNodes[1];
390 if (simnode.Name != "sim")
391 {
392 return "ERROR! bad XML - expected sim tag";
393 }
394
395 if (authkeynode.InnerText != TheSim.regionRecvKey)
396 {
397 return "ERROR! invalid key";
398 }
399
400 //TheSim.regionSendKey = Cfg;
401 TheSim.regionRecvKey = config.SimRecvKey;
402 TheSim.regionSendKey = config.SimSendKey;
403 TheSim.regionSecret = config.SimRecvKey;
404 TheSim.regionDataURI = "";
405 TheSim.regionAssetURI = config.DefaultAssetServer;
406 TheSim.regionAssetRecvKey = config.AssetRecvKey;
407 TheSim.regionAssetSendKey = config.AssetSendKey;
408 TheSim.regionUserURI = config.DefaultUserServer;
409 TheSim.regionUserSendKey = config.UserSendKey;
410 TheSim.regionUserRecvKey = config.UserRecvKey;
411
412
413 for (int i = 0; i < simnode.ChildNodes.Count; i++)
414 {
415 switch (simnode.ChildNodes[i].Name)
416 {
417 case "regionname":
418 TheSim.regionName = simnode.ChildNodes[i].InnerText;
419 break;
420
421 case "sim_ip":
422 TheSim.serverIP = simnode.ChildNodes[i].InnerText;
423 break;
424
425 case "sim_port":
426 TheSim.serverPort = Convert.ToUInt32(simnode.ChildNodes[i].InnerText);
427 break;
428
429 case "region_locx":
430 TheSim.regionLocX = Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText);
431 TheSim.regionHandle = Helpers.UIntsToLong((TheSim.regionLocX * 256), (TheSim.regionLocY * 256));
432 break;
433
434 case "region_locy":
435 TheSim.regionLocY = Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText);
436 TheSim.regionHandle = Helpers.UIntsToLong((TheSim.regionLocX * 256), (TheSim.regionLocY * 256));
437 break;
438 }
439 }
440
441 TheSim.serverURI = "http://" + TheSim.serverIP + ":" + TheSim.serverPort + "/";
442
443 bool requirePublic = false;
444
445 if (requirePublic && (TheSim.serverIP.StartsWith("172.16") || TheSim.serverIP.StartsWith("192.168") || TheSim.serverIP.StartsWith("10.") || TheSim.serverIP.StartsWith("0.") || TheSim.serverIP.StartsWith("255.")))
446 {
447 return "ERROR! Servers must register with public addresses.";
448 }
449
450 try
451 {
452 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"Attempting to add a new region to the grid - " + _plugins.Count + " storage provider(s) registered.");
453 foreach (KeyValuePair<string, IGridData> kvp in _plugins)
454 {
455 try
456 {
457 kvp.Value.AddProfile(TheSim);
458 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"New sim added to grid (" + TheSim.regionName + ")");
459 }
460 catch (Exception e)
461 {
462 OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW,"getRegionPlugin Handle " + kvp.Key + " unable to add new sim: " + e.ToString());
463 }
464 }
465 return "OK";
466 }
467 catch (Exception e)
468 {
469 return "ERROR! Could not save to database! (" + e.ToString() + ")";
470 }
471 }
472
473 }
474}