RegionReadyModule.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.Generic;
  29. using System.Reflection;
  30. using System.Net;
  31. using System.IO;
  32. using System.Text;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenMetaverse.StructuredData;
  37. using OpenSim.Services.Interfaces;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. namespace OpenSim.Region.OptionalModules.Scripting.RegionReady
  42. {
  43. public class RegionReadyModule : INonSharedRegionModule
  44. {
  45. private static readonly ILog m_log =
  46. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private IConfig m_config = null;
  48. private bool m_firstEmptyCompileQueue;
  49. private bool m_oarFileLoading;
  50. private bool m_lastOarLoadedOk;
  51. private int m_channelNotify = -1000;
  52. private bool m_enabled = false;
  53. private bool m_disable_logins = false;
  54. private string m_uri = string.Empty;
  55. Scene m_scene = null;
  56. #region INonSharedRegionModule interface
  57. public Type ReplaceableInterface
  58. {
  59. get { return null; }
  60. }
  61. public void Initialise(IConfigSource config)
  62. {
  63. //m_log.Info("[RegionReady] Initialising");
  64. m_config = config.Configs["RegionReady"];
  65. if (m_config != null)
  66. {
  67. m_enabled = m_config.GetBoolean("enabled", false);
  68. if (m_enabled)
  69. {
  70. m_channelNotify = m_config.GetInt("channel_notify", m_channelNotify);
  71. m_disable_logins = m_config.GetBoolean("login_disable", false);
  72. m_uri = m_config.GetString("alert_uri",string.Empty);
  73. }
  74. }
  75. // if (!m_enabled)
  76. // m_log.Info("[RegionReady] disabled.");
  77. }
  78. public void AddRegion(Scene scene)
  79. {
  80. if (!m_enabled)
  81. return;
  82. m_firstEmptyCompileQueue = true;
  83. m_oarFileLoading = false;
  84. m_lastOarLoadedOk = true;
  85. m_scene = scene;
  86. m_scene.EventManager.OnEmptyScriptCompileQueue += OnEmptyScriptCompileQueue;
  87. m_scene.EventManager.OnOarFileLoaded += OnOarFileLoaded;
  88. m_scene.EventManager.OnLoginsEnabled += OnLoginsEnabled;
  89. m_log.DebugFormat("[RegionReady]: Enabled for region {0}", scene.RegionInfo.RegionName);
  90. if(m_disable_logins == true)
  91. {
  92. scene.LoginLock = true;
  93. scene.LoginsDisabled = true;
  94. m_log.InfoFormat("[RegionReady]: Logins disabled for {0}",m_scene.RegionInfo.RegionName);
  95. if(m_uri != string.Empty)
  96. {
  97. RRAlert("disabled");
  98. }
  99. }
  100. }
  101. public void RemoveRegion(Scene scene)
  102. {
  103. if (!m_enabled)
  104. return;
  105. m_scene.EventManager.OnEmptyScriptCompileQueue -= OnEmptyScriptCompileQueue;
  106. m_scene.EventManager.OnOarFileLoaded -= OnOarFileLoaded;
  107. if(m_uri != string.Empty)
  108. {
  109. RRAlert("shutdown");
  110. }
  111. m_scene = null;
  112. }
  113. public void Close()
  114. {
  115. }
  116. public void RegionLoaded(Scene scene)
  117. {
  118. }
  119. public string Name
  120. {
  121. get { return "RegionReadyModule"; }
  122. }
  123. #endregion
  124. void OnEmptyScriptCompileQueue(int numScriptsFailed, string message)
  125. {
  126. if (m_firstEmptyCompileQueue || m_oarFileLoading)
  127. {
  128. OSChatMessage c = new OSChatMessage();
  129. if (m_firstEmptyCompileQueue)
  130. c.Message = "server_startup,";
  131. else
  132. c.Message = "oar_file_load,";
  133. m_firstEmptyCompileQueue = false;
  134. m_oarFileLoading = false;
  135. m_scene.Backup(false);
  136. c.From = "RegionReady";
  137. if (m_lastOarLoadedOk)
  138. c.Message += "1,";
  139. else
  140. c.Message += "0,";
  141. c.Channel = m_channelNotify;
  142. c.Message += numScriptsFailed.ToString() + "," + message;
  143. c.Type = ChatTypeEnum.Region;
  144. c.Position = new Vector3(((int)Constants.RegionSize * 0.5f), ((int)Constants.RegionSize * 0.5f), 30);
  145. c.Sender = null;
  146. c.SenderUUID = UUID.Zero;
  147. c.Scene = m_scene;
  148. m_log.InfoFormat("[RegionReady]: Region \"{0}\" is ready: \"{1}\" on channel {2}",
  149. m_scene.RegionInfo.RegionName, c.Message, m_channelNotify);
  150. m_scene.EventManager.TriggerOnChatBroadcast(this, c);
  151. m_scene.EventManager.TriggerLoginsEnabled(m_scene.RegionInfo.RegionName);
  152. m_scene.SceneGridService.InformNeighborsThatRegionisUp(m_scene.RequestModuleInterface<INeighbourService>(), m_scene.RegionInfo);
  153. }
  154. }
  155. void OnOarFileLoaded(Guid requestId, string message)
  156. {
  157. m_oarFileLoading = true;
  158. if (message==String.Empty)
  159. {
  160. m_lastOarLoadedOk = true;
  161. } else {
  162. m_log.InfoFormat("[RegionReady]: Oar file load errors: {0}", message);
  163. m_lastOarLoadedOk = false;
  164. }
  165. }
  166. void OnLoginsEnabled(string regionName)
  167. {
  168. if (m_disable_logins == true)
  169. {
  170. if (m_scene.StartDisabled == false)
  171. {
  172. m_scene.LoginsDisabled = false;
  173. m_scene.LoginLock = false;
  174. m_log.InfoFormat("[RegionReady]: Logins enabled for {0}", m_scene.RegionInfo.RegionName);
  175. if ( m_uri != string.Empty )
  176. {
  177. RRAlert("enabled");
  178. }
  179. }
  180. }
  181. }
  182. public void RRAlert(string status)
  183. {
  184. string request_method = "POST";
  185. string content_type = "application/json";
  186. OSDMap RRAlert = new OSDMap();
  187. RRAlert["alert"] = "region_ready";
  188. RRAlert["login"] = status;
  189. RRAlert["region_name"] = m_scene.RegionInfo.RegionName;
  190. RRAlert["region_id"] = m_scene.RegionInfo.RegionID;
  191. string strBuffer = "";
  192. byte[] buffer = new byte[1];
  193. try
  194. {
  195. strBuffer = OSDParser.SerializeJsonString(RRAlert);
  196. Encoding str = Util.UTF8;
  197. buffer = str.GetBytes(strBuffer);
  198. }
  199. catch (Exception e)
  200. {
  201. m_log.WarnFormat("[RegionReady]: Exception thrown on alert: {0}", e.Message);
  202. }
  203. WebRequest request = WebRequest.Create(m_uri);
  204. request.Method = request_method;
  205. request.ContentType = content_type;
  206. Stream os = null;
  207. try
  208. {
  209. request.ContentLength = buffer.Length;
  210. os = request.GetRequestStream();
  211. os.Write(buffer, 0, strBuffer.Length);
  212. }
  213. catch(Exception e)
  214. {
  215. m_log.WarnFormat("[RegionReady]: Exception thrown sending alert: {0}", e.Message);
  216. }
  217. finally
  218. {
  219. if (os != null)
  220. os.Close();
  221. }
  222. }
  223. }
  224. }