PresenceServicesConnector.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 PresenceServicesConnector : BaseServiceConnector, IPresenceService
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(
  45. MethodBase.GetCurrentMethod().DeclaringType);
  46. private string m_ServerURI = String.Empty;
  47. public PresenceServicesConnector()
  48. {
  49. }
  50. public PresenceServicesConnector(string serverURI)
  51. {
  52. m_ServerURI = serverURI.TrimEnd('/');
  53. }
  54. public PresenceServicesConnector(IConfigSource source)
  55. {
  56. Initialise(source);
  57. }
  58. public virtual void Initialise(IConfigSource source)
  59. {
  60. IConfig gridConfig = source.Configs["PresenceService"];
  61. if (gridConfig == null)
  62. {
  63. m_log.Error("[PRESENCE CONNECTOR]: PresenceService missing from OpenSim.ini");
  64. throw new Exception("Presence connector init error");
  65. }
  66. string serviceURI = gridConfig.GetString("PresenceServerURI",
  67. String.Empty);
  68. if (serviceURI == String.Empty)
  69. {
  70. m_log.Error("[PRESENCE CONNECTOR]: No Server URI named in section PresenceService");
  71. throw new Exception("Presence connector init error");
  72. }
  73. m_ServerURI = serviceURI;
  74. base.Initialise(source, "PresenceService");
  75. }
  76. #region IPresenceService
  77. public bool LoginAgent(string userID, UUID sessionID, UUID secureSessionID)
  78. {
  79. Dictionary<string, object> sendData = new Dictionary<string, object>();
  80. //sendData["SCOPEID"] = scopeID.ToString();
  81. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  82. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  83. sendData["METHOD"] = "login";
  84. sendData["UserID"] = userID;
  85. sendData["SessionID"] = sessionID.ToString();
  86. sendData["SecureSessionID"] = secureSessionID.ToString();
  87. string reqString = ServerUtils.BuildQueryString(sendData);
  88. string uri = m_ServerURI + "/presence";
  89. // m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
  90. try
  91. {
  92. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  93. uri,
  94. reqString,
  95. m_Auth);
  96. if (reply != string.Empty)
  97. {
  98. int indx = reply.IndexOf("success", StringComparison.InvariantCultureIgnoreCase);
  99. if (indx > 0)
  100. return true;
  101. return false;
  102. }
  103. else
  104. m_log.DebugFormat("[PRESENCE CONNECTOR]: LoginAgent received empty reply");
  105. }
  106. catch (Exception e)
  107. {
  108. m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  109. }
  110. return false;
  111. }
  112. public bool LogoutAgent(UUID sessionID)
  113. {
  114. Dictionary<string, object> sendData = new Dictionary<string, object>();
  115. //sendData["SCOPEID"] = scopeID.ToString();
  116. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  117. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  118. sendData["METHOD"] = "logout";
  119. sendData["SessionID"] = sessionID.ToString();
  120. string reqString = ServerUtils.BuildQueryString(sendData);
  121. string uri = m_ServerURI + "/presence";
  122. // m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
  123. try
  124. {
  125. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  126. uri,
  127. reqString,
  128. m_Auth);
  129. if (reply != string.Empty)
  130. {
  131. int indx = reply.IndexOf("success", StringComparison.InvariantCultureIgnoreCase);
  132. if (indx > 0)
  133. return true;
  134. return false;
  135. }
  136. else
  137. m_log.DebugFormat("[PRESENCE CONNECTOR]: LogoutAgent received empty reply");
  138. }
  139. catch (Exception e)
  140. {
  141. m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  142. }
  143. return false;
  144. }
  145. public bool LogoutRegionAgents(UUID regionID)
  146. {
  147. Dictionary<string, object> sendData = new Dictionary<string, object>();
  148. //sendData["SCOPEID"] = scopeID.ToString();
  149. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  150. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  151. sendData["METHOD"] = "logoutregion";
  152. sendData["RegionID"] = regionID.ToString();
  153. string reqString = ServerUtils.BuildQueryString(sendData);
  154. string uri = m_ServerURI + "/presence";
  155. // m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
  156. try
  157. {
  158. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  159. uri,
  160. reqString,
  161. m_Auth);
  162. if (reply != string.Empty)
  163. {
  164. int indx = reply.IndexOf("success", StringComparison.InvariantCultureIgnoreCase);
  165. if (indx > 0)
  166. return true;
  167. return false;
  168. }
  169. else
  170. m_log.DebugFormat("[PRESENCE CONNECTOR]: LogoutRegionAgents received empty reply");
  171. }
  172. catch (Exception e)
  173. {
  174. m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  175. }
  176. return false;
  177. }
  178. public bool ReportAgent(UUID sessionID, UUID regionID)
  179. {
  180. Dictionary<string, object> sendData = new Dictionary<string, object>();
  181. //sendData["SCOPEID"] = scopeID.ToString();
  182. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  183. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  184. sendData["METHOD"] = "report";
  185. sendData["SessionID"] = sessionID.ToString();
  186. sendData["RegionID"] = regionID.ToString();
  187. string reqString = ServerUtils.BuildQueryString(sendData);
  188. string uri = m_ServerURI + "/presence";
  189. // m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
  190. try
  191. {
  192. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  193. uri,
  194. reqString,
  195. m_Auth);
  196. if (reply != string.Empty)
  197. {
  198. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  199. if (replyData.ContainsKey("result"))
  200. {
  201. if (replyData["result"].ToString().ToLower() == "success")
  202. return true;
  203. else
  204. return false;
  205. }
  206. else
  207. m_log.DebugFormat("[PRESENCE CONNECTOR]: ReportAgent reply data does not contain result field");
  208. }
  209. else
  210. m_log.DebugFormat("[PRESENCE CONNECTOR]: ReportAgent received empty reply");
  211. }
  212. catch (Exception e)
  213. {
  214. m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  215. }
  216. return false;
  217. }
  218. public PresenceInfo GetAgent(UUID sessionID)
  219. {
  220. Dictionary<string, object> sendData = new Dictionary<string, object>();
  221. //sendData["SCOPEID"] = scopeID.ToString();
  222. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  223. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  224. sendData["METHOD"] = "getagent";
  225. sendData["SessionID"] = sessionID.ToString();
  226. string reply = string.Empty;
  227. string reqString = ServerUtils.BuildQueryString(sendData);
  228. string uri = m_ServerURI + "/presence";
  229. // m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
  230. try
  231. {
  232. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  233. uri,
  234. reqString,
  235. m_Auth);
  236. if (reply == null || (reply != null && reply == string.Empty))
  237. {
  238. m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgent received null or empty reply");
  239. return null;
  240. }
  241. }
  242. catch (Exception e)
  243. {
  244. m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  245. return null;
  246. }
  247. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  248. PresenceInfo pinfo = null;
  249. if ((replyData != null) && replyData.ContainsKey("result") && (replyData["result"] != null))
  250. {
  251. if (replyData["result"] is Dictionary<string, object>)
  252. {
  253. pinfo = new PresenceInfo((Dictionary<string, object>)replyData["result"]);
  254. }
  255. else
  256. {
  257. if (replyData["result"].ToString() == "null")
  258. return null;
  259. m_log.DebugFormat("[PRESENCE CONNECTOR]: Invalid reply (result not dictionary) received from presence server when querying for sessionID {0}", sessionID.ToString());
  260. }
  261. }
  262. else
  263. {
  264. m_log.DebugFormat("[PRESENCE CONNECTOR]: Invalid reply received from presence server when querying for sessionID {0}", sessionID.ToString());
  265. }
  266. return pinfo;
  267. }
  268. public PresenceInfo[] GetAgents(string[] userIDs)
  269. {
  270. Dictionary<string, object> sendData = new Dictionary<string, object>();
  271. //sendData["SCOPEID"] = scopeID.ToString();
  272. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  273. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  274. sendData["METHOD"] = "getagents";
  275. sendData["uuids"] = new List<string>(userIDs);
  276. string reply = string.Empty;
  277. string reqString = ServerUtils.BuildQueryString(sendData);
  278. string uri = m_ServerURI + "/presence";
  279. //m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
  280. try
  281. {
  282. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  283. uri,
  284. reqString,
  285. m_Auth);
  286. if (reply == null || (reply != null && reply == string.Empty))
  287. {
  288. m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgents received null or empty reply");
  289. return null;
  290. }
  291. }
  292. catch (Exception e)
  293. {
  294. m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  295. }
  296. List<PresenceInfo> rinfos = new List<PresenceInfo>();
  297. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  298. if (replyData != null)
  299. {
  300. if (replyData.ContainsKey("result") &&
  301. (replyData["result"].ToString() == "null" || replyData["result"].ToString() == "Failure"))
  302. {
  303. return new PresenceInfo[0];
  304. }
  305. Dictionary<string, object>.ValueCollection pinfosList = replyData.Values;
  306. //m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgents returned {0} elements", pinfosList.Count);
  307. foreach (object presence in pinfosList)
  308. {
  309. if (presence is Dictionary<string, object>)
  310. {
  311. PresenceInfo pinfo = new PresenceInfo((Dictionary<string, object>)presence);
  312. rinfos.Add(pinfo);
  313. }
  314. else
  315. m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgents received invalid response type {0}",
  316. presence.GetType());
  317. }
  318. }
  319. else
  320. m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgents received null response");
  321. return rinfos.ToArray();
  322. }
  323. #endregion
  324. }
  325. }