MessageServersConnector.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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.Collections;
  28. using System.Collections.Generic;
  29. using System.Net;
  30. using System.Reflection;
  31. using OpenMetaverse;
  32. using log4net;
  33. using Nwc.XmlRpc;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Servers;
  36. using System.Threading;
  37. namespace OpenSim.Grid.UserServer
  38. {
  39. public enum NotificationRequest : int
  40. {
  41. Login = 0,
  42. Logout = 1,
  43. Shutdown = 2
  44. }
  45. public struct PresenceNotification
  46. {
  47. public NotificationRequest request;
  48. public UUID agentID;
  49. public UUID sessionID;
  50. public UUID RegionID;
  51. public ulong regionhandle;
  52. public float positionX;
  53. public float positionY;
  54. public float positionZ;
  55. public string firstname;
  56. public string lastname;
  57. }
  58. public delegate void AgentLocationDelegate(UUID agentID, UUID regionID, ulong regionHandle);
  59. public delegate void AgentLeavingDelegate(UUID agentID, UUID regionID, ulong regionHandle);
  60. public delegate void RegionStartupDelegate(UUID regionID);
  61. public delegate void RegionShutdownDelegate(UUID regionID);
  62. public class MessageServersConnector
  63. {
  64. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  65. public Dictionary<string, MessageServerInfo> MessageServers;
  66. private BlockingQueue<PresenceNotification> m_NotifyQueue =
  67. new BlockingQueue<PresenceNotification>();
  68. Thread m_NotifyThread;
  69. public event AgentLocationDelegate OnAgentLocation;
  70. public event AgentLeavingDelegate OnAgentLeaving;
  71. public event RegionStartupDelegate OnRegionStartup;
  72. public event RegionShutdownDelegate OnRegionShutdown;
  73. public MessageServersConnector()
  74. {
  75. MessageServers = new Dictionary<string, MessageServerInfo>();
  76. m_NotifyThread = new Thread(new ThreadStart(NotifyQueueRunner));
  77. m_NotifyThread.Start();
  78. }
  79. public void RegisterMessageServer(string URI, MessageServerInfo serverData)
  80. {
  81. lock (MessageServers)
  82. {
  83. if (!MessageServers.ContainsKey(URI))
  84. MessageServers.Add(URI, serverData);
  85. }
  86. }
  87. public void DeRegisterMessageServer(string URI)
  88. {
  89. lock (MessageServers)
  90. {
  91. if (MessageServers.ContainsKey(URI))
  92. MessageServers.Remove(URI);
  93. }
  94. }
  95. public void AddResponsibleRegion(string URI, ulong regionhandle)
  96. {
  97. if (!MessageServers.ContainsKey(URI))
  98. {
  99. m_log.Warn("[MSGSERVER]: Got addResponsibleRegion Request for a MessageServer that isn't registered");
  100. }
  101. else
  102. {
  103. MessageServerInfo msginfo = MessageServers["URI"];
  104. msginfo.responsibleForRegions.Add(regionhandle);
  105. MessageServers["URI"] = msginfo;
  106. }
  107. }
  108. public void RemoveResponsibleRegion(string URI, ulong regionhandle)
  109. {
  110. if (!MessageServers.ContainsKey(URI))
  111. {
  112. m_log.Warn("[MSGSERVER]: Got RemoveResponsibleRegion Request for a MessageServer that isn't registered");
  113. }
  114. else
  115. {
  116. MessageServerInfo msginfo = MessageServers["URI"];
  117. if (msginfo.responsibleForRegions.Contains(regionhandle))
  118. {
  119. msginfo.responsibleForRegions.Remove(regionhandle);
  120. MessageServers["URI"] = msginfo;
  121. }
  122. }
  123. }
  124. public XmlRpcResponse XmlRPCRegisterMessageServer(XmlRpcRequest request)
  125. {
  126. XmlRpcResponse response = new XmlRpcResponse();
  127. Hashtable requestData = (Hashtable)request.Params[0];
  128. Hashtable responseData = new Hashtable();
  129. if (requestData.Contains("uri"))
  130. {
  131. string URI = (string)requestData["uri"];
  132. string sendkey=(string)requestData["sendkey"];
  133. string recvkey=(string)requestData["recvkey"];
  134. MessageServerInfo m = new MessageServerInfo();
  135. m.URI = URI;
  136. m.sendkey = sendkey;
  137. m.recvkey = recvkey;
  138. RegisterMessageServer(URI, m);
  139. responseData["responsestring"] = "TRUE";
  140. response.Value = responseData;
  141. }
  142. return response;
  143. }
  144. public XmlRpcResponse XmlRPCDeRegisterMessageServer(XmlRpcRequest request)
  145. {
  146. XmlRpcResponse response = new XmlRpcResponse();
  147. Hashtable requestData = (Hashtable)request.Params[0];
  148. Hashtable responseData = new Hashtable();
  149. if (requestData.Contains("uri"))
  150. {
  151. string URI = (string)requestData["uri"];
  152. DeRegisterMessageServer(URI);
  153. responseData["responsestring"] = "TRUE";
  154. response.Value = responseData;
  155. }
  156. return response;
  157. }
  158. public XmlRpcResponse XmlRPCUserMovedtoRegion(XmlRpcRequest request)
  159. {
  160. XmlRpcResponse response = new XmlRpcResponse();
  161. Hashtable requestData = (Hashtable)request.Params[0];
  162. Hashtable responseData = new Hashtable();
  163. if (requestData.Contains("fromuri"))
  164. {
  165. // string sURI = (string)requestData["fromuri"];
  166. // string sagentID = (string)requestData["agentid"];
  167. // string ssessionID = (string)requestData["sessionid"];
  168. // string scurrentRegionID = (string)requestData["regionid"];
  169. // string sregionhandle = (string)requestData["regionhandle"];
  170. // string scurrentpos = (string)requestData["currentpos"];
  171. //Vector3.TryParse((string)reader["currentPos"], out retval.currentPos);
  172. // TODO: Okay now raise event so the user server can pass this data to the Usermanager
  173. responseData["responsestring"] = "TRUE";
  174. response.Value = responseData;
  175. }
  176. return response;
  177. }
  178. public void TellMessageServersAboutUser(UUID agentID, UUID sessionID, UUID RegionID,
  179. ulong regionhandle, float positionX, float positionY,
  180. float positionZ, string firstname, string lastname)
  181. {
  182. PresenceNotification notification = new PresenceNotification();
  183. notification.request = NotificationRequest.Login;
  184. notification.agentID = agentID;
  185. notification.sessionID = sessionID;
  186. notification.RegionID = RegionID;
  187. notification.regionhandle = regionhandle;
  188. notification.positionX = positionX;
  189. notification.positionY = positionY;
  190. notification.positionZ = positionZ;
  191. notification.firstname = firstname;
  192. notification.lastname = lastname;
  193. m_NotifyQueue.Enqueue(notification);
  194. }
  195. private void TellMessageServersAboutUserInternal(UUID agentID, UUID sessionID, UUID RegionID,
  196. ulong regionhandle, float positionX, float positionY,
  197. float positionZ, string firstname, string lastname)
  198. {
  199. // Loop over registered Message Servers (AND THERE WILL BE MORE THEN ONE :D)
  200. lock (MessageServers)
  201. {
  202. if (MessageServers.Count > 0)
  203. {
  204. m_log.Info("[MSGCONNECTOR]: Sending login notice to registered message servers");
  205. }
  206. // else
  207. // {
  208. // m_log.Debug("[MSGCONNECTOR]: No Message Servers registered, ignoring");
  209. // }
  210. foreach (MessageServerInfo serv in MessageServers.Values)
  211. {
  212. NotifyMessageServerAboutUser(serv, agentID, sessionID, RegionID,
  213. regionhandle, positionX, positionY, positionZ,
  214. firstname, lastname);
  215. }
  216. }
  217. }
  218. private void TellMessageServersAboutUserLogoffInternal(UUID agentID)
  219. {
  220. lock (MessageServers)
  221. {
  222. if (MessageServers.Count > 0)
  223. {
  224. m_log.Info("[MSGCONNECTOR]: Sending logoff notice to registered message servers");
  225. }
  226. else
  227. {
  228. // m_log.Debug("[MSGCONNECTOR]: No Message Servers registered, ignoring");
  229. }
  230. foreach (MessageServerInfo serv in MessageServers.Values)
  231. {
  232. NotifyMessageServerAboutUserLogoff(serv,agentID);
  233. }
  234. }
  235. }
  236. private void TellMessageServersAboutRegionShutdownInternal(UUID regionID)
  237. {
  238. lock (MessageServers)
  239. {
  240. if (MessageServers.Count > 0)
  241. {
  242. m_log.Info("[MSGCONNECTOR]: Sending region down notice to registered message servers");
  243. }
  244. else
  245. {
  246. // m_log.Debug("[MSGCONNECTOR]: No Message Servers registered, ignoring");
  247. }
  248. foreach (MessageServerInfo serv in MessageServers.Values)
  249. {
  250. NotifyMessageServerAboutRegionShutdown(serv,regionID);
  251. }
  252. }
  253. }
  254. public void TellMessageServersAboutUserLogoff(UUID agentID)
  255. {
  256. PresenceNotification notification = new PresenceNotification();
  257. notification.request = NotificationRequest.Logout;
  258. notification.agentID = agentID;
  259. m_NotifyQueue.Enqueue(notification);
  260. }
  261. public void TellMessageServersAboutRegionShutdown(UUID regionID)
  262. {
  263. PresenceNotification notification = new PresenceNotification();
  264. notification.request = NotificationRequest.Shutdown;
  265. notification.RegionID = regionID;
  266. m_NotifyQueue.Enqueue(notification);
  267. }
  268. private void NotifyMessageServerAboutUserLogoff(MessageServerInfo serv, UUID agentID)
  269. {
  270. Hashtable reqparams = new Hashtable();
  271. reqparams["sendkey"] = serv.sendkey;
  272. reqparams["agentid"] = agentID.ToString();
  273. ArrayList SendParams = new ArrayList();
  274. SendParams.Add(reqparams);
  275. XmlRpcRequest GridReq = new XmlRpcRequest("logout_of_simulator", SendParams);
  276. try
  277. {
  278. GridReq.Send(serv.URI, 6000);
  279. }
  280. catch (WebException)
  281. {
  282. m_log.Warn("[MSGCONNECTOR]: Unable to notify Message Server about log out. Other users might still think this user is online");
  283. }
  284. m_log.Info("[LOGOUT]: Notified : " + serv.URI + " about user logout");
  285. }
  286. private void NotifyMessageServerAboutRegionShutdown(MessageServerInfo serv, UUID regionID)
  287. {
  288. Hashtable reqparams = new Hashtable();
  289. reqparams["sendkey"] = serv.sendkey;
  290. reqparams["regionid"] = regionID.ToString();
  291. ArrayList SendParams = new ArrayList();
  292. SendParams.Add(reqparams);
  293. XmlRpcRequest GridReq = new XmlRpcRequest("process_region_shutdown", SendParams);
  294. try
  295. {
  296. GridReq.Send(serv.URI, 6000);
  297. }
  298. catch (WebException)
  299. {
  300. m_log.Warn("[MSGCONNECTOR]: Unable to notify Message Server about region shutdown.");
  301. }
  302. m_log.Info("[REGION UPDOWN]: Notified : " + serv.URI + " about region state change");
  303. }
  304. private void NotifyMessageServerAboutUser(MessageServerInfo serv,
  305. UUID agentID, UUID sessionID, UUID RegionID,
  306. ulong regionhandle, float positionX, float positionY, float positionZ,
  307. string firstname, string lastname)
  308. {
  309. Hashtable reqparams = new Hashtable();
  310. reqparams["sendkey"] = serv.sendkey;
  311. reqparams["agentid"] = agentID.ToString();
  312. reqparams["sessionid"] = sessionID.ToString();
  313. reqparams["regionid"] = RegionID.ToString();
  314. reqparams["regionhandle"] = regionhandle.ToString();
  315. reqparams["positionx"] = positionX.ToString();
  316. reqparams["positiony"] = positionY.ToString();
  317. reqparams["positionz"] = positionZ.ToString();
  318. reqparams["firstname"] = firstname;
  319. reqparams["lastname"] = lastname;
  320. //reqparams["position"] = Position.ToString();
  321. ArrayList SendParams = new ArrayList();
  322. SendParams.Add(reqparams);
  323. XmlRpcRequest GridReq = new XmlRpcRequest("login_to_simulator", SendParams);
  324. try
  325. {
  326. GridReq.Send(serv.URI, 6000);
  327. m_log.Info("[LOGIN]: Notified : " + serv.URI + " about user login");
  328. }
  329. catch (WebException)
  330. {
  331. m_log.Warn("[MSGCONNECTOR]: Unable to notify Message Server about login. Presence might be borked for this user");
  332. }
  333. }
  334. private void NotifyQueueRunner()
  335. {
  336. while (true)
  337. {
  338. PresenceNotification presence = m_NotifyQueue.Dequeue();
  339. if (presence.request == NotificationRequest.Shutdown)
  340. {
  341. TellMessageServersAboutRegionShutdownInternal(presence.RegionID);
  342. }
  343. if (presence.request == NotificationRequest.Login)
  344. {
  345. TellMessageServersAboutUserInternal(presence.agentID,
  346. presence.sessionID, presence.RegionID,
  347. presence.regionhandle, presence.positionX,
  348. presence.positionY, presence.positionZ,
  349. presence.firstname, presence.lastname);
  350. }
  351. if (presence.request == NotificationRequest.Logout)
  352. {
  353. TellMessageServersAboutUserLogoffInternal(presence.agentID);
  354. }
  355. }
  356. }
  357. public XmlRpcResponse RegionStartup(XmlRpcRequest request)
  358. {
  359. Hashtable requestData = (Hashtable)request.Params[0];
  360. Hashtable result = new Hashtable();
  361. UUID regionID;
  362. if (UUID.TryParse((string)requestData["RegionUUID"], out regionID))
  363. {
  364. if (OnRegionStartup != null)
  365. OnRegionStartup(regionID);
  366. result["responsestring"] = "TRUE";
  367. }
  368. XmlRpcResponse response = new XmlRpcResponse();
  369. response.Value = result;
  370. return response;
  371. }
  372. public XmlRpcResponse RegionShutdown(XmlRpcRequest request)
  373. {
  374. Hashtable requestData = (Hashtable)request.Params[0];
  375. Hashtable result = new Hashtable();
  376. UUID regionID;
  377. if (UUID.TryParse((string)requestData["RegionUUID"], out regionID))
  378. {
  379. if (OnRegionShutdown != null)
  380. OnRegionShutdown(regionID);
  381. result["responsestring"] = "TRUE";
  382. }
  383. XmlRpcResponse response = new XmlRpcResponse();
  384. response.Value = result;
  385. return response;
  386. }
  387. public XmlRpcResponse AgentLocation(XmlRpcRequest request)
  388. {
  389. Hashtable requestData = (Hashtable)request.Params[0];
  390. Hashtable result = new Hashtable();
  391. UUID agentID;
  392. UUID regionID;
  393. ulong regionHandle;
  394. if (UUID.TryParse((string)requestData["AgentID"], out agentID) && UUID.TryParse((string)requestData["RegionUUID"], out regionID) && ulong.TryParse((string)requestData["RegionHandle"], out regionHandle))
  395. {
  396. if (OnAgentLocation != null)
  397. OnAgentLocation(agentID, regionID, regionHandle);
  398. result["responsestring"] = "TRUE";
  399. }
  400. XmlRpcResponse response = new XmlRpcResponse();
  401. response.Value = result;
  402. return response;
  403. }
  404. public XmlRpcResponse AgentLeaving(XmlRpcRequest request)
  405. {
  406. Hashtable requestData = (Hashtable)request.Params[0];
  407. Hashtable result = new Hashtable();
  408. UUID agentID;
  409. UUID regionID;
  410. ulong regionHandle;
  411. if (UUID.TryParse((string)requestData["AgentID"], out agentID) && UUID.TryParse((string)requestData["RegionUUID"], out regionID) && ulong.TryParse((string)requestData["RegionHandle"], out regionHandle))
  412. {
  413. if (OnAgentLeaving != null)
  414. OnAgentLeaving(agentID, regionID, regionHandle);
  415. result["responsestring"] = "TRUE";
  416. }
  417. XmlRpcResponse response = new XmlRpcResponse();
  418. response.Value = result;
  419. return response;
  420. }
  421. }
  422. }