GridServiceConnector.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.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 OpenSimulator 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. using log4net;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications;
  35. using OpenSim.Framework.Servers.HttpServer;
  36. using OpenSim.Services.Interfaces;
  37. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  38. using OpenSim.Server.Base;
  39. using OpenMetaverse;
  40. namespace OpenSim.Services.Connectors
  41. {
  42. public class GridServicesConnector : IGridService
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(
  46. MethodBase.GetCurrentMethod().DeclaringType);
  47. private string m_ServerURI = String.Empty;
  48. public GridServicesConnector()
  49. {
  50. }
  51. public GridServicesConnector(string serverURI)
  52. {
  53. m_ServerURI = serverURI.TrimEnd('/');
  54. }
  55. public GridServicesConnector(IConfigSource source)
  56. {
  57. Initialise(source);
  58. }
  59. public virtual void Initialise(IConfigSource source)
  60. {
  61. IConfig gridConfig = source.Configs["GridService"];
  62. if (gridConfig == null)
  63. {
  64. m_log.Error("[GRID CONNECTOR]: GridService missing from OpenSim.ini");
  65. throw new Exception("Grid connector init error");
  66. }
  67. string serviceURI = gridConfig.GetString("GridServerURI",
  68. String.Empty);
  69. if (serviceURI == String.Empty)
  70. {
  71. m_log.Error("[GRID CONNECTOR]: No Server URI named in section GridService");
  72. throw new Exception("Grid connector init error");
  73. }
  74. m_ServerURI = serviceURI;
  75. }
  76. #region IGridService
  77. public virtual string RegisterRegion(UUID scopeID, GridRegion regionInfo)
  78. {
  79. Dictionary<string, object> rinfo = regionInfo.ToKeyValuePairs();
  80. Dictionary<string, object> sendData = new Dictionary<string,object>();
  81. foreach (KeyValuePair<string, object> kvp in rinfo)
  82. sendData[kvp.Key] = (string)kvp.Value;
  83. sendData["SCOPEID"] = scopeID.ToString();
  84. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  85. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  86. sendData["METHOD"] = "register";
  87. string reqString = ServerUtils.BuildQueryString(sendData);
  88. // m_log.DebugFormat("[GRID CONNECTOR]: queryString = {0}", reqString);
  89. try
  90. {
  91. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  92. m_ServerURI + "/grid",
  93. reqString);
  94. if (reply != string.Empty)
  95. {
  96. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  97. if (replyData.ContainsKey("Result")&& (replyData["Result"].ToString().ToLower() == "success"))
  98. {
  99. return String.Empty;
  100. }
  101. else if (replyData.ContainsKey("Result")&& (replyData["Result"].ToString().ToLower() == "failure"))
  102. {
  103. m_log.DebugFormat("[GRID CONNECTOR]: Registration failed: {0}", replyData["Message"].ToString());
  104. return replyData["Message"].ToString();
  105. }
  106. else if (!replyData.ContainsKey("Result"))
  107. {
  108. m_log.DebugFormat("[GRID CONNECTOR]: reply data does not contain result field");
  109. }
  110. else
  111. {
  112. m_log.DebugFormat("[GRID CONNECTOR]: unexpected result {0}", replyData["Result"].ToString());
  113. return "Unexpected result "+replyData["Result"].ToString();
  114. }
  115. }
  116. else
  117. m_log.DebugFormat("[GRID CONNECTOR]: RegisterRegion received null reply");
  118. }
  119. catch (Exception e)
  120. {
  121. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  122. }
  123. return "Error communicating with grid service";
  124. }
  125. public virtual bool DeregisterRegion(UUID regionID)
  126. {
  127. Dictionary<string, object> sendData = new Dictionary<string, object>();
  128. sendData["REGIONID"] = regionID.ToString();
  129. sendData["METHOD"] = "deregister";
  130. try
  131. {
  132. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  133. m_ServerURI + "/grid",
  134. ServerUtils.BuildQueryString(sendData));
  135. if (reply != string.Empty)
  136. {
  137. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  138. if ((replyData["Result"] != null) && (replyData["Result"].ToString().ToLower() == "success"))
  139. return true;
  140. }
  141. else
  142. m_log.DebugFormat("[GRID CONNECTOR]: DeregisterRegion received null reply");
  143. }
  144. catch (Exception e)
  145. {
  146. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  147. }
  148. return false;
  149. }
  150. public virtual List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID)
  151. {
  152. Dictionary<string, object> sendData = new Dictionary<string, object>();
  153. sendData["SCOPEID"] = scopeID.ToString();
  154. sendData["REGIONID"] = regionID.ToString();
  155. sendData["METHOD"] = "get_neighbours";
  156. List<GridRegion> rinfos = new List<GridRegion>();
  157. string reqString = ServerUtils.BuildQueryString(sendData);
  158. string reply = string.Empty;
  159. try
  160. {
  161. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  162. m_ServerURI + "/grid",
  163. reqString);
  164. }
  165. catch (Exception e)
  166. {
  167. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  168. return rinfos;
  169. }
  170. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  171. if (replyData != null)
  172. {
  173. Dictionary<string, object>.ValueCollection rinfosList = replyData.Values;
  174. //m_log.DebugFormat("[GRID CONNECTOR]: get neighbours returned {0} elements", rinfosList.Count);
  175. foreach (object r in rinfosList)
  176. {
  177. if (r is Dictionary<string, object>)
  178. {
  179. GridRegion rinfo = new GridRegion((Dictionary<string, object>)r);
  180. rinfos.Add(rinfo);
  181. }
  182. else
  183. m_log.DebugFormat("[GRID CONNECTOR]: GetNeighbours {0}, {1} received invalid response type {2}",
  184. scopeID, regionID, r.GetType());
  185. }
  186. }
  187. else
  188. m_log.DebugFormat("[GRID CONNECTOR]: GetNeighbours {0}, {1} received null response",
  189. scopeID, regionID);
  190. return rinfos;
  191. }
  192. public virtual GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
  193. {
  194. Dictionary<string, object> sendData = new Dictionary<string, object>();
  195. sendData["SCOPEID"] = scopeID.ToString();
  196. sendData["REGIONID"] = regionID.ToString();
  197. sendData["METHOD"] = "get_region_by_uuid";
  198. string reply = string.Empty;
  199. try
  200. {
  201. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  202. m_ServerURI + "/grid",
  203. ServerUtils.BuildQueryString(sendData));
  204. }
  205. catch (Exception e)
  206. {
  207. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  208. return null;
  209. }
  210. GridRegion rinfo = null;
  211. if (reply != string.Empty)
  212. {
  213. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  214. if ((replyData != null) && (replyData["result"] != null))
  215. {
  216. if (replyData["result"] is Dictionary<string, object>)
  217. rinfo = new GridRegion((Dictionary<string, object>)replyData["result"]);
  218. //else
  219. // m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByUUID {0}, {1} received null response",
  220. // scopeID, regionID);
  221. }
  222. else
  223. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByUUID {0}, {1} received null response",
  224. scopeID, regionID);
  225. }
  226. else
  227. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByUUID received null reply");
  228. return rinfo;
  229. }
  230. public virtual GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
  231. {
  232. Dictionary<string, object> sendData = new Dictionary<string, object>();
  233. sendData["SCOPEID"] = scopeID.ToString();
  234. sendData["X"] = x.ToString();
  235. sendData["Y"] = y.ToString();
  236. sendData["METHOD"] = "get_region_by_position";
  237. string reply = string.Empty;
  238. try
  239. {
  240. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  241. m_ServerURI + "/grid",
  242. ServerUtils.BuildQueryString(sendData));
  243. }
  244. catch (Exception e)
  245. {
  246. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  247. return null;
  248. }
  249. GridRegion rinfo = null;
  250. if (reply != string.Empty)
  251. {
  252. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  253. if ((replyData != null) && (replyData["result"] != null))
  254. {
  255. if (replyData["result"] is Dictionary<string, object>)
  256. rinfo = new GridRegion((Dictionary<string, object>)replyData["result"]);
  257. else
  258. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByPosition {0}, {1}-{2} received invalid response",
  259. scopeID, x, y);
  260. }
  261. else
  262. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByPosition {0}, {1}-{2} received null response",
  263. scopeID, x, y);
  264. }
  265. else
  266. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByPosition received null reply");
  267. return rinfo;
  268. }
  269. public virtual GridRegion GetRegionByName(UUID scopeID, string regionName)
  270. {
  271. Dictionary<string, object> sendData = new Dictionary<string, object>();
  272. sendData["SCOPEID"] = scopeID.ToString();
  273. sendData["NAME"] = regionName;
  274. sendData["METHOD"] = "get_region_by_name";
  275. string reply = string.Empty;
  276. try
  277. {
  278. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  279. m_ServerURI + "/grid",
  280. ServerUtils.BuildQueryString(sendData));
  281. }
  282. catch (Exception e)
  283. {
  284. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  285. return null;
  286. }
  287. GridRegion rinfo = null;
  288. if (reply != string.Empty)
  289. {
  290. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  291. if ((replyData != null) && (replyData["result"] != null))
  292. {
  293. if (replyData["result"] is Dictionary<string, object>)
  294. rinfo = new GridRegion((Dictionary<string, object>)replyData["result"]);
  295. }
  296. else
  297. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByPosition {0}, {1} received null response",
  298. scopeID, regionName);
  299. }
  300. else
  301. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByName received null reply");
  302. return rinfo;
  303. }
  304. public virtual List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
  305. {
  306. Dictionary<string, object> sendData = new Dictionary<string, object>();
  307. sendData["SCOPEID"] = scopeID.ToString();
  308. sendData["NAME"] = name;
  309. sendData["MAX"] = maxNumber.ToString();
  310. sendData["METHOD"] = "get_regions_by_name";
  311. List<GridRegion> rinfos = new List<GridRegion>();
  312. string reply = string.Empty;
  313. try
  314. {
  315. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  316. m_ServerURI + "/grid",
  317. ServerUtils.BuildQueryString(sendData));
  318. }
  319. catch (Exception e)
  320. {
  321. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  322. return rinfos;
  323. }
  324. if (reply != string.Empty)
  325. {
  326. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  327. if (replyData != null)
  328. {
  329. Dictionary<string, object>.ValueCollection rinfosList = replyData.Values;
  330. foreach (object r in rinfosList)
  331. {
  332. if (r is Dictionary<string, object>)
  333. {
  334. GridRegion rinfo = new GridRegion((Dictionary<string, object>)r);
  335. rinfos.Add(rinfo);
  336. }
  337. else
  338. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionsByName {0}, {1}, {2} received invalid response",
  339. scopeID, name, maxNumber);
  340. }
  341. }
  342. else
  343. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionsByName {0}, {1}, {2} received null response",
  344. scopeID, name, maxNumber);
  345. }
  346. else
  347. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionsByName received null reply");
  348. return rinfos;
  349. }
  350. public virtual List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
  351. {
  352. Dictionary<string, object> sendData = new Dictionary<string, object>();
  353. sendData["SCOPEID"] = scopeID.ToString();
  354. sendData["XMIN"] = xmin.ToString();
  355. sendData["XMAX"] = xmax.ToString();
  356. sendData["YMIN"] = ymin.ToString();
  357. sendData["YMAX"] = ymax.ToString();
  358. sendData["METHOD"] = "get_region_range";
  359. List<GridRegion> rinfos = new List<GridRegion>();
  360. string reply = string.Empty;
  361. try
  362. {
  363. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  364. m_ServerURI + "/grid",
  365. ServerUtils.BuildQueryString(sendData));
  366. //m_log.DebugFormat("[GRID CONNECTOR]: reply was {0}", reply);
  367. }
  368. catch (Exception e)
  369. {
  370. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  371. return rinfos;
  372. }
  373. if (reply != string.Empty)
  374. {
  375. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  376. if (replyData != null)
  377. {
  378. Dictionary<string, object>.ValueCollection rinfosList = replyData.Values;
  379. foreach (object r in rinfosList)
  380. {
  381. if (r is Dictionary<string, object>)
  382. {
  383. GridRegion rinfo = new GridRegion((Dictionary<string, object>)r);
  384. rinfos.Add(rinfo);
  385. }
  386. }
  387. }
  388. else
  389. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionRange {0}, {1}-{2} {3}-{4} received null response",
  390. scopeID, xmin, xmax, ymin, ymax);
  391. }
  392. else
  393. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionRange received null reply");
  394. return rinfos;
  395. }
  396. #endregion
  397. }
  398. }