MessageServersConnector.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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;
  29. using System.Collections.Generic;
  30. using System.Text;
  31. using System.Threading;
  32. using libsecondlife;
  33. using Nwc.XmlRpc;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Communications.Cache;
  36. using OpenSim.Framework.Console;
  37. using OpenSim.Framework.Servers;
  38. namespace OpenSim.Grid.UserServer
  39. {
  40. public class MessageServersConnector
  41. {
  42. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  43. public Dictionary<string, MessageServerInfo> MessageServers;
  44. public MessageServersConnector()
  45. {
  46. MessageServers = new Dictionary<string, MessageServerInfo>();
  47. }
  48. public void RegisterMessageServer(string URI, MessageServerInfo serverData)
  49. {
  50. lock (MessageServers)
  51. {
  52. if (!MessageServers.ContainsKey(URI))
  53. MessageServers.Add(URI, serverData);
  54. }
  55. }
  56. public void DeRegisterMessageServer(string URI)
  57. {
  58. lock (MessageServers)
  59. {
  60. if (MessageServers.ContainsKey(URI))
  61. MessageServers.Remove(URI);
  62. }
  63. }
  64. public void AddResponsibleRegion(string URI, ulong regionhandle)
  65. {
  66. if (!MessageServers.ContainsKey(URI))
  67. {
  68. m_log.Warn("[MSGSERVER]: Got addResponsibleRegion Request for a MessageServer that isn't registered");
  69. }
  70. else
  71. {
  72. MessageServerInfo msginfo = MessageServers["URI"];
  73. msginfo.responsibleForRegions.Add(regionhandle);
  74. MessageServers["URI"] = msginfo;
  75. }
  76. }
  77. public void RemoveResponsibleRegion(string URI, ulong regionhandle)
  78. {
  79. if (!MessageServers.ContainsKey(URI))
  80. {
  81. m_log.Warn("[MSGSERVER]: Got RemoveResponsibleRegion Request for a MessageServer that isn't registered");
  82. }
  83. else
  84. {
  85. MessageServerInfo msginfo = MessageServers["URI"];
  86. if (msginfo.responsibleForRegions.Contains(regionhandle))
  87. {
  88. msginfo.responsibleForRegions.Remove(regionhandle);
  89. MessageServers["URI"] = msginfo;
  90. }
  91. }
  92. }
  93. public XmlRpcResponse XmlRPCRegisterMessageServer(XmlRpcRequest request)
  94. {
  95. XmlRpcResponse response = new XmlRpcResponse();
  96. Hashtable requestData = (Hashtable)request.Params[0];
  97. Hashtable responseData = new Hashtable();
  98. if (requestData.Contains("uri"))
  99. {
  100. string URI = (string)requestData["uri"];
  101. string sendkey=(string)requestData["sendkey"];
  102. string recvkey=(string)requestData["recvkey"];
  103. MessageServerInfo m = new MessageServerInfo();
  104. m.URI = URI;
  105. m.sendkey = sendkey;
  106. m.recvkey = recvkey;
  107. RegisterMessageServer(URI, m);
  108. responseData["responsestring"] = "TRUE";
  109. response.Value = responseData;
  110. }
  111. return response;
  112. }
  113. public XmlRpcResponse XmlRPCDeRegisterMessageServer(XmlRpcRequest request)
  114. {
  115. XmlRpcResponse response = new XmlRpcResponse();
  116. Hashtable requestData = (Hashtable)request.Params[0];
  117. Hashtable responseData = new Hashtable();
  118. if (requestData.Contains("uri"))
  119. {
  120. string URI = (string)requestData["uri"];
  121. DeRegisterMessageServer(URI);
  122. responseData["responsestring"] = "TRUE";
  123. response.Value = responseData;
  124. }
  125. return response;
  126. }
  127. public XmlRpcResponse XmlRPCUserMovedtoRegion(XmlRpcRequest request)
  128. {
  129. XmlRpcResponse response = new XmlRpcResponse();
  130. Hashtable requestData = (Hashtable)request.Params[0];
  131. Hashtable responseData = new Hashtable();
  132. if (requestData.Contains("fromuri"))
  133. {
  134. string sURI = (string)requestData["fromuri"];
  135. string sagentID = (string)requestData["agentid"];
  136. string ssessionID = (string)requestData["sessionid"];
  137. string scurrentRegionID = (string)requestData["regionid"];
  138. string sregionhandle = (string)requestData["regionhandle"];
  139. string scurrentpos = (string)requestData["currentpos"];
  140. //LLVector3.TryParse((string)reader["currentPos"], out retval.currentPos);
  141. // TODO: Okay now raise event so the user server can pass this data to the Usermanager
  142. responseData["responsestring"] = "TRUE";
  143. response.Value = responseData;
  144. }
  145. return response;
  146. }
  147. public void TellMessageServersAboutUser(LLUUID agentID, LLUUID sessionID, LLUUID RegionID,
  148. ulong regionhandle, float positionX, float positionY,
  149. float positionZ, string firstname, string lastname)
  150. {
  151. // Loop over registered Message Servers ( AND THERE WILL BE MORE THEN ONE :D )
  152. lock (MessageServers)
  153. {
  154. if (MessageServers.Count > 0)
  155. {
  156. m_log.Info("[MSGCONNECTOR]: Sending login notice to registered message servers");
  157. }
  158. else
  159. {
  160. m_log.Info("[MSGCONNECTOR]: No Message Servers registered, ignoring");
  161. }
  162. foreach (MessageServerInfo serv in MessageServers.Values)
  163. {
  164. NotifyMessageServerAboutUser(serv, agentID, sessionID, RegionID,
  165. regionhandle, positionX, positionY, positionZ,
  166. firstname, lastname);
  167. }
  168. }
  169. }
  170. public void TellMessageServersAboutUserLogoff(LLUUID agentID)
  171. {
  172. lock (MessageServers)
  173. {
  174. if (MessageServers.Count > 0)
  175. {
  176. m_log.Info("[MSGCONNECTOR]: Sending login notice to registered message servers");
  177. }
  178. else
  179. {
  180. m_log.Info("[MSGCONNECTOR]: No Message Servers registered, ignoring");
  181. }
  182. foreach (MessageServerInfo serv in MessageServers.Values)
  183. {
  184. NotifyMessageServerAboutUserLogoff(serv,agentID);
  185. }
  186. }
  187. }
  188. private void NotifyMessageServerAboutUserLogoff(MessageServerInfo serv, LLUUID agentID)
  189. {
  190. Hashtable reqparams = new Hashtable();
  191. reqparams["sendkey"] = serv.sendkey;
  192. reqparams["agentid"] = agentID.ToString();
  193. ArrayList SendParams = new ArrayList();
  194. SendParams.Add(reqparams);
  195. XmlRpcRequest GridReq = new XmlRpcRequest("logout_of_simulator", SendParams);
  196. try
  197. {
  198. XmlRpcResponse GridResp = GridReq.Send(serv.URI, 6000);
  199. }
  200. catch (System.Net.WebException)
  201. {
  202. m_log.Warn("[MSGCONNECTOR]: Unable to notify Message Server about log out. Other users might still think this user is online");
  203. }
  204. m_log.Info("[LOGOUT]: Notified : " + serv.URI + " about user logout");
  205. }
  206. private void NotifyMessageServerAboutUser(MessageServerInfo serv,
  207. LLUUID agentID, LLUUID sessionID, LLUUID RegionID,
  208. ulong regionhandle, float positionX, float positionY, float positionZ,
  209. string firstname, string lastname)
  210. {
  211. Hashtable reqparams = new Hashtable();
  212. reqparams["sendkey"] = serv.sendkey;
  213. reqparams["agentid"] = agentID.ToString();
  214. reqparams["sessionid"] = sessionID.ToString();
  215. reqparams["regionid"] = RegionID.ToString();
  216. reqparams["regionhandle"] = regionhandle.ToString();
  217. reqparams["positionx"] = positionX.ToString();
  218. reqparams["positiony"] = positionY.ToString();
  219. reqparams["positionz"] = positionZ.ToString();
  220. reqparams["firstname"] = firstname;
  221. reqparams["lastname"] = lastname;
  222. //reqparams["position"] = Position.ToString();
  223. ArrayList SendParams = new ArrayList();
  224. SendParams.Add(reqparams);
  225. XmlRpcRequest GridReq = new XmlRpcRequest("login_to_simulator", SendParams);
  226. try
  227. {
  228. XmlRpcResponse GridResp = GridReq.Send(serv.URI, 6000);
  229. m_log.Info("[LOGIN]: Notified : " + serv.URI + " about user login");
  230. }
  231. catch (System.Net.WebException)
  232. {
  233. m_log.Warn("[MSGCONNECTOR]: Unable to notify Message Server about login. Presence might be borked for this user");
  234. }
  235. }
  236. }
  237. }