1
0

PresenceServicesConnector.cs 15 KB

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