PresenceServiceConnector.cs 15 KB

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