1
0

RegionReadyModule.cs 11 KB

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