PresenceServiceConnector.cs 15 KB

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