GridUserServicesConnector.cs 12 KB

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