GridManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Reflection;
  6. using OpenGrid.Framework.Data;
  7. using OpenSim.Framework.Utilities;
  8. using OpenSim.Framework.Console;
  9. using OpenSim.Framework.Sims;
  10. using libsecondlife;
  11. using Nwc.XmlRpc;
  12. using System.Xml;
  13. namespace OpenGridServices.GridServer
  14. {
  15. class GridManager
  16. {
  17. Dictionary<string, IGridData> _plugins = new Dictionary<string, IGridData>();
  18. public string defaultRecvKey;
  19. /// <summary>
  20. /// Adds a new grid server plugin - grid servers will be requested in the order they were loaded.
  21. /// </summary>
  22. /// <param name="FileName">The filename to the grid server plugin DLL</param>
  23. public void AddPlugin(string FileName)
  24. {
  25. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Storage: Attempting to load " + FileName);
  26. Assembly pluginAssembly = Assembly.LoadFrom(FileName);
  27. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Storage: Found " + pluginAssembly.GetTypes().Length + " interfaces.");
  28. foreach (Type pluginType in pluginAssembly.GetTypes())
  29. {
  30. if (!pluginType.IsAbstract)
  31. {
  32. Type typeInterface = pluginType.GetInterface("IGridData", true);
  33. if (typeInterface != null)
  34. {
  35. IGridData plug = (IGridData)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
  36. plug.Initialise();
  37. this._plugins.Add(plug.getName(), plug);
  38. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Storage: Added IGridData Interface");
  39. }
  40. typeInterface = null;
  41. }
  42. }
  43. pluginAssembly = null;
  44. }
  45. /// <summary>
  46. /// Returns a region by argument
  47. /// </summary>
  48. /// <param name="uuid">A UUID key of the region to return</param>
  49. /// <returns>A SimProfileData for the region</returns>
  50. public SimProfileData getRegion(libsecondlife.LLUUID uuid)
  51. {
  52. foreach(KeyValuePair<string,IGridData> kvp in _plugins) {
  53. try
  54. {
  55. return kvp.Value.GetProfileByLLUUID(uuid);
  56. }
  57. catch (Exception e)
  58. {
  59. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Storage: Unable to find region " + uuid.ToStringHyphenated() + " via " + kvp.Key);
  60. }
  61. }
  62. return null;
  63. }
  64. /// <summary>
  65. /// Returns a region by argument
  66. /// </summary>
  67. /// <param name="uuid">A regionHandle of the region to return</param>
  68. /// <returns>A SimProfileData for the region</returns>
  69. public SimProfileData getRegion(ulong handle)
  70. {
  71. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  72. {
  73. try
  74. {
  75. return kvp.Value.GetProfileByHandle(handle);
  76. }
  77. catch (Exception e)
  78. {
  79. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Storage: Unable to find region " + handle.ToString() + " via " + kvp.Key);
  80. }
  81. }
  82. return null;
  83. }
  84. /// <summary>
  85. /// Returns a XML String containing a list of the neighbouring regions
  86. /// </summary>
  87. /// <param name="reqhandle">The regionhandle for the center sim</param>
  88. /// <returns>An XML string containing neighbour entities</returns>
  89. public string GetXMLNeighbours(ulong reqhandle)
  90. {
  91. string response = "";
  92. SimProfileData central_region = getRegion(reqhandle);
  93. SimProfileData neighbour;
  94. for (int x = -1; x < 2; x++) for (int y = -1; y < 2; y++)
  95. {
  96. if (getRegion(Util.UIntsToLong((uint)((central_region.regionLocX + x) * 256), (uint)(central_region.regionLocY + y) * 256)) != null)
  97. {
  98. neighbour = getRegion(Util.UIntsToLong((uint)((central_region.regionLocX + x) * 256), (uint)(central_region.regionLocY + y) * 256));
  99. response += "<neighbour>";
  100. response += "<sim_ip>" + neighbour.serverIP + "</sim_ip>";
  101. response += "<sim_port>" + neighbour.serverPort.ToString() + "</sim_port>";
  102. response += "<locx>" + neighbour.regionLocX.ToString() + "</locx>";
  103. response += "<locy>" + neighbour.regionLocY.ToString() + "</locy>";
  104. response += "<regionhandle>" + neighbour.regionHandle.ToString() + "</regionhandle>";
  105. response += "</neighbour>";
  106. }
  107. }
  108. return response;
  109. }
  110. /// <summary>
  111. /// Performed when a region connects to the grid server initially.
  112. /// </summary>
  113. /// <param name="request">The XMLRPC Request</param>
  114. /// <returns>Startup parameters</returns>
  115. public XmlRpcResponse XmlRpcLoginToSimulatorMethod(XmlRpcRequest request)
  116. {
  117. XmlRpcResponse response = new XmlRpcResponse();
  118. Hashtable responseData = new Hashtable();
  119. response.Value = responseData;
  120. SimProfileData TheSim = null;
  121. Hashtable requestData = (Hashtable)request.Params[0];
  122. if (requestData.ContainsKey("UUID"))
  123. {
  124. TheSim = getRegion(new LLUUID((string)requestData["UUID"]));
  125. }
  126. else if (requestData.ContainsKey("region_handle"))
  127. {
  128. TheSim = getRegion((ulong)Convert.ToUInt64(requestData["region_handle"]));
  129. }
  130. if (TheSim == null)
  131. {
  132. responseData["error"] = "sim not found";
  133. }
  134. else
  135. {
  136. ArrayList SimNeighboursData = new ArrayList();
  137. SimProfileData neighbour;
  138. Hashtable NeighbourBlock;
  139. for (int x = -1; x < 2; x++) for (int y = -1; y < 2; y++)
  140. {
  141. if (getRegion(Helpers.UIntsToLong((uint)((TheSim.regionLocX + x) * 256), (uint)(TheSim.regionLocY + y) * 256)) != null)
  142. {
  143. neighbour = getRegion(Helpers.UIntsToLong((uint)((TheSim.regionLocX + x) * 256), (uint)(TheSim.regionLocY + y) * 256));
  144. NeighbourBlock = new Hashtable();
  145. NeighbourBlock["sim_ip"] = neighbour.serverIP;
  146. NeighbourBlock["sim_port"] = neighbour.serverPort.ToString();
  147. NeighbourBlock["region_locx"] = neighbour.regionLocX.ToString();
  148. NeighbourBlock["region_locy"] = neighbour.regionLocY.ToString();
  149. NeighbourBlock["UUID"] = neighbour.UUID.ToString();
  150. if (neighbour.UUID != TheSim.UUID) SimNeighboursData.Add(NeighbourBlock);
  151. }
  152. }
  153. responseData["UUID"] = TheSim.UUID.ToString();
  154. responseData["region_locx"] = TheSim.regionLocX.ToString();
  155. responseData["region_locy"] = TheSim.regionLocY.ToString();
  156. responseData["regionname"] = TheSim.regionName;
  157. responseData["estate_id"] = "1";
  158. responseData["neighbours"] = SimNeighboursData;
  159. responseData["sim_ip"] = TheSim.serverIP;
  160. responseData["sim_port"] = TheSim.serverPort.ToString();
  161. responseData["asset_url"] = TheSim.regionAssetURI;
  162. responseData["asset_sendkey"] = TheSim.regionAssetSendKey;
  163. responseData["asset_recvkey"] = TheSim.regionAssetRecvKey;
  164. responseData["user_url"] = TheSim.regionUserURI;
  165. responseData["user_sendkey"] = TheSim.regionUserSendKey;
  166. responseData["user_recvkey"] = TheSim.regionUserRecvKey;
  167. responseData["authkey"] = TheSim.regionSecret;
  168. // New! If set, use as URL to local sim storage (ie http://remotehost/region.yap)
  169. responseData["data_uri"] = TheSim.regionDataURI;
  170. }
  171. return response;
  172. }
  173. /// <summary>
  174. /// Performs a REST Get Operation
  175. /// </summary>
  176. /// <param name="request"></param>
  177. /// <param name="path"></param>
  178. /// <param name="param"></param>
  179. /// <returns></returns>
  180. public string RestGetRegionMethod(string request, string path, string param)
  181. {
  182. return RestGetSimMethod("", "/sims/", param);
  183. }
  184. /// <summary>
  185. /// Performs a REST Set Operation
  186. /// </summary>
  187. /// <param name="request"></param>
  188. /// <param name="path"></param>
  189. /// <param name="param"></param>
  190. /// <returns></returns>
  191. public string RestSetRegionMethod(string request, string path, string param)
  192. {
  193. return RestSetSimMethod("", "/sims/", param);
  194. }
  195. /// <summary>
  196. /// Returns information about a sim via a REST Request
  197. /// </summary>
  198. /// <param name="request"></param>
  199. /// <param name="path"></param>
  200. /// <param name="param"></param>
  201. /// <returns>Information about the sim in XML</returns>
  202. public string RestGetSimMethod(string request, string path, string param)
  203. {
  204. string respstring = String.Empty;
  205. SimProfileData TheSim;
  206. LLUUID UUID = new LLUUID(param);
  207. TheSim = getRegion(UUID);
  208. if (!(TheSim == null))
  209. {
  210. respstring = "<Root>";
  211. respstring += "<authkey>" + TheSim.regionSendKey + "</authkey>";
  212. respstring += "<sim>";
  213. respstring += "<uuid>" + TheSim.UUID.ToString() + "</uuid>";
  214. respstring += "<regionname>" + TheSim.regionName + "</regionname>";
  215. respstring += "<sim_ip>" + TheSim.serverIP + "</sim_ip>";
  216. respstring += "<sim_port>" + TheSim.serverPort.ToString() + "</sim_port>";
  217. respstring += "<region_locx>" + TheSim.regionLocX.ToString() + "</region_locx>";
  218. respstring += "<region_locy>" + TheSim.regionLocY.ToString() + "</region_locy>";
  219. respstring += "<estate_id>1</estate_id>";
  220. respstring += "</sim>";
  221. respstring += "</Root>";
  222. }
  223. return respstring;
  224. }
  225. /// <summary>
  226. /// Creates or updates a sim via a REST Method Request
  227. /// BROKEN with SQL Update
  228. /// </summary>
  229. /// <param name="request"></param>
  230. /// <param name="path"></param>
  231. /// <param name="param"></param>
  232. /// <returns>"OK" or an error</returns>
  233. public string RestSetSimMethod(string request, string path, string param)
  234. {
  235. Console.WriteLine("SimProfiles.cs:RestSetSimMethod() - processing request......");
  236. SimProfileData TheSim;
  237. TheSim = getRegion(new LLUUID(param));
  238. if ((TheSim) == null)
  239. {
  240. TheSim = new SimProfileData();
  241. LLUUID UUID = new LLUUID(param);
  242. TheSim.UUID = UUID;
  243. TheSim.regionRecvKey = defaultRecvKey;
  244. }
  245. XmlDocument doc = new XmlDocument();
  246. doc.LoadXml(request);
  247. XmlNode rootnode = doc.FirstChild;
  248. XmlNode authkeynode = rootnode.ChildNodes[0];
  249. if (authkeynode.Name != "authkey")
  250. {
  251. return "ERROR! bad XML - expected authkey tag";
  252. }
  253. XmlNode simnode = rootnode.ChildNodes[1];
  254. if (simnode.Name != "sim")
  255. {
  256. return "ERROR! bad XML - expected sim tag";
  257. }
  258. if (authkeynode.InnerText != TheSim.regionRecvKey)
  259. {
  260. return "ERROR! invalid key";
  261. }
  262. for (int i = 0; i < simnode.ChildNodes.Count; i++)
  263. {
  264. switch (simnode.ChildNodes[i].Name)
  265. {
  266. case "regionname":
  267. TheSim.regionName = simnode.ChildNodes[i].InnerText;
  268. break;
  269. case "sim_ip":
  270. TheSim.serverIP = simnode.ChildNodes[i].InnerText;
  271. break;
  272. case "sim_port":
  273. TheSim.serverPort = Convert.ToUInt32(simnode.ChildNodes[i].InnerText);
  274. break;
  275. case "region_locx":
  276. TheSim.regionLocX = Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText);
  277. TheSim.regionHandle = Helpers.UIntsToLong((TheSim.regionLocX * 256), (TheSim.regionLocY * 256));
  278. break;
  279. case "region_locy":
  280. TheSim.regionLocY = Convert.ToUInt32((string)simnode.ChildNodes[i].InnerText);
  281. TheSim.regionHandle = Helpers.UIntsToLong((TheSim.regionLocX * 256), (TheSim.regionLocY * 256));
  282. break;
  283. }
  284. }
  285. try
  286. {
  287. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Attempting to add a new region to the grid - " + _plugins.Count + " storage provider(s) registered.");
  288. foreach (KeyValuePair<string, IGridData> kvp in _plugins)
  289. {
  290. try
  291. {
  292. kvp.Value.AddProfile(TheSim);
  293. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("New sim added to grid (" + TheSim.regionName + ")");
  294. }
  295. catch (Exception e)
  296. {
  297. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("getRegionPlugin Handle " + kvp.Key + " unable to add new sim: " + e.ToString());
  298. }
  299. }
  300. return "OK";
  301. }
  302. catch (Exception e)
  303. {
  304. return "ERROR! could not save to database! (" + e.ToString() + ")";
  305. }
  306. }
  307. }
  308. }