MessageServersConnector.cs 20 KB

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