CommunicationsManager.cs 15 KB

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