1
0

PresenceServicesConnector.cs 15 KB

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