1
0

GridUserServicesConnector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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.ServiceAuth;
  35. using OpenSim.Services.Interfaces;
  36. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  37. using OpenSim.Server.Base;
  38. using OpenMetaverse;
  39. namespace OpenSim.Services.Connectors
  40. {
  41. public class GridUserServicesConnector : BaseServiceConnector, IGridUserService
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(
  45. MethodBase.GetCurrentMethod().DeclaringType);
  46. private string m_ServerURI = String.Empty;
  47. public GridUserServicesConnector()
  48. {
  49. }
  50. public GridUserServicesConnector(string serverURI)
  51. {
  52. m_ServerURI = serverURI.TrimEnd('/');
  53. }
  54. public GridUserServicesConnector(IConfigSource source)
  55. {
  56. Initialise(source);
  57. }
  58. public virtual void Initialise(IConfigSource source)
  59. {
  60. IConfig gridConfig = source.Configs["GridUserService"];
  61. if (gridConfig == null)
  62. {
  63. m_log.Error("[GRID USER CONNECTOR]: GridUserService missing from OpenSim.ini");
  64. throw new Exception("GridUser connector init error");
  65. }
  66. string serviceURI = gridConfig.GetString("GridUserServerURI", string.Empty);
  67. if (string.IsNullOrWhiteSpace(serviceURI))
  68. {
  69. m_log.Error("[GRIDUSER CONNECTOR]: GridUserServerURI not found section GridUserService");
  70. throw new Exception("GridUser connector init error");
  71. }
  72. OSHHTPHost tmp = new OSHHTPHost(serviceURI, true);
  73. if (!tmp.IsResolvedHost)
  74. {
  75. m_log.ErrorFormat("[GRIDUSER CONNECTOR]: {0}", tmp.IsValidHost ? "Could not resolve GridUserServerURI" : "GridUserServerURI is a invalid host");
  76. throw new Exception("User account connector init error");
  77. }
  78. m_ServerURI = tmp.URI;
  79. base.Initialise(source, "GridUserService");
  80. }
  81. #region IGridUserService
  82. public GridUserInfo LoggedIn(string userID)
  83. {
  84. Dictionary<string, object> sendData = new Dictionary<string, object>();
  85. //sendData["SCOPEID"] = scopeID.ToString();
  86. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  87. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  88. sendData["METHOD"] = "loggedin";
  89. sendData["UserID"] = userID;
  90. return Get(sendData);
  91. }
  92. public bool LoggedOut(string userID, UUID sessionID, UUID region, Vector3 position, Vector3 lookat)
  93. {
  94. Dictionary<string, object> sendData = new Dictionary<string, object>();
  95. //sendData["SCOPEID"] = scopeID.ToString();
  96. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  97. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  98. sendData["METHOD"] = "loggedout";
  99. return Set(sendData, userID, region, position, lookat);
  100. }
  101. public bool SetHome(string userID, UUID regionID, Vector3 position, Vector3 lookAt)
  102. {
  103. Dictionary<string, object> sendData = new Dictionary<string, object>();
  104. //sendData["SCOPEID"] = scopeID.ToString();
  105. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  106. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  107. sendData["METHOD"] = "sethome";
  108. return Set(sendData, userID, regionID, position, lookAt);
  109. }
  110. public bool SetLastPosition(string userID, UUID sessionID, UUID regionID, Vector3 position, Vector3 lookAt)
  111. {
  112. Dictionary<string, object> sendData = new Dictionary<string, object>();
  113. //sendData["SCOPEID"] = scopeID.ToString();
  114. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  115. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  116. sendData["METHOD"] = "setposition";
  117. return Set(sendData, userID, regionID, position, lookAt);
  118. }
  119. public GridUserInfo GetGridUserInfo(string userID)
  120. {
  121. Dictionary<string, object> sendData = new Dictionary<string, object>();
  122. //sendData["SCOPEID"] = scopeID.ToString();
  123. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  124. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  125. sendData["METHOD"] = "getgriduserinfo";
  126. sendData["UserID"] = userID;
  127. return Get(sendData);
  128. }
  129. #endregion
  130. protected bool Set(Dictionary<string, object> sendData, string userID, UUID regionID, Vector3 position, Vector3 lookAt)
  131. {
  132. sendData["UserID"] = userID;
  133. sendData["RegionID"] = regionID.ToString();
  134. sendData["Position"] = position.ToString();
  135. sendData["LookAt"] = lookAt.ToString();
  136. string reqString = ServerUtils.BuildQueryString(sendData);
  137. string uri = m_ServerURI + "/griduser";
  138. // m_log.DebugFormat("[GRID USER CONNECTOR]: queryString = {0}", reqString);
  139. try
  140. {
  141. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  142. uri,
  143. reqString,
  144. m_Auth);
  145. if (reply != string.Empty)
  146. {
  147. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  148. if (replyData.ContainsKey("result"))
  149. {
  150. if (replyData["result"].ToString().ToLower() == "success")
  151. return true;
  152. else
  153. return false;
  154. }
  155. else
  156. m_log.DebugFormat("[GRID USER CONNECTOR]: SetPosition reply data does not contain result field");
  157. }
  158. else
  159. m_log.DebugFormat("[GRID USER CONNECTOR]: SetPosition received empty reply");
  160. }
  161. catch (Exception e)
  162. {
  163. m_log.DebugFormat("[GRID USER CONNECTOR]: Exception when contacting grid user server at {0}: {1}", uri, e.Message);
  164. }
  165. return false;
  166. }
  167. protected GridUserInfo Get(Dictionary<string, object> sendData)
  168. {
  169. string reqString = ServerUtils.BuildQueryString(sendData);
  170. string uri = m_ServerURI + "/griduser";
  171. // m_log.DebugFormat("[GRID USER CONNECTOR]: queryString = {0}", reqString);
  172. try
  173. {
  174. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  175. uri,
  176. reqString,
  177. m_Auth);
  178. if (reply != string.Empty)
  179. {
  180. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  181. GridUserInfo guinfo = null;
  182. if ((replyData != null) && replyData.ContainsKey("result") && (replyData["result"] != null))
  183. {
  184. if (replyData["result"] is Dictionary<string, object>)
  185. guinfo = Create((Dictionary<string, object>)replyData["result"]);
  186. }
  187. return guinfo;
  188. }
  189. else
  190. m_log.DebugFormat("[GRID USER CONNECTOR]: Get received empty reply");
  191. }
  192. catch (Exception e)
  193. {
  194. m_log.DebugFormat("[GRID USER CONNECTOR]: Exception when contacting grid user server at {0}: {1}", uri, e.Message);
  195. }
  196. return null;
  197. }
  198. public GridUserInfo[] GetGridUserInfo(string[] userIDs)
  199. {
  200. Dictionary<string, object> sendData = new Dictionary<string, object>();
  201. //sendData["SCOPEID"] = scopeID.ToString();
  202. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  203. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  204. sendData["METHOD"] = "getgriduserinfos";
  205. sendData["AgentIDs"] = new List<string>(userIDs);
  206. string reply = string.Empty;
  207. string reqString = ServerUtils.BuildQueryString(sendData);
  208. string uri = m_ServerURI + "/griduser";
  209. //m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
  210. try
  211. {
  212. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  213. uri,
  214. reqString,
  215. m_Auth);
  216. if (string.IsNullOrEmpty(reply))
  217. {
  218. m_log.DebugFormat("[GRID USER CONNECTOR]: GetGridUserInfo received null or empty reply");
  219. return null;
  220. }
  221. }
  222. catch (Exception e)
  223. {
  224. m_log.DebugFormat("[GRID USER CONNECTOR]: Exception when contacting grid user server at {0}: {1}", uri, e.Message);
  225. }
  226. List<GridUserInfo> rinfos = new List<GridUserInfo>();
  227. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  228. if (replyData != null)
  229. {
  230. if (replyData.ContainsKey("result") &&
  231. (replyData["result"].ToString() == "null" || replyData["result"].ToString() == "Failure"))
  232. {
  233. return new GridUserInfo[0];
  234. }
  235. Dictionary<string, object>.ValueCollection pinfosList = replyData.Values;
  236. //m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgents returned {0} elements", pinfosList.Count);
  237. foreach (object griduser in pinfosList)
  238. {
  239. if (griduser is Dictionary<string, object>)
  240. {
  241. GridUserInfo pinfo = Create((Dictionary<string, object>)griduser);
  242. rinfos.Add(pinfo);
  243. }
  244. else
  245. m_log.DebugFormat("[GRID USER CONNECTOR]: GetGridUserInfo received invalid response type {0}",
  246. griduser.GetType());
  247. }
  248. }
  249. else
  250. m_log.DebugFormat("[GRID USER CONNECTOR]: GetGridUserInfo received null response");
  251. return rinfos.ToArray();
  252. }
  253. protected virtual GridUserInfo Create(Dictionary<string, object> griduser)
  254. {
  255. return new GridUserInfo(griduser);
  256. }
  257. }
  258. }