CommunicationsManager.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using OpenMetaverse;
  31. using log4net;
  32. using OpenSim.Framework.Communications.Cache;
  33. using OpenSim.Framework.Servers;
  34. namespace OpenSim.Framework.Communications
  35. {
  36. /// <summary>
  37. /// This class manages references to OpenSim non-region services (asset, inventory, user, etc.)
  38. /// </summary>
  39. public class CommunicationsManager
  40. {
  41. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. protected Dictionary<UUID, string[]> m_nameRequestCache = new Dictionary<UUID, string[]>();
  43. public IUserService UserService
  44. {
  45. get { return m_userService; }
  46. }
  47. protected IUserService m_userService;
  48. public IMessagingService MessageService
  49. {
  50. get { return m_messageService; }
  51. }
  52. protected IMessagingService m_messageService;
  53. public IGridServices GridService
  54. {
  55. get { return m_gridService; }
  56. }
  57. protected IGridServices m_gridService;
  58. public IInterRegionCommunications InterRegion
  59. {
  60. get { return m_interRegion; }
  61. }
  62. protected IInterRegionCommunications m_interRegion;
  63. public UserProfileCacheService UserProfileCacheService
  64. {
  65. get { return m_userProfileCacheService; }
  66. }
  67. protected UserProfileCacheService m_userProfileCacheService;
  68. // protected AgentAssetTransactionsManager m_transactionsManager;
  69. // public AgentAssetTransactionsManager TransactionsManager
  70. // {
  71. // get { return m_transactionsManager; }
  72. // }
  73. public IAvatarService AvatarService
  74. {
  75. get { return m_avatarService; }
  76. }
  77. protected IAvatarService m_avatarService;
  78. public AssetCache AssetCache
  79. {
  80. get { return m_assetCache; }
  81. }
  82. protected AssetCache m_assetCache;
  83. public IInterServiceInventoryServices InterServiceInventoryService
  84. {
  85. get { return m_interServiceInventoryService; }
  86. }
  87. protected IInterServiceInventoryServices m_interServiceInventoryService;
  88. public NetworkServersInfo NetworkServersInfo
  89. {
  90. get { return m_networkServersInfo; }
  91. }
  92. protected NetworkServersInfo m_networkServersInfo;
  93. /// <summary>
  94. /// Interface to user service for administrating users.
  95. /// </summary>
  96. public IUserAdminService UserAdminService
  97. {
  98. get { return m_userAdminService; }
  99. }
  100. protected IUserAdminService m_userAdminService;
  101. public BaseHttpServer HttpServer
  102. {
  103. get { return m_httpServer; }
  104. }
  105. protected BaseHttpServer m_httpServer;
  106. /// <summary>
  107. /// Constructor
  108. /// </summary>
  109. /// <param name="serversInfo"></param>
  110. /// <param name="httpServer"></param>
  111. /// <param name="assetCache"></param>
  112. /// <param name="dumpAssetsToFile"></param>
  113. public CommunicationsManager(NetworkServersInfo serversInfo, BaseHttpServer httpServer, AssetCache assetCache,
  114. bool dumpAssetsToFile, LibraryRootFolder libraryRootFolder)
  115. {
  116. m_networkServersInfo = serversInfo;
  117. m_assetCache = assetCache;
  118. m_userProfileCacheService = new UserProfileCacheService(this, libraryRootFolder);
  119. m_httpServer = httpServer;
  120. // m_transactionsManager = new AgentAssetTransactionsManager(this, dumpAssetsToFile);
  121. }
  122. #region Inventory
  123. protected string m_defaultInventoryHost = "default";
  124. protected List<IInventoryServices> m_inventoryServices = new List<IInventoryServices>();
  125. // protected IInventoryServices m_inventoryService;
  126. protected List<ISecureInventoryService> m_secureinventoryServices = new List<ISecureInventoryService>();
  127. public ISecureInventoryService SecureInventoryService
  128. {
  129. get
  130. {
  131. if (m_secureinventoryServices.Count > 0)
  132. {
  133. // return m_inventoryServices[0];
  134. ISecureInventoryService invService;
  135. if (TryGetSecureInventoryService(m_defaultInventoryHost, out invService))
  136. {
  137. return invService;
  138. }
  139. }
  140. return null;
  141. }
  142. }
  143. public IInventoryServices InventoryService
  144. {
  145. get
  146. {
  147. if (m_inventoryServices.Count > 0)
  148. {
  149. // return m_inventoryServices[0];
  150. IInventoryServices invService;
  151. if (TryGetInventoryService(m_defaultInventoryHost, out invService))
  152. {
  153. return invService;
  154. }
  155. }
  156. return null;
  157. }
  158. }
  159. public bool TryGetSecureInventoryService(string host, out ISecureInventoryService inventoryService)
  160. {
  161. if ((host == string.Empty) || (host == "default"))
  162. {
  163. host = m_defaultInventoryHost;
  164. }
  165. lock (m_secureinventoryServices)
  166. {
  167. foreach (ISecureInventoryService service in m_secureinventoryServices)
  168. {
  169. if (service.Host == host)
  170. {
  171. inventoryService = service;
  172. return true;
  173. }
  174. }
  175. }
  176. inventoryService = null;
  177. return false;
  178. }
  179. public bool TryGetInventoryService(string host, out IInventoryServices inventoryService)
  180. {
  181. if ((host == string.Empty) || (host == "default"))
  182. {
  183. host = m_defaultInventoryHost;
  184. }
  185. lock (m_inventoryServices)
  186. {
  187. foreach (IInventoryServices service in m_inventoryServices)
  188. {
  189. if (service.Host == host)
  190. {
  191. inventoryService = service;
  192. return true;
  193. }
  194. }
  195. }
  196. inventoryService = null;
  197. return false;
  198. }
  199. public virtual void AddInventoryService(string hostUrl)
  200. {
  201. }
  202. public virtual void AddSecureInventoryService(string hostUrl)
  203. {
  204. }
  205. public virtual void AddSecureInventoryService(ISecureInventoryService service)
  206. {
  207. lock (m_secureinventoryServices)
  208. {
  209. m_secureinventoryServices.Add(service);
  210. }
  211. }
  212. public virtual void AddInventoryService(IInventoryServices service)
  213. {
  214. lock (m_inventoryServices)
  215. {
  216. m_inventoryServices.Add(service);
  217. }
  218. }
  219. #endregion
  220. #region Friend Methods
  221. /// <summary>
  222. /// Adds a new friend to the database for XUser
  223. /// </summary>
  224. /// <param name="friendlistowner">The agent that who's friends list is being added to</param>
  225. /// <param name="friend">The agent that being added to the friends list of the friends list owner</param>
  226. /// <param name="perms">A uint bit vector for set perms that the friend being added has; 0 = none, 1=This friend can see when they sign on, 2 = map, 4 edit objects </param>
  227. public void AddNewUserFriend(UUID friendlistowner, UUID friend, uint perms)
  228. {
  229. m_userService.AddNewUserFriend(friendlistowner, friend, perms);
  230. }
  231. /// <summary>
  232. /// Logs off a user and does the appropriate communications
  233. /// </summary>
  234. /// <param name="userid"></param>
  235. /// <param name="regionid"></param>
  236. /// <param name="regionhandle"></param>
  237. /// <param name="position"></param>
  238. /// <param name="lookat"></param>
  239. public void LogOffUser(UUID userid, UUID regionid, ulong regionhandle, Vector3 position, Vector3 lookat)
  240. {
  241. m_userService.LogOffUser(userid, regionid, regionhandle, position, lookat);
  242. }
  243. /// <summary>
  244. /// Logs off a user and does the appropriate communications (deprecated as of 2008-08-27)
  245. /// </summary>
  246. /// <param name="userid"></param>
  247. /// <param name="regionid"></param>
  248. /// <param name="regionhandle"></param>
  249. /// <param name="posx"></param>
  250. /// <param name="posy"></param>
  251. /// <param name="posz"></param>
  252. public void LogOffUser(UUID userid, UUID regionid, ulong regionhandle, float posx, float posy, float posz)
  253. {
  254. m_userService.LogOffUser(userid, regionid, regionhandle, posx, posy, posz);
  255. }
  256. /// <summary>
  257. /// Delete friend on friendlistowner's friendlist.
  258. /// </summary>
  259. /// <param name="friendlistowner">The agent that who's friends list is being updated</param>
  260. /// <param name="friend">The Ex-friend agent</param>
  261. public void RemoveUserFriend(UUID friendlistowner, UUID friend)
  262. {
  263. m_userService.RemoveUserFriend(friendlistowner, friend);
  264. }
  265. /// <summary>
  266. /// Update permissions for friend on friendlistowner's friendlist.
  267. /// </summary>
  268. /// <param name="friendlistowner">The agent that who's friends list is being updated</param>
  269. /// <param name="friend">The agent that is getting or loosing permissions</param>
  270. /// <param name="perms">A uint bit vector for set perms that the friend being added has; 0 = none, 1=This friend can see when they sign on, 2 = map, 4 edit objects </param>
  271. public void UpdateUserFriendPerms(UUID friendlistowner, UUID friend, uint perms)
  272. {
  273. m_userService.UpdateUserFriendPerms(friendlistowner, friend, perms);
  274. }
  275. /// <summary>
  276. /// Returns a list of FriendsListItems that describe the friends and permissions in the friend relationship for UUID friendslistowner
  277. /// </summary>
  278. /// <param name="friendlistowner">The agent that we're retreiving the friends Data.</param>
  279. public List<FriendListItem> GetUserFriendList(UUID friendlistowner)
  280. {
  281. return m_userService.GetUserFriendList(friendlistowner);
  282. }
  283. public Dictionary<UUID, FriendRegionInfo> GetFriendRegionInfos(List<UUID> uuids)
  284. {
  285. return m_messageService.GetFriendRegionInfos(uuids);
  286. }
  287. public List<UUID> InformFriendsInOtherRegion(UUID agentId, ulong destRegionHandle, List<UUID> friends, bool online)
  288. {
  289. return m_interRegion.InformFriendsInOtherRegion(agentId, destRegionHandle, friends, online);
  290. }
  291. public bool TriggerTerminateFriend(ulong regionHandle, UUID agentID, UUID exFriendID)
  292. {
  293. return m_interRegion.TriggerTerminateFriend(regionHandle, agentID, exFriendID);
  294. }
  295. #endregion
  296. #region Packet Handlers
  297. public void UpdateAvatarPropertiesRequest(IClientAPI remote_client, UserProfileData UserProfile)
  298. {
  299. m_userService.UpdateUserProfile(UserProfile);
  300. return;
  301. }
  302. public void HandleUUIDNameRequest(UUID uuid, IClientAPI remote_client)
  303. {
  304. if (uuid == m_userProfileCacheService.LibraryRoot.Owner)
  305. {
  306. remote_client.SendNameReply(uuid, "Mr", "OpenSim");
  307. }
  308. else
  309. {
  310. string[] names = doUUIDNameRequest(uuid);
  311. if (names.Length == 2)
  312. {
  313. remote_client.SendNameReply(uuid, names[0], names[1]);
  314. }
  315. }
  316. }
  317. private string[] doUUIDNameRequest(UUID uuid)
  318. {
  319. string[] returnstring = new string[0];
  320. bool doLookup = false;
  321. lock (m_nameRequestCache)
  322. {
  323. if (m_nameRequestCache.ContainsKey(uuid))
  324. {
  325. returnstring = m_nameRequestCache[uuid];
  326. }
  327. else
  328. {
  329. // we don't want to lock the dictionary while we're doing the lookup
  330. doLookup = true;
  331. }
  332. }
  333. if (doLookup) {
  334. UserProfileData profileData = m_userService.GetUserProfile(uuid);
  335. if (profileData != null)
  336. {
  337. returnstring = new string[2];
  338. // UUID profileId = profileData.ID;
  339. returnstring[0] = profileData.FirstName;
  340. returnstring[1] = profileData.SurName;
  341. lock (m_nameRequestCache)
  342. {
  343. if (!m_nameRequestCache.ContainsKey(uuid))
  344. m_nameRequestCache.Add(uuid, returnstring);
  345. }
  346. }
  347. }
  348. return returnstring;
  349. }
  350. public bool UUIDNameCachedTest(UUID uuid)
  351. {
  352. lock (m_nameRequestCache)
  353. return m_nameRequestCache.ContainsKey(uuid);
  354. }
  355. public string UUIDNameRequestString(UUID uuid)
  356. {
  357. string[] names = doUUIDNameRequest(uuid);
  358. if (names.Length == 2)
  359. {
  360. string firstname = names[0];
  361. string lastname = names[1];
  362. return firstname + " " + lastname;
  363. }
  364. return "(hippos)";
  365. }
  366. public List<AvatarPickerAvatar> GenerateAgentPickerRequestResponse(UUID queryID, string query)
  367. {
  368. List<AvatarPickerAvatar> pickerlist = m_userService.GenerateAgentPickerRequestResponse(queryID, query);
  369. return pickerlist;
  370. }
  371. #endregion
  372. }
  373. }