PresenceServicesConnector.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  99. if (replyData.ContainsKey("result"))
  100. {
  101. if (replyData["result"].ToString().ToLower() == "success")
  102. return true;
  103. else
  104. return false;
  105. }
  106. else
  107. m_log.DebugFormat("[PRESENCE CONNECTOR]: LoginAgent reply data does not contain result field");
  108. }
  109. else
  110. m_log.DebugFormat("[PRESENCE CONNECTOR]: LoginAgent received empty reply");
  111. }
  112. catch (Exception e)
  113. {
  114. m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  115. }
  116. return false;
  117. }
  118. public bool LogoutAgent(UUID sessionID)
  119. {
  120. Dictionary<string, object> sendData = new Dictionary<string, object>();
  121. //sendData["SCOPEID"] = scopeID.ToString();
  122. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  123. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  124. sendData["METHOD"] = "logout";
  125. sendData["SessionID"] = sessionID.ToString();
  126. string reqString = ServerUtils.BuildQueryString(sendData);
  127. string uri = m_ServerURI + "/presence";
  128. // m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
  129. try
  130. {
  131. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  132. uri,
  133. reqString,
  134. m_Auth);
  135. if (reply != string.Empty)
  136. {
  137. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  138. if (replyData.ContainsKey("result"))
  139. {
  140. if (replyData["result"].ToString().ToLower() == "success")
  141. return true;
  142. else
  143. return false;
  144. }
  145. else
  146. m_log.DebugFormat("[PRESENCE CONNECTOR]: LogoutAgent reply data does not contain result field");
  147. }
  148. else
  149. m_log.DebugFormat("[PRESENCE CONNECTOR]: LogoutAgent received empty reply");
  150. }
  151. catch (Exception e)
  152. {
  153. m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  154. }
  155. return false;
  156. }
  157. public bool LogoutRegionAgents(UUID regionID)
  158. {
  159. Dictionary<string, object> sendData = new Dictionary<string, object>();
  160. //sendData["SCOPEID"] = scopeID.ToString();
  161. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  162. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  163. sendData["METHOD"] = "logoutregion";
  164. sendData["RegionID"] = regionID.ToString();
  165. string reqString = ServerUtils.BuildQueryString(sendData);
  166. string uri = m_ServerURI + "/presence";
  167. // m_log.DebugFormat("[PRESENCE 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. if (replyData.ContainsKey("result"))
  178. {
  179. if (replyData["result"].ToString().ToLower() == "success")
  180. return true;
  181. else
  182. return false;
  183. }
  184. else
  185. m_log.DebugFormat("[PRESENCE CONNECTOR]: LogoutRegionAgents reply data does not contain result field");
  186. }
  187. else
  188. m_log.DebugFormat("[PRESENCE CONNECTOR]: LogoutRegionAgents received empty reply");
  189. }
  190. catch (Exception e)
  191. {
  192. m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  193. }
  194. return false;
  195. }
  196. public bool ReportAgent(UUID sessionID, UUID regionID)
  197. {
  198. Dictionary<string, object> sendData = new Dictionary<string, object>();
  199. //sendData["SCOPEID"] = scopeID.ToString();
  200. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  201. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  202. sendData["METHOD"] = "report";
  203. sendData["SessionID"] = sessionID.ToString();
  204. sendData["RegionID"] = regionID.ToString();
  205. string reqString = ServerUtils.BuildQueryString(sendData);
  206. string uri = m_ServerURI + "/presence";
  207. // m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
  208. try
  209. {
  210. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  211. uri,
  212. reqString,
  213. m_Auth);
  214. if (reply != string.Empty)
  215. {
  216. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  217. if (replyData.ContainsKey("result"))
  218. {
  219. if (replyData["result"].ToString().ToLower() == "success")
  220. return true;
  221. else
  222. return false;
  223. }
  224. else
  225. m_log.DebugFormat("[PRESENCE CONNECTOR]: ReportAgent reply data does not contain result field");
  226. }
  227. else
  228. m_log.DebugFormat("[PRESENCE CONNECTOR]: ReportAgent received empty reply");
  229. }
  230. catch (Exception e)
  231. {
  232. m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  233. }
  234. return false;
  235. }
  236. public PresenceInfo GetAgent(UUID sessionID)
  237. {
  238. Dictionary<string, object> sendData = new Dictionary<string, object>();
  239. //sendData["SCOPEID"] = scopeID.ToString();
  240. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  241. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  242. sendData["METHOD"] = "getagent";
  243. sendData["SessionID"] = sessionID.ToString();
  244. string reply = string.Empty;
  245. string reqString = ServerUtils.BuildQueryString(sendData);
  246. string uri = m_ServerURI + "/presence";
  247. // m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
  248. try
  249. {
  250. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  251. uri,
  252. reqString,
  253. m_Auth);
  254. if (reply == null || (reply != null && reply == string.Empty))
  255. {
  256. m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgent received null or empty reply");
  257. return null;
  258. }
  259. }
  260. catch (Exception e)
  261. {
  262. m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  263. return null;
  264. }
  265. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  266. PresenceInfo pinfo = null;
  267. if ((replyData != null) && replyData.ContainsKey("result") && (replyData["result"] != null))
  268. {
  269. if (replyData["result"] is Dictionary<string, object>)
  270. {
  271. pinfo = new PresenceInfo((Dictionary<string, object>)replyData["result"]);
  272. }
  273. else
  274. {
  275. if (replyData["result"].ToString() == "null")
  276. return null;
  277. m_log.DebugFormat("[PRESENCE CONNECTOR]: Invalid reply (result not dictionary) received from presence server when querying for sessionID {0}", sessionID.ToString());
  278. }
  279. }
  280. else
  281. {
  282. m_log.DebugFormat("[PRESENCE CONNECTOR]: Invalid reply received from presence server when querying for sessionID {0}", sessionID.ToString());
  283. }
  284. return pinfo;
  285. }
  286. public PresenceInfo[] GetAgents(string[] userIDs)
  287. {
  288. Dictionary<string, object> sendData = new Dictionary<string, object>();
  289. //sendData["SCOPEID"] = scopeID.ToString();
  290. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  291. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  292. sendData["METHOD"] = "getagents";
  293. sendData["uuids"] = new List<string>(userIDs);
  294. string reply = string.Empty;
  295. string reqString = ServerUtils.BuildQueryString(sendData);
  296. string uri = m_ServerURI + "/presence";
  297. //m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString);
  298. try
  299. {
  300. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  301. uri,
  302. reqString,
  303. m_Auth);
  304. if (reply == null || (reply != null && reply == string.Empty))
  305. {
  306. m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgents received null or empty reply");
  307. return null;
  308. }
  309. }
  310. catch (Exception e)
  311. {
  312. m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  313. }
  314. List<PresenceInfo> rinfos = new List<PresenceInfo>();
  315. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  316. if (replyData != null)
  317. {
  318. if (replyData.ContainsKey("result") &&
  319. (replyData["result"].ToString() == "null" || replyData["result"].ToString() == "Failure"))
  320. {
  321. return new PresenceInfo[0];
  322. }
  323. Dictionary<string, object>.ValueCollection pinfosList = replyData.Values;
  324. //m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgents returned {0} elements", pinfosList.Count);
  325. foreach (object presence in pinfosList)
  326. {
  327. if (presence is Dictionary<string, object>)
  328. {
  329. PresenceInfo pinfo = new PresenceInfo((Dictionary<string, object>)presence);
  330. rinfos.Add(pinfo);
  331. }
  332. else
  333. m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgents received invalid response type {0}",
  334. presence.GetType());
  335. }
  336. }
  337. else
  338. m_log.DebugFormat("[PRESENCE CONNECTOR]: GetAgents received null response");
  339. return rinfos.ToArray();
  340. }
  341. #endregion
  342. }
  343. }