InterMessageUserServerModule.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. lock (reconnectTimer)
  55. reconnectTimer.Start();
  56. }
  57. public void Initialise()
  58. {
  59. m_messageCore.RegisterInterface<IInterServiceUserService>(this);
  60. }
  61. public void PostInitialise()
  62. {
  63. }
  64. public void RegisterHandlers()
  65. {
  66. //have these in separate method as some servers restart the http server and reregister all the handlers.
  67. }
  68. public void registerWithUserServer(object sender, ElapsedEventArgs e)
  69. {
  70. registerWithUserServer();
  71. }
  72. public bool registerWithUserServer()
  73. {
  74. Hashtable UserParams = new Hashtable();
  75. // Login / Authentication
  76. if (m_cfg.HttpSSL)
  77. {
  78. UserParams["uri"] = "https://" + m_cfg.MessageServerIP + ":" + m_cfg.HttpPort;
  79. }
  80. else
  81. {
  82. UserParams["uri"] = "http://" + m_cfg.MessageServerIP + ":" + m_cfg.HttpPort;
  83. }
  84. UserParams["recvkey"] = m_cfg.UserRecvKey;
  85. UserParams["sendkey"] = m_cfg.UserRecvKey;
  86. // Package into an XMLRPC Request
  87. ArrayList SendParams = new ArrayList();
  88. SendParams.Add(UserParams);
  89. bool success = true;
  90. string[] servers = m_cfg.UserServerURL.Split(' ');
  91. foreach (string srv in servers)
  92. {
  93. // Send Request
  94. try
  95. {
  96. XmlRpcRequest UserReq = new XmlRpcRequest("register_messageserver", SendParams);
  97. XmlRpcResponse UserResp = UserReq.Send(srv, 16000);
  98. // Process Response
  99. Hashtable GridRespData = (Hashtable)UserResp.Value;
  100. // if we got a response, we were successful
  101. if (!GridRespData.ContainsKey("responsestring"))
  102. success = false;
  103. else
  104. m_log.InfoFormat("[SERVER] Registered with {0}", srv);
  105. }
  106. catch
  107. {
  108. m_log.ErrorFormat("Unable to connect to server {0}. Server not running?", srv);
  109. success = false;
  110. }
  111. }
  112. return success;
  113. }
  114. public bool deregisterWithUserServer()
  115. {
  116. Hashtable request = new Hashtable();
  117. return SendToUserServer(request, "deregister_messageserver");
  118. }
  119. public bool SendToUserServer(Hashtable request, string method)
  120. {
  121. // Login / Authentication
  122. if (m_cfg.HttpSSL)
  123. {
  124. request["uri"] = "https://" + m_cfg.MessageServerIP + ":" + m_cfg.HttpPort;
  125. }
  126. else
  127. {
  128. request["uri"] = "http://" + m_cfg.MessageServerIP + ":" + m_cfg.HttpPort;
  129. }
  130. request["recvkey"] = m_cfg.UserRecvKey;
  131. request["sendkey"] = m_cfg.UserRecvKey;
  132. // Package into an XMLRPC Request
  133. ArrayList SendParams = new ArrayList();
  134. SendParams.Add(request);
  135. bool success = true;
  136. string[] servers = m_cfg.UserServerURL.Split(' ');
  137. // Send Request
  138. foreach (string srv in servers)
  139. {
  140. try
  141. {
  142. XmlRpcRequest UserReq = new XmlRpcRequest(method, SendParams);
  143. XmlRpcResponse UserResp = UserReq.Send(m_cfg.UserServerURL, 16000);
  144. // Process Response
  145. Hashtable UserRespData = (Hashtable)UserResp.Value;
  146. // if we got a response, we were successful
  147. if (!UserRespData.ContainsKey("responsestring"))
  148. success = false;
  149. }
  150. catch
  151. {
  152. m_log.ErrorFormat("Unable to connect to server {0}. Server not running?", srv);
  153. success = false;
  154. }
  155. }
  156. return success;
  157. }
  158. }
  159. }