1
0

RegionReadyModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.Runtime;
  31. using System.Net;
  32. using System.IO;
  33. using System.Text;
  34. using log4net;
  35. using Mono.Addins;
  36. using Nini.Config;
  37. using OpenMetaverse;
  38. using OpenMetaverse.StructuredData;
  39. using OpenSim.Framework;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.Framework.Scenes;
  42. using OpenSim.Services.Interfaces;
  43. namespace OpenSim.Region.OptionalModules.Scripting.RegionReady
  44. {
  45. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "RegionReadyModule")]
  46. public class RegionReadyModule : IRegionReadyModule, INonSharedRegionModule
  47. {
  48. private static readonly ILog m_log =
  49. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private IConfig m_config = null;
  51. private bool m_firstEmptyCompileQueue;
  52. private bool m_oarFileLoading;
  53. private bool m_lastOarLoadedOk;
  54. private int m_channelNotify = -1000;
  55. private bool m_enabled = false;
  56. private bool m_disable_logins;
  57. private string m_uri = string.Empty;
  58. Scene m_scene;
  59. #region INonSharedRegionModule interface
  60. public Type ReplaceableInterface
  61. {
  62. get { return null; }
  63. }
  64. public void Initialise(IConfigSource config)
  65. {
  66. m_config = config.Configs["RegionReady"];
  67. if (m_config != null)
  68. {
  69. m_enabled = m_config.GetBoolean("enabled", false);
  70. if (m_enabled)
  71. {
  72. m_channelNotify = m_config.GetInt("channel_notify", m_channelNotify);
  73. m_disable_logins = m_config.GetBoolean("login_disable", false);
  74. m_uri = m_config.GetString("alert_uri",string.Empty);
  75. }
  76. }
  77. }
  78. public void AddRegion(Scene scene)
  79. {
  80. if (!m_enabled)
  81. return;
  82. m_scene = scene;
  83. m_scene.RegisterModuleInterface<IRegionReadyModule>(this);
  84. m_firstEmptyCompileQueue = true;
  85. m_oarFileLoading = false;
  86. m_lastOarLoadedOk = true;
  87. m_scene.EventManager.OnOarFileLoaded += OnOarFileLoaded;
  88. m_log.DebugFormat("[RegionReady]: Enabled for region {0}", scene.RegionInfo.RegionName);
  89. if (m_disable_logins)
  90. {
  91. m_scene.LoginLock = true;
  92. m_scene.EventManager.OnEmptyScriptCompileQueue += OnEmptyScriptCompileQueue;
  93. // This should always show up to the user but should not trigger warn/errors as these messages are
  94. // expected and are not simulator problems. Ideally, there would be a status level in log4net but
  95. // failing that, we will print out to console instead.
  96. MainConsole.Instance.OutputFormat("Region {0} - LOGINS DISABLED DURING INITIALIZATION.", m_scene.Name);
  97. if (m_uri != string.Empty)
  98. {
  99. RRAlert("disabled");
  100. }
  101. }
  102. }
  103. public void RemoveRegion(Scene scene)
  104. {
  105. if (!m_enabled)
  106. return;
  107. m_scene.EventManager.OnOarFileLoaded -= OnOarFileLoaded;
  108. if (m_disable_logins)
  109. m_scene.EventManager.OnEmptyScriptCompileQueue -= OnEmptyScriptCompileQueue;
  110. if (m_uri != string.Empty)
  111. RRAlert("shutdown");
  112. m_scene = null;
  113. }
  114. public void Close()
  115. {
  116. }
  117. public void RegionLoaded(Scene scene)
  118. {
  119. }
  120. public string Name
  121. {
  122. get { return "RegionReadyModule"; }
  123. }
  124. #endregion
  125. void OnEmptyScriptCompileQueue(int numScriptsFailed, string message)
  126. {
  127. m_log.DebugFormat("[RegionReady]: Script compile queue empty!");
  128. if (m_firstEmptyCompileQueue || m_oarFileLoading)
  129. {
  130. OSChatMessage c = new OSChatMessage();
  131. if (m_firstEmptyCompileQueue)
  132. c.Message = "server_startup,";
  133. else
  134. c.Message = "oar_file_load,";
  135. m_firstEmptyCompileQueue = false;
  136. m_oarFileLoading = false;
  137. m_scene.Backup(false);
  138. c.From = "RegionReady";
  139. if (m_lastOarLoadedOk)
  140. c.Message += "1,";
  141. else
  142. c.Message += "0,";
  143. c.Channel = m_channelNotify;
  144. c.Message += numScriptsFailed.ToString() + "," + message;
  145. c.Type = ChatTypeEnum.Region;
  146. if (m_scene != null)
  147. c.Position = new Vector3((m_scene.RegionInfo.RegionSizeX * 0.5f), (m_scene.RegionInfo.RegionSizeY * 0.5f), 30);
  148. else
  149. c.Position = new Vector3(((int)Constants.RegionSize * 0.5f), ((int)Constants.RegionSize * 0.5f), 30);
  150. c.Sender = null;
  151. c.SenderUUID = UUID.Zero;
  152. c.Scene = m_scene;
  153. m_log.DebugFormat("[RegionReady]: Region \"{0}\" is ready: \"{1}\" on channel {2}",
  154. m_scene.RegionInfo.RegionName, c.Message, m_channelNotify);
  155. m_scene.EventManager.TriggerOnChatBroadcast(this, c);
  156. TriggerRegionReady(m_scene);
  157. }
  158. }
  159. void OnOarFileLoaded(Guid requestId, List<UUID> loadedScenes, string message)
  160. {
  161. m_oarFileLoading = true;
  162. if (message==String.Empty)
  163. {
  164. m_lastOarLoadedOk = true;
  165. }
  166. else
  167. {
  168. m_log.WarnFormat("[RegionReady]: Oar file load errors: {0}", message);
  169. m_lastOarLoadedOk = false;
  170. }
  171. }
  172. /// <summary>
  173. /// This will be triggered by Scene directly if it contains no scripts on startup. Otherwise it is triggered
  174. /// when the script compile queue is empty after initial region startup.
  175. /// </summary>
  176. /// <param name='scene'></param>
  177. public void TriggerRegionReady(IScene scene)
  178. {
  179. m_scene.EventManager.OnEmptyScriptCompileQueue -= OnEmptyScriptCompileQueue;
  180. m_scene.LoginLock = false;
  181. GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
  182. GC.Collect();
  183. GC.WaitForPendingFinalizers();
  184. GC.Collect();
  185. GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.Default;
  186. if (!m_scene.StartDisabled)
  187. {
  188. m_scene.LoginsEnabled = true;
  189. // m_log.InfoFormat("[RegionReady]: Logins enabled for {0}, Oar {1}",
  190. // m_scene.RegionInfo.RegionName, m_oarFileLoading.ToString());
  191. // Putting this out to console to make it eye-catching for people who are running OpenSimulator
  192. // without info log messages enabled. Making this a warning is arguably misleading since it isn't a
  193. // warning, and monitor scripts looking for warn/error/fatal messages will received false positives.
  194. // Arguably, log4net needs a status log level (like Apache).
  195. MainConsole.Instance.OutputFormat("INITIALIZATION COMPLETE FOR {0} - LOGINS ENABLED", m_scene.Name);
  196. }
  197. m_scene.SceneGridService.InformNeighborsThatRegionisUp(
  198. m_scene.RequestModuleInterface<INeighbourService>(), m_scene.RegionInfo);
  199. if (m_uri != string.Empty)
  200. {
  201. RRAlert("enabled");
  202. }
  203. m_scene.Ready = true;
  204. }
  205. public void OarLoadingAlert(string msg)
  206. {
  207. // Let's bypass this for now until some better feedback can be established
  208. //
  209. // if (msg == "load")
  210. // {
  211. // m_scene.EventManager.OnEmptyScriptCompileQueue += OnEmptyScriptCompileQueue;
  212. // m_scene.EventManager.OnOarFileLoaded += OnOarFileLoaded;
  213. // m_scene.EventManager.OnLoginsEnabled += OnLoginsEnabled;
  214. // m_scene.EventManager.OnRezScript += OnRezScript;
  215. // m_oarFileLoading = true;
  216. // m_firstEmptyCompileQueue = true;
  217. //
  218. // m_scene.LoginsDisabled = true;
  219. // m_scene.LoginLock = true;
  220. // if ( m_uri != string.Empty )
  221. // {
  222. // RRAlert("loading oar");
  223. // RRAlert("disabled");
  224. // }
  225. // }
  226. }
  227. public void RRAlert(string status)
  228. {
  229. string request_method = "POST";
  230. string content_type = "application/json";
  231. OSDMap RRAlert = new OSDMap();
  232. RRAlert["alert"] = "region_ready";
  233. RRAlert["login"] = status;
  234. RRAlert["region_name"] = m_scene.RegionInfo.RegionName;
  235. RRAlert["region_id"] = m_scene.RegionInfo.RegionID;
  236. string strBuffer = "";
  237. byte[] buffer = new byte[1];
  238. try
  239. {
  240. strBuffer = OSDParser.SerializeJsonString(RRAlert);
  241. Encoding str = Util.UTF8;
  242. buffer = str.GetBytes(strBuffer);
  243. }
  244. catch (Exception e)
  245. {
  246. m_log.WarnFormat("[RegionReady]: Exception thrown on alert: {0}", e.Message);
  247. }
  248. WebRequest request = WebRequest.Create(m_uri);
  249. request.Method = request_method;
  250. request.ContentType = content_type;
  251. Stream os = null;
  252. try
  253. {
  254. request.ContentLength = buffer.Length;
  255. os = request.GetRequestStream();
  256. os.Write(buffer, 0, strBuffer.Length);
  257. }
  258. catch(Exception e)
  259. {
  260. m_log.WarnFormat("[RegionReady]: Exception thrown sending alert: {0}", e.Message);
  261. }
  262. finally
  263. {
  264. if (os != null)
  265. os.Dispose();
  266. }
  267. }
  268. }
  269. }