opensim – Blame information for rev 1

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