InterMessageUserServerModule.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.Threading;
  33. using System.Timers;
  34. using log4net;
  35. using Nwc.XmlRpc;
  36. using OpenMetaverse;
  37. using OpenSim.Data;
  38. using OpenSim.Framework;
  39. using OpenSim.Grid.Framework;
  40. using Timer = System.Timers.Timer;
  41. namespace OpenSim.Grid.MessagingServer.Modules
  42. {
  43. public class InterMessageUserServerModule : IInterServiceUserService
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. private MessageServerConfig m_cfg;
  47. private IGridServiceCore m_messageCore;
  48. private Timer reconnectTimer = new Timer(300000); // 5 mins
  49. public InterMessageUserServerModule(MessageServerConfig config, IGridServiceCore messageCore)
  50. {
  51. m_cfg = config;
  52. m_messageCore = messageCore;
  53. reconnectTimer.Elapsed += registerWithUserServer;
  54. reconnectTimer.Start();
  55. }
  56. public void Initialise()
  57. {
  58. m_messageCore.RegisterInterface<IInterServiceUserService>(this);
  59. }
  60. public void PostInitialise()
  61. {
  62. }
  63. public void RegisterHandlers()
  64. {
  65. //have these in separate method as some servers restart the http server and reregister all the handlers.
  66. }
  67. public void registerWithUserServer(object sender, ElapsedEventArgs e)
  68. {
  69. registerWithUserServer();
  70. }
  71. public bool registerWithUserServer()
  72. {
  73. Hashtable UserParams = new Hashtable();
  74. // Login / Authentication
  75. if (m_cfg.HttpSSL)
  76. {
  77. UserParams["uri"] = "https://" + m_cfg.MessageServerIP + ":" + m_cfg.HttpPort;
  78. }
  79. else
  80. {
  81. UserParams["uri"] = "http://" + m_cfg.MessageServerIP + ":" + m_cfg.HttpPort;
  82. }
  83. UserParams["recvkey"] = m_cfg.UserRecvKey;
  84. UserParams["sendkey"] = m_cfg.UserRecvKey;
  85. // Package into an XMLRPC Request
  86. ArrayList SendParams = new ArrayList();
  87. SendParams.Add(UserParams);
  88. bool success = true;
  89. string[] servers = m_cfg.UserServerURL.Split(' ');
  90. foreach (string srv in servers)
  91. {
  92. // Send Request
  93. try
  94. {
  95. XmlRpcRequest UserReq = new XmlRpcRequest("register_messageserver", SendParams);
  96. XmlRpcResponse UserResp = UserReq.Send(srv, 16000);
  97. // Process Response
  98. Hashtable GridRespData = (Hashtable)UserResp.Value;
  99. // if we got a response, we were successful
  100. if (!GridRespData.ContainsKey("responsestring"))
  101. success = false;
  102. else
  103. m_log.InfoFormat("[SERVER] Registered with {0}", srv);
  104. }
  105. catch
  106. {
  107. m_log.ErrorFormat("Unable to connect to server {0}. Server not running?", srv);
  108. success = false;
  109. }
  110. }
  111. return success;
  112. }
  113. public bool deregisterWithUserServer()
  114. {
  115. Hashtable request = new Hashtable();
  116. return SendToUserServer(request, "deregister_messageserver");
  117. }
  118. public bool SendToUserServer(Hashtable request, string method)
  119. {
  120. // Login / Authentication
  121. if (m_cfg.HttpSSL)
  122. {
  123. request["uri"] = "https://" + m_cfg.MessageServerIP + ":" + m_cfg.HttpPort;
  124. }
  125. else
  126. {
  127. request["uri"] = "http://" + m_cfg.MessageServerIP + ":" + m_cfg.HttpPort;
  128. }
  129. request["recvkey"] = m_cfg.UserRecvKey;
  130. request["sendkey"] = m_cfg.UserRecvKey;
  131. // Package into an XMLRPC Request
  132. ArrayList SendParams = new ArrayList();
  133. SendParams.Add(request);
  134. bool success = true;
  135. string[] servers = m_cfg.UserServerURL.Split(' ');
  136. // Send Request
  137. foreach (string srv in servers)
  138. {
  139. try
  140. {
  141. XmlRpcRequest UserReq = new XmlRpcRequest(method, SendParams);
  142. XmlRpcResponse UserResp = UserReq.Send(m_cfg.UserServerURL, 16000);
  143. // Process Response
  144. Hashtable UserRespData = (Hashtable)UserResp.Value;
  145. // if we got a response, we were successful
  146. if (!UserRespData.ContainsKey("responsestring"))
  147. success = false;
  148. }
  149. catch
  150. {
  151. m_log.ErrorFormat("Unable to connect to server {0}. Server not running?", srv);
  152. success = false;
  153. }
  154. }
  155. return success;
  156. }
  157. }
  158. }