GridServiceConnector.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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 bool 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. return true;
  99. else if (!replyData.ContainsKey("Result"))
  100. m_log.DebugFormat("[GRID CONNECTOR]: reply data does not contain result field");
  101. else
  102. m_log.DebugFormat("[GRID CONNECTOR]: unexpected result {0}", replyData["Result"].ToString());
  103. }
  104. else
  105. m_log.DebugFormat("[GRID CONNECTOR]: RegisterRegion received null reply");
  106. }
  107. catch (Exception e)
  108. {
  109. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  110. }
  111. return false;
  112. }
  113. public virtual bool DeregisterRegion(UUID regionID)
  114. {
  115. Dictionary<string, object> sendData = new Dictionary<string, object>();
  116. sendData["REGIONID"] = regionID.ToString();
  117. sendData["METHOD"] = "deregister";
  118. try
  119. {
  120. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  121. m_ServerURI + "/grid",
  122. ServerUtils.BuildQueryString(sendData));
  123. if (reply != string.Empty)
  124. {
  125. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  126. if ((replyData["Result"] != null) && (replyData["Result"].ToString().ToLower() == "success"))
  127. return true;
  128. }
  129. else
  130. m_log.DebugFormat("[GRID CONNECTOR]: DeregisterRegion received null reply");
  131. }
  132. catch (Exception e)
  133. {
  134. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  135. }
  136. return false;
  137. }
  138. public virtual List<GridRegion> GetNeighbours(UUID scopeID, UUID regionID)
  139. {
  140. Dictionary<string, object> sendData = new Dictionary<string, object>();
  141. sendData["SCOPEID"] = scopeID.ToString();
  142. sendData["REGIONID"] = regionID.ToString();
  143. sendData["METHOD"] = "get_neighbours";
  144. List<GridRegion> rinfos = new List<GridRegion>();
  145. string reqString = ServerUtils.BuildQueryString(sendData);
  146. string reply = string.Empty;
  147. try
  148. {
  149. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  150. m_ServerURI + "/grid",
  151. reqString);
  152. }
  153. catch (Exception e)
  154. {
  155. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  156. return rinfos;
  157. }
  158. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  159. if (replyData != null)
  160. {
  161. Dictionary<string, object>.ValueCollection rinfosList = replyData.Values;
  162. //m_log.DebugFormat("[GRID CONNECTOR]: get neighbours returned {0} elements", rinfosList.Count);
  163. foreach (object r in rinfosList)
  164. {
  165. if (r is Dictionary<string, object>)
  166. {
  167. GridRegion rinfo = new GridRegion((Dictionary<string, object>)r);
  168. rinfos.Add(rinfo);
  169. }
  170. else
  171. m_log.DebugFormat("[GRID CONNECTOR]: GetNeighbours {0}, {1} received invalid response type {2}",
  172. scopeID, regionID, r.GetType());
  173. }
  174. }
  175. else
  176. m_log.DebugFormat("[GRID CONNECTOR]: GetNeighbours {0}, {1} received null response",
  177. scopeID, regionID);
  178. return rinfos;
  179. }
  180. public virtual GridRegion GetRegionByUUID(UUID scopeID, UUID regionID)
  181. {
  182. Dictionary<string, object> sendData = new Dictionary<string, object>();
  183. sendData["SCOPEID"] = scopeID.ToString();
  184. sendData["REGIONID"] = regionID.ToString();
  185. sendData["METHOD"] = "get_region_by_uuid";
  186. string reply = string.Empty;
  187. try
  188. {
  189. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  190. m_ServerURI + "/grid",
  191. ServerUtils.BuildQueryString(sendData));
  192. }
  193. catch (Exception e)
  194. {
  195. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  196. return null;
  197. }
  198. GridRegion rinfo = null;
  199. if (reply != string.Empty)
  200. {
  201. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  202. if ((replyData != null) && (replyData["result"] != null))
  203. {
  204. if (replyData["result"] is Dictionary<string, object>)
  205. rinfo = new GridRegion((Dictionary<string, object>)replyData["result"]);
  206. //else
  207. // m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByUUID {0}, {1} received null response",
  208. // scopeID, regionID);
  209. }
  210. else
  211. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByUUID {0}, {1} received null response",
  212. scopeID, regionID);
  213. }
  214. else
  215. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByUUID received null reply");
  216. return rinfo;
  217. }
  218. public virtual GridRegion GetRegionByPosition(UUID scopeID, int x, int y)
  219. {
  220. Dictionary<string, object> sendData = new Dictionary<string, object>();
  221. sendData["SCOPEID"] = scopeID.ToString();
  222. sendData["X"] = x.ToString();
  223. sendData["Y"] = y.ToString();
  224. sendData["METHOD"] = "get_region_by_position";
  225. string reply = string.Empty;
  226. try
  227. {
  228. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  229. m_ServerURI + "/grid",
  230. ServerUtils.BuildQueryString(sendData));
  231. }
  232. catch (Exception e)
  233. {
  234. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  235. return null;
  236. }
  237. GridRegion rinfo = null;
  238. if (reply != string.Empty)
  239. {
  240. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  241. if ((replyData != null) && (replyData["result"] != null))
  242. {
  243. if (replyData["result"] is Dictionary<string, object>)
  244. rinfo = new GridRegion((Dictionary<string, object>)replyData["result"]);
  245. else
  246. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByPosition {0}, {1}-{2} received invalid response",
  247. scopeID, x, y);
  248. }
  249. else
  250. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByPosition {0}, {1}-{2} received null response",
  251. scopeID, x, y);
  252. }
  253. else
  254. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByPosition received null reply");
  255. return rinfo;
  256. }
  257. public virtual GridRegion GetRegionByName(UUID scopeID, string regionName)
  258. {
  259. Dictionary<string, object> sendData = new Dictionary<string, object>();
  260. sendData["SCOPEID"] = scopeID.ToString();
  261. sendData["NAME"] = regionName;
  262. sendData["METHOD"] = "get_region_by_name";
  263. string reply = string.Empty;
  264. try
  265. {
  266. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  267. m_ServerURI + "/grid",
  268. ServerUtils.BuildQueryString(sendData));
  269. }
  270. catch (Exception e)
  271. {
  272. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  273. return null;
  274. }
  275. GridRegion rinfo = null;
  276. if (reply != string.Empty)
  277. {
  278. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  279. if ((replyData != null) && (replyData["result"] != null))
  280. {
  281. if (replyData["result"] is Dictionary<string, object>)
  282. rinfo = new GridRegion((Dictionary<string, object>)replyData["result"]);
  283. }
  284. else
  285. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByPosition {0}, {1} received null response",
  286. scopeID, regionName);
  287. }
  288. else
  289. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionByName received null reply");
  290. return rinfo;
  291. }
  292. public virtual List<GridRegion> GetRegionsByName(UUID scopeID, string name, int maxNumber)
  293. {
  294. Dictionary<string, object> sendData = new Dictionary<string, object>();
  295. sendData["SCOPEID"] = scopeID.ToString();
  296. sendData["NAME"] = name;
  297. sendData["MAX"] = maxNumber.ToString();
  298. sendData["METHOD"] = "get_regions_by_name";
  299. List<GridRegion> rinfos = new List<GridRegion>();
  300. string reply = string.Empty;
  301. try
  302. {
  303. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  304. m_ServerURI + "/grid",
  305. ServerUtils.BuildQueryString(sendData));
  306. }
  307. catch (Exception e)
  308. {
  309. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  310. return rinfos;
  311. }
  312. if (reply != string.Empty)
  313. {
  314. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  315. if (replyData != null)
  316. {
  317. Dictionary<string, object>.ValueCollection rinfosList = replyData.Values;
  318. foreach (object r in rinfosList)
  319. {
  320. if (r is Dictionary<string, object>)
  321. {
  322. GridRegion rinfo = new GridRegion((Dictionary<string, object>)r);
  323. rinfos.Add(rinfo);
  324. }
  325. else
  326. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionsByName {0}, {1}, {2} received invalid response",
  327. scopeID, name, maxNumber);
  328. }
  329. }
  330. else
  331. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionsByName {0}, {1}, {2} received null response",
  332. scopeID, name, maxNumber);
  333. }
  334. else
  335. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionsByName received null reply");
  336. return rinfos;
  337. }
  338. public virtual List<GridRegion> GetRegionRange(UUID scopeID, int xmin, int xmax, int ymin, int ymax)
  339. {
  340. Dictionary<string, object> sendData = new Dictionary<string, object>();
  341. sendData["SCOPEID"] = scopeID.ToString();
  342. sendData["XMIN"] = xmin.ToString();
  343. sendData["XMAX"] = xmax.ToString();
  344. sendData["YMIN"] = ymin.ToString();
  345. sendData["YMAX"] = ymax.ToString();
  346. sendData["METHOD"] = "get_region_range";
  347. List<GridRegion> rinfos = new List<GridRegion>();
  348. string reply = string.Empty;
  349. try
  350. {
  351. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  352. m_ServerURI + "/grid",
  353. ServerUtils.BuildQueryString(sendData));
  354. //m_log.DebugFormat("[GRID CONNECTOR]: reply was {0}", reply);
  355. }
  356. catch (Exception e)
  357. {
  358. m_log.DebugFormat("[GRID CONNECTOR]: Exception when contacting grid server: {0}", e.Message);
  359. return rinfos;
  360. }
  361. if (reply != string.Empty)
  362. {
  363. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  364. if (replyData != null)
  365. {
  366. Dictionary<string, object>.ValueCollection rinfosList = replyData.Values;
  367. foreach (object r in rinfosList)
  368. {
  369. if (r is Dictionary<string, object>)
  370. {
  371. GridRegion rinfo = new GridRegion((Dictionary<string, object>)r);
  372. rinfos.Add(rinfo);
  373. }
  374. }
  375. }
  376. else
  377. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionRange {0}, {1}-{2} {3}-{4} received null response",
  378. scopeID, xmin, xmax, ymin, ymax);
  379. }
  380. else
  381. m_log.DebugFormat("[GRID CONNECTOR]: GetRegionRange received null reply");
  382. return rinfos;
  383. }
  384. #endregion
  385. }
  386. }