CommunicationsManager.cs 15 KB

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