1
0

PresenceServicesConnector.cs 16 KB

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