MessageService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 OpenSim 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. */
  28. using System;
  29. using System.Net;
  30. using System.Net.Sockets;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. //using System.Xml;
  34. using libsecondlife;
  35. using Nwc.XmlRpc;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Console;
  38. using OpenSim.Framework.Data;
  39. using OpenSim.Framework.Servers;
  40. using FriendRights = libsecondlife.FriendRights;
  41. namespace OpenSim.Grid.MessagingServer
  42. {
  43. public class MessageService
  44. {
  45. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  46. private MessageServerConfig m_cfg;
  47. //A hashtable of all current presences this server knows about
  48. private Hashtable m_presences = new Hashtable();
  49. //a hashtable of all current regions this server knows about
  50. private Hashtable m_regionInfoCache = new Hashtable();
  51. //A hashtable containing lists of UUIDs keyed by UUID for fast backreferencing
  52. private Hashtable m_presence_BackReferences = new Hashtable();
  53. // Hashtable containing work units that need to be processed
  54. private Hashtable m_unProcessedWorkUnits = new Hashtable();
  55. public MessageService(MessageServerConfig cfg)
  56. {
  57. m_cfg = cfg;
  58. }
  59. #region RegionComms Methods
  60. /// <summary>
  61. /// Informs a region about an Agent
  62. /// </summary>
  63. /// <param name="TalkingAbout">User to talk about</param>
  64. /// <param name="UserToUpdate">User we're sending this too (contains the region)</param>
  65. public void SendRegionPresenceUpdate(UserPresenceData TalkingAbout, UserPresenceData UserToUpdate)
  66. {
  67. // TODO: Fill in pertenant Presence Data from 'TalkingAbout'
  68. RegionProfileData whichRegion = UserToUpdate.regionData;
  69. //whichRegion.httpServerURI
  70. Hashtable PresenceParams = new Hashtable();
  71. ArrayList SendParams = new ArrayList();
  72. SendParams.Add(PresenceParams);
  73. m_log.Info("[PRESENCE]: Informing " + whichRegion.regionName + " at " + whichRegion.httpServerURI);
  74. // Send
  75. XmlRpcRequest RegionReq = new XmlRpcRequest("presence_update", SendParams);
  76. XmlRpcResponse RegionResp = RegionReq.Send(whichRegion.httpServerURI, 6000);
  77. }
  78. #endregion
  79. #region FriendList Methods
  80. /// <summary>
  81. /// Process Friendlist subscriptions for a user
  82. /// The login method calls this for a User
  83. /// </summary>
  84. /// <param name="userpresence">The Agent we're processing the friendlist subscriptions</param>
  85. public void ProcessFriendListSubscriptions(UserPresenceData userpresence)
  86. {
  87. List<FriendListItem> uFriendList = userpresence.friendData;
  88. for (int i = 0; i < uFriendList.Count; i++)
  89. {
  90. m_presence_BackReferences.Add(userpresence.agentData.AgentID, uFriendList[i].Friend);
  91. m_presence_BackReferences.Add(uFriendList[i].Friend, userpresence.agentData.AgentID);
  92. if (m_presences.Contains(uFriendList[i].Friend))
  93. {
  94. UserPresenceData friendup = (UserPresenceData)m_presences[uFriendList[i]];
  95. // Add backreference
  96. SubscribeToPresenceUpdates(userpresence, friendup, uFriendList[i],i);
  97. }
  98. }
  99. m_presences.Add(userpresence.agentData.AgentID, userpresence);
  100. }
  101. /// <summary>
  102. /// Does the necessary work to subscribe one agent to another's presence notifications
  103. /// Gets called by ProcessFriendListSubscriptions. You shouldn't call this directly
  104. /// unless you know what you're doing
  105. /// </summary>
  106. /// <param name="userpresence">P1</param>
  107. /// <param name="friendpresence">P2</param>
  108. /// <param name="uFriendListItem"></param>
  109. /// <param name="uFriendListIndex"></param>
  110. public void SubscribeToPresenceUpdates(UserPresenceData userpresence, UserPresenceData friendpresence,
  111. FriendListItem uFriendListItem, int uFriendListIndex)
  112. {
  113. if ((uFriendListItem.FriendListOwnerPerms & (uint)FriendRights.CanSeeOnline) != 0)
  114. {
  115. // Subscribe and Send Out updates
  116. if (!friendpresence.subscriptionData.Contains(friendpresence.agentData.AgentID))
  117. {
  118. userpresence.subscriptionData.Add(friendpresence.agentData.AgentID);
  119. //Send Region Notice....
  120. }
  121. else
  122. {
  123. // we need to send out online status update, but the user is already subscribed
  124. }
  125. SendRegionPresenceUpdate(friendpresence, userpresence);
  126. }
  127. if ((uFriendListItem.FriendPerms & (uint)FriendRights.CanSeeOnline) != 0)
  128. {
  129. if (!friendpresence.subscriptionData.Contains(userpresence.agentData.AgentID))
  130. {
  131. friendpresence.subscriptionData.Add(userpresence.agentData.AgentID);
  132. //Send Region Notice....
  133. }
  134. else
  135. {
  136. // we need to send out online status update, but the user is already subscribed
  137. }
  138. SendRegionPresenceUpdate(userpresence, friendpresence);
  139. }
  140. }
  141. /// <summary>
  142. /// Adds a backreference so presence specific data doesn't have to be
  143. /// enumerated for each logged in user every time someone logs on or off.
  144. /// </summary>
  145. /// <param name="agentID"></param>
  146. /// <param name="friendID"></param>
  147. public void addBackReference(LLUUID agentID, LLUUID friendID)
  148. {
  149. if (m_presence_BackReferences.Contains(friendID))
  150. {
  151. List<LLUUID> presenseBackReferences = (List<LLUUID>)m_presence_BackReferences[friendID];
  152. if (!presenseBackReferences.Contains(agentID))
  153. {
  154. presenseBackReferences.Add(agentID);
  155. }
  156. m_presence_BackReferences[friendID] = presenseBackReferences;
  157. }
  158. else
  159. {
  160. List<LLUUID> presenceBackReferences = new List<LLUUID>();
  161. presenceBackReferences.Add(agentID);
  162. m_presence_BackReferences[friendID] = presenceBackReferences;
  163. }
  164. }
  165. /// <summary>
  166. /// Removes a backreference to free up some memory
  167. /// </summary>
  168. /// <param name="agentID"></param>
  169. /// <param name="friendID"></param>
  170. public void removeBackReference(LLUUID agentID, LLUUID friendID)
  171. {
  172. if (m_presence_BackReferences.Contains(friendID))
  173. {
  174. List<LLUUID> presenseBackReferences = (List<LLUUID>)m_presence_BackReferences[friendID];
  175. if (presenseBackReferences.Contains(agentID))
  176. {
  177. presenseBackReferences.Remove(agentID);
  178. }
  179. // If there are no more backreferences for this agent,
  180. // remove it to free up memory.
  181. if (presenseBackReferences.Count == 0)
  182. {
  183. m_presence_BackReferences.Remove(agentID);
  184. }
  185. }
  186. }
  187. /// <summary>
  188. /// Logoff Processor. Call this to clean up agent presence data and send logoff presence notifications
  189. /// </summary>
  190. /// <param name="AgentID"></param>
  191. private void ProcessLogOff(LLUUID AgentID)
  192. {
  193. if (m_presences.Contains(AgentID))
  194. {
  195. UserPresenceData AgentData = (UserPresenceData)m_presences[AgentID];
  196. if (m_presence_BackReferences.Contains(AgentID))
  197. {
  198. List<LLUUID> AgentsNeedingNotification = (List<LLUUID>)m_presence_BackReferences[AgentID];
  199. for (int i = 0; i < AgentsNeedingNotification.Count; i++)
  200. {
  201. // TODO: Do Region Notifications
  202. if (m_presences.Contains(AgentsNeedingNotification[i]))
  203. {
  204. UserPresenceData friendd = (UserPresenceData)m_presences[AgentsNeedingNotification[i]];
  205. // This might need to be enumerated and checked before we try to remove it.
  206. friendd.subscriptionData.Remove(AgentID);
  207. List<FriendListItem> fl = friendd.friendData;
  208. for (int j = 0; j < fl.Count; j++)
  209. {
  210. if (fl[j].Friend == AgentID)
  211. {
  212. fl[j].onlinestatus = false;
  213. }
  214. }
  215. friendd.friendData = fl;
  216. SendRegionPresenceUpdate(AgentData, friendd);
  217. }
  218. removeBackReference(AgentID, AgentsNeedingNotification[i]);
  219. }
  220. }
  221. }
  222. }
  223. #endregion
  224. #region UserServer Comms
  225. /// <summary>
  226. /// Returns a list of FriendsListItems that describe the friends and permissions in the friend relationship for LLUUID friendslistowner
  227. /// </summary>
  228. /// <param name="friendlistowner">The agent that we're retreiving the friends Data.</param>
  229. public List<FriendListItem> GetUserFriendList(LLUUID friendlistowner)
  230. {
  231. List<FriendListItem> buddylist = new List<FriendListItem>();
  232. try
  233. {
  234. Hashtable param = new Hashtable();
  235. param["ownerID"] = friendlistowner.UUID.ToString();
  236. IList parameters = new ArrayList();
  237. parameters.Add(param);
  238. XmlRpcRequest req = new XmlRpcRequest("get_user_friend_list", parameters);
  239. XmlRpcResponse resp = req.Send(m_cfg.UserServerURL, 3000);
  240. Hashtable respData = (Hashtable)resp.Value;
  241. if (respData.Contains("avcount"))
  242. {
  243. buddylist = ConvertXMLRPCDataToFriendListItemList(respData);
  244. }
  245. }
  246. catch (WebException e)
  247. {
  248. m_log.Warn("Error when trying to fetch Avatar's friends list: " +
  249. e.Message);
  250. // Return Empty list (no friends)
  251. }
  252. return buddylist;
  253. }
  254. /// <summary>
  255. /// Converts XMLRPC Friend List to FriendListItem Object
  256. /// </summary>
  257. /// <param name="data">XMLRPC response data Hashtable</param>
  258. /// <returns></returns>
  259. public List<FriendListItem> ConvertXMLRPCDataToFriendListItemList(Hashtable data)
  260. {
  261. List<FriendListItem> buddylist = new List<FriendListItem>();
  262. int buddycount = Convert.ToInt32((string)data["avcount"]);
  263. for (int i = 0; i < buddycount; i++)
  264. {
  265. FriendListItem buddylistitem = new FriendListItem();
  266. buddylistitem.FriendListOwner = new LLUUID((string)data["ownerID" + i.ToString()]);
  267. buddylistitem.Friend = new LLUUID((string)data["friendID" + i.ToString()]);
  268. buddylistitem.FriendListOwnerPerms = (uint)Convert.ToInt32((string)data["ownerPerms" + i.ToString()]);
  269. buddylistitem.FriendPerms = (uint)Convert.ToInt32((string)data["friendPerms" + i.ToString()]);
  270. buddylist.Add(buddylistitem);
  271. }
  272. return buddylist;
  273. }
  274. /// <summary>
  275. /// UserServer sends an expect_user method
  276. /// this handles the method and provisions the
  277. /// necessary info for presence to work
  278. /// </summary>
  279. /// <param name="request">UserServer Data</param>
  280. /// <returns></returns>
  281. public XmlRpcResponse UserLoggedOn(XmlRpcRequest request)
  282. {
  283. Hashtable requestData = (Hashtable)request.Params[0];
  284. AgentCircuitData agentData = new AgentCircuitData();
  285. agentData.SessionID = new LLUUID((string)requestData["session_id"]);
  286. agentData.SecureSessionID = new LLUUID((string)requestData["secure_session_id"]);
  287. agentData.firstname = (string)requestData["firstname"];
  288. agentData.lastname = (string)requestData["lastname"];
  289. agentData.AgentID = new LLUUID((string)requestData["agent_id"]);
  290. agentData.circuitcode = Convert.ToUInt32(requestData["circuit_code"]);
  291. agentData.CapsPath = (string)requestData["caps_path"];
  292. if (requestData.ContainsKey("child_agent") && requestData["child_agent"].Equals("1"))
  293. {
  294. agentData.child = true;
  295. }
  296. else
  297. {
  298. agentData.startpos =
  299. new LLVector3(Convert.ToUInt32(requestData["startpos_x"]),
  300. Convert.ToUInt32(requestData["startpos_y"]),
  301. Convert.ToUInt32(requestData["startpos_z"]));
  302. agentData.child = false;
  303. }
  304. ulong regionHandle = Convert.ToUInt64((string)requestData["regionhandle"]);
  305. UserPresenceData up = new UserPresenceData();
  306. up.agentData = agentData;
  307. List<FriendListItem> flData = GetUserFriendList(agentData.AgentID);
  308. up.friendData = flData;
  309. RegionProfileData riData = GetRegionInfo(regionHandle);
  310. up.regionData = riData;
  311. ProcessFriendListSubscriptions(up);
  312. return new XmlRpcResponse();
  313. }
  314. /// <summary>
  315. /// The UserServer got a Logoff message
  316. /// Cleanup time for that user. Send out presence notifications
  317. /// </summary>
  318. /// <param name="request"></param>
  319. /// <returns></returns>
  320. public XmlRpcResponse UserLoggedOff(XmlRpcRequest request)
  321. {
  322. Hashtable requestData = (Hashtable)request.Params[0];
  323. LLUUID AgentID = new LLUUID((string)requestData["agent_id"]);
  324. //ProcessLogOff(AgentID);
  325. return new XmlRpcResponse();
  326. }
  327. #endregion
  328. #region regioninfo gathering
  329. /// <summary>
  330. /// Gets and caches a RegionInfo object from the gridserver based on regionhandle
  331. /// if the regionhandle is already cached, use the cached values
  332. /// </summary>
  333. /// <param name="regionhandle">handle to the XY of the region we're looking for</param>
  334. /// <returns>A RegionInfo object to stick in the presence info</returns>
  335. public RegionProfileData GetRegionInfo(ulong regionhandle)
  336. {
  337. RegionProfileData regionInfo = null;
  338. if (m_regionInfoCache.Contains(regionhandle))
  339. {
  340. regionInfo = (RegionProfileData)m_regionInfoCache[regionhandle];
  341. }
  342. else
  343. {
  344. regionInfo = RequestRegionInfo(regionhandle);
  345. }
  346. return regionInfo;
  347. }
  348. /// <summary>
  349. /// Get RegionProfileData from the GridServer
  350. /// We'll Cache this information and use it for presence updates
  351. /// </summary>
  352. /// <param name="regionHandle"></param>
  353. /// <returns></returns>
  354. public RegionProfileData RequestRegionInfo(ulong regionHandle)
  355. { RegionProfileData regionProfile = null;
  356. try
  357. {
  358. Hashtable requestData = new Hashtable();
  359. requestData["region_handle"] = regionHandle.ToString();
  360. requestData["authkey"] = m_cfg.GridSendKey;
  361. ArrayList SendParams = new ArrayList();
  362. SendParams.Add(requestData);
  363. XmlRpcRequest GridReq = new XmlRpcRequest("simulator_data_request", SendParams);
  364. XmlRpcResponse GridResp = GridReq.Send(m_cfg.GridServerURL, 3000);
  365. Hashtable responseData = (Hashtable)GridResp.Value;
  366. if (responseData.ContainsKey("error"))
  367. {
  368. m_log.Error("[GRID]: error received from grid server" + responseData["error"]);
  369. return null;
  370. }
  371. uint regX = Convert.ToUInt32((string)responseData["region_locx"]);
  372. uint regY = Convert.ToUInt32((string)responseData["region_locy"]);
  373. string internalIpStr = (string)responseData["sim_ip"];
  374. uint port = Convert.ToUInt32(responseData["sim_port"]);
  375. string externalUri = (string)responseData["sim_uri"];
  376. string neighbourExternalUri = externalUri;
  377. regionProfile = new RegionProfileData();
  378. regionProfile.httpPort = (uint)Convert.ToInt32((string)responseData["http_port"]);
  379. regionProfile.httpServerURI = "http://" + internalIpStr + ":" + regionProfile.httpPort + "/";
  380. regionProfile.regionHandle = Helpers.UIntsToLong((regX * Constants.RegionSize), (regY * Constants.RegionSize));
  381. regionProfile.regionLocX = regX;
  382. regionProfile.regionLocY = regY;
  383. regionProfile.remotingPort = Convert.ToUInt32((string)responseData["remoting_port"]);
  384. regionProfile.UUID = new LLUUID((string)responseData["region_UUID"]);
  385. regionProfile.regionName = (string)responseData["region_name"];
  386. m_regionInfoCache.Add(regionHandle, regionProfile);
  387. }
  388. catch (WebException)
  389. {
  390. m_log.Error("[GRID]: " +
  391. "Region lookup failed for: " + regionHandle.ToString() +
  392. " - Is the GridServer down?");
  393. return null;
  394. }
  395. return regionProfile;
  396. }
  397. #endregion
  398. }
  399. }