1
0

MessageServersConnector.cs 11 KB

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