1
0

RegionReadyModule.cs 11 KB

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