aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/Servers/ServerBase.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/Servers/ServerBase.cs')
-rw-r--r--OpenSim/Framework/Servers/ServerBase.cs510
1 files changed, 509 insertions, 1 deletions
diff --git a/OpenSim/Framework/Servers/ServerBase.cs b/OpenSim/Framework/Servers/ServerBase.cs
index d19234b..c182a3a 100644
--- a/OpenSim/Framework/Servers/ServerBase.cs
+++ b/OpenSim/Framework/Servers/ServerBase.cs
@@ -26,20 +26,392 @@
26 */ 26 */
27 27
28using System; 28using System;
29using System.Collections.Generic;
30using System.IO;
31using System.Reflection;
29using System.Text; 32using System.Text;
33using System.Text.RegularExpressions;
34using log4net;
35using log4net.Appender;
36using log4net.Core;
37using log4net.Repository;
38using Nini.Config;
39using OpenSim.Framework.Console;
30 40
31namespace OpenSim.Framework.Servers 41namespace OpenSim.Framework.Servers
32{ 42{
33 public class ServerBase 43 public class ServerBase
34 { 44 {
45 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
46
47 public IConfigSource Config { get; protected set; }
48
35 /// <summary> 49 /// <summary>
36 /// Time at which this server was started 50 /// Console to be used for any command line output. Can be null, in which case there should be no output.
37 /// </summary> 51 /// </summary>
52 protected ICommandConsole m_console;
53
54 protected OpenSimAppender m_consoleAppender;
55 protected FileAppender m_logFileAppender;
56
38 protected DateTime m_startuptime; 57 protected DateTime m_startuptime;
58 protected string m_startupDirectory = Environment.CurrentDirectory;
59
60 protected string m_pidFile = String.Empty;
61
62 /// <summary>
63 /// Server version information. Usually VersionInfo + information about git commit, operating system, etc.
64 /// </summary>
65 protected string m_version;
39 66
40 public ServerBase() 67 public ServerBase()
41 { 68 {
42 m_startuptime = DateTime.Now; 69 m_startuptime = DateTime.Now;
70 m_version = VersionInfo.Version;
71 EnhanceVersionInformation();
72 }
73
74 protected void CreatePIDFile(string path)
75 {
76 try
77 {
78 string pidstring = System.Diagnostics.Process.GetCurrentProcess().Id.ToString();
79
80 using (FileStream fs = File.Create(path))
81 {
82 Byte[] buf = Encoding.ASCII.GetBytes(pidstring);
83 fs.Write(buf, 0, buf.Length);
84 }
85
86 m_pidFile = path;
87
88 m_log.InfoFormat("[SERVER BASE]: Created pid file {0}", m_pidFile);
89 }
90 catch (Exception e)
91 {
92 m_log.Warn(string.Format("[SERVER BASE]: Could not create PID file at {0} ", path), e);
93 }
94 }
95
96 protected void RemovePIDFile()
97 {
98 if (m_pidFile != String.Empty)
99 {
100 try
101 {
102 File.Delete(m_pidFile);
103 }
104 catch (Exception e)
105 {
106 m_log.Error(string.Format("[SERVER BASE]: Error whilst removing {0} ", m_pidFile), e);
107 }
108
109 m_pidFile = String.Empty;
110 }
111 }
112
113 public void RegisterCommonAppenders(IConfig startupConfig)
114 {
115 ILoggerRepository repository = LogManager.GetRepository();
116 IAppender[] appenders = repository.GetAppenders();
117
118 foreach (IAppender appender in appenders)
119 {
120 if (appender.Name == "Console")
121 {
122 m_consoleAppender = (OpenSimAppender)appender;
123 }
124 else if (appender.Name == "LogFileAppender")
125 {
126 m_logFileAppender = (FileAppender)appender;
127 }
128 }
129
130 if (null == m_consoleAppender)
131 {
132 Notice("No appender named Console found (see the log4net config file for this executable)!");
133 }
134 else
135 {
136 // FIXME: This should be done through an interface rather than casting.
137 m_consoleAppender.Console = (ConsoleBase)m_console;
138
139 // If there is no threshold set then the threshold is effectively everything.
140 if (null == m_consoleAppender.Threshold)
141 m_consoleAppender.Threshold = Level.All;
142
143 Notice(String.Format("Console log level is {0}", m_consoleAppender.Threshold));
144 }
145
146 if (m_logFileAppender != null && startupConfig != null)
147 {
148 string cfgFileName = startupConfig.GetString("LogFile", null);
149 if (cfgFileName != null)
150 {
151 m_logFileAppender.File = cfgFileName;
152 m_logFileAppender.ActivateOptions();
153 }
154
155 m_log.InfoFormat("[SERVER BASE]: Logging started to file {0}", m_logFileAppender.File);
156 }
157 }
158
159 /// <summary>
160 /// Register common commands once m_console has been set if it is going to be set
161 /// </summary>
162 public void RegisterCommonCommands()
163 {
164 if (m_console == null)
165 return;
166
167 m_console.Commands.AddCommand(
168 "General", false, "show info", "show info", "Show general information about the server", HandleShow);
169
170 m_console.Commands.AddCommand(
171 "General", false, "show uptime", "show uptime", "Show server uptime", HandleShow);
172
173 m_console.Commands.AddCommand(
174 "General", false, "get log level", "get log level", "Get the current console logging level",
175 (mod, cmd) => ShowLogLevel());
176
177 m_console.Commands.AddCommand(
178 "General", false, "set log level", "set log level <level>",
179 "Set the console logging level for this session.", HandleSetLogLevel);
180
181 m_console.Commands.AddCommand(
182 "General", false, "config set",
183 "config set <section> <key> <value>",
184 "Set a config option. In most cases this is not useful since changed parameters are not dynamically reloaded. Neither do changed parameters persist - you will have to change a config file manually and restart.", HandleConfig);
185
186 m_console.Commands.AddCommand(
187 "General", false, "config get",
188 "config get [<section>] [<key>]",
189 "Synonym for config show",
190 HandleConfig);
191
192 m_console.Commands.AddCommand(
193 "General", false, "config show",
194 "config show [<section>] [<key>]",
195 "Show config information",
196 "If neither section nor field are specified, then the whole current configuration is printed." + Environment.NewLine
197 + "If a section is given but not a field, then all fields in that section are printed.",
198 HandleConfig);
199
200 m_console.Commands.AddCommand(
201 "General", false, "config save",
202 "config save <path>",
203 "Save current configuration to a file at the given path", HandleConfig);
204
205 m_console.Commands.AddCommand(
206 "General", false, "command-script",
207 "command-script <script>",
208 "Run a command script from file", HandleScript);
209 }
210
211 public virtual void HandleShow(string module, string[] cmd)
212 {
213 List<string> args = new List<string>(cmd);
214
215 args.RemoveAt(0);
216
217 string[] showParams = args.ToArray();
218
219 switch (showParams[0])
220 {
221 case "info":
222 ShowInfo();
223 break;
224
225 case "uptime":
226 Notice(GetUptimeReport());
227 break;
228 }
229 }
230
231 /// <summary>
232 /// Change and load configuration file data.
233 /// </summary>
234 /// <param name="module"></param>
235 /// <param name="cmd"></param>
236 private void HandleConfig(string module, string[] cmd)
237 {
238 List<string> args = new List<string>(cmd);
239 args.RemoveAt(0);
240 string[] cmdparams = args.ToArray();
241
242 if (cmdparams.Length > 0)
243 {
244 string firstParam = cmdparams[0].ToLower();
245
246 switch (firstParam)
247 {
248 case "set":
249 if (cmdparams.Length < 4)
250 {
251 Notice("Syntax: config set <section> <key> <value>");
252 Notice("Example: config set ScriptEngine.DotNetEngine NumberOfScriptThreads 5");
253 }
254 else
255 {
256 IConfig c;
257 IConfigSource source = new IniConfigSource();
258 c = source.AddConfig(cmdparams[1]);
259 if (c != null)
260 {
261 string _value = String.Join(" ", cmdparams, 3, cmdparams.Length - 3);
262 c.Set(cmdparams[2], _value);
263 Config.Merge(source);
264
265 Notice("In section [{0}], set {1} = {2}", c.Name, cmdparams[2], _value);
266 }
267 }
268 break;
269
270 case "get":
271 case "show":
272 if (cmdparams.Length == 1)
273 {
274 foreach (IConfig config in Config.Configs)
275 {
276 Notice("[{0}]", config.Name);
277 string[] keys = config.GetKeys();
278 foreach (string key in keys)
279 Notice(" {0} = {1}", key, config.GetString(key));
280 }
281 }
282 else if (cmdparams.Length == 2 || cmdparams.Length == 3)
283 {
284 IConfig config = Config.Configs[cmdparams[1]];
285 if (config == null)
286 {
287 Notice("Section \"{0}\" does not exist.",cmdparams[1]);
288 break;
289 }
290 else
291 {
292 if (cmdparams.Length == 2)
293 {
294 Notice("[{0}]", config.Name);
295 foreach (string key in config.GetKeys())
296 Notice(" {0} = {1}", key, config.GetString(key));
297 }
298 else
299 {
300 Notice(
301 "config get {0} {1} : {2}",
302 cmdparams[1], cmdparams[2], config.GetString(cmdparams[2]));
303 }
304 }
305 }
306 else
307 {
308 Notice("Syntax: config {0} [<section>] [<key>]", firstParam);
309 Notice("Example: config {0} ScriptEngine.DotNetEngine NumberOfScriptThreads", firstParam);
310 }
311
312 break;
313
314 case "save":
315 if (cmdparams.Length < 2)
316 {
317 Notice("Syntax: config save <path>");
318 return;
319 }
320
321 string path = cmdparams[1];
322 Notice("Saving configuration file: {0}", path);
323
324 if (Config is IniConfigSource)
325 {
326 IniConfigSource iniCon = (IniConfigSource)Config;
327 iniCon.Save(path);
328 }
329 else if (Config is XmlConfigSource)
330 {
331 XmlConfigSource xmlCon = (XmlConfigSource)Config;
332 xmlCon.Save(path);
333 }
334
335 break;
336 }
337 }
338 }
339
340 private void HandleSetLogLevel(string module, string[] cmd)
341 {
342 if (cmd.Length != 4)
343 {
344 Notice("Usage: set log level <level>");
345 return;
346 }
347
348 if (null == m_consoleAppender)
349 {
350 Notice("No appender named Console found (see the log4net config file for this executable)!");
351 return;
352 }
353
354 string rawLevel = cmd[3];
355
356 ILoggerRepository repository = LogManager.GetRepository();
357 Level consoleLevel = repository.LevelMap[rawLevel];
358
359 if (consoleLevel != null)
360 m_consoleAppender.Threshold = consoleLevel;
361 else
362 Notice(
363 "{0} is not a valid logging level. Valid logging levels are ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF",
364 rawLevel);
365
366 ShowLogLevel();
367 }
368
369 private void ShowLogLevel()
370 {
371 Notice("Console log level is {0}", m_consoleAppender.Threshold);
372 }
373
374 protected virtual void HandleScript(string module, string[] parms)
375 {
376 if (parms.Length != 2)
377 {
378 Notice("Usage: command-script <path-to-script");
379 return;
380 }
381
382 RunCommandScript(parms[1]);
383 }
384
385 /// <summary>
386 /// Run an optional startup list of commands
387 /// </summary>
388 /// <param name="fileName"></param>
389 protected void RunCommandScript(string fileName)
390 {
391 if (m_console == null)
392 return;
393
394 if (File.Exists(fileName))
395 {
396 m_log.Info("[SERVER BASE]: Running " + fileName);
397
398 using (StreamReader readFile = File.OpenText(fileName))
399 {
400 string currentCommand;
401 while ((currentCommand = readFile.ReadLine()) != null)
402 {
403 currentCommand = currentCommand.Trim();
404 if (!(currentCommand == ""
405 || currentCommand.StartsWith(";")
406 || currentCommand.StartsWith("//")
407 || currentCommand.StartsWith("#")))
408 {
409 m_log.Info("[SERVER BASE]: Running '" + currentCommand + "'");
410 m_console.RunCommand(currentCommand);
411 }
412 }
413 }
414 }
43 } 415 }
44 416
45 /// <summary> 417 /// <summary>
@@ -54,5 +426,141 @@ namespace OpenSim.Framework.Servers
54 426
55 return sb.ToString(); 427 return sb.ToString();
56 } 428 }
429
430 protected void ShowInfo()
431 {
432 Notice(GetVersionText());
433 Notice("Startup directory: " + m_startupDirectory);
434 if (null != m_consoleAppender)
435 Notice(String.Format("Console log level: {0}", m_consoleAppender.Threshold));
436 }
437
438 /// <summary>
439 /// Enhance the version string with extra information if it's available.
440 /// </summary>
441 protected void EnhanceVersionInformation()
442 {
443 string buildVersion = string.Empty;
444
445 // The subversion information is deprecated and will be removed at a later date
446 // Add subversion revision information if available
447 // Try file "svn_revision" in the current directory first, then the .svn info.
448 // This allows to make the revision available in simulators not running from the source tree.
449 // FIXME: Making an assumption about the directory we're currently in - we do this all over the place
450 // elsewhere as well
451 string gitDir = "../.git/";
452 string gitRefPointerPath = gitDir + "HEAD";
453
454 string svnRevisionFileName = "svn_revision";
455 string svnFileName = ".svn/entries";
456 string manualVersionFileName = ".version";
457 string inputLine;
458 int strcmp;
459
460 if (File.Exists(manualVersionFileName))
461 {
462 using (StreamReader CommitFile = File.OpenText(manualVersionFileName))
463 buildVersion = CommitFile.ReadLine();
464
465 m_version += buildVersion ?? "";
466 }
467 else if (File.Exists(gitRefPointerPath))
468 {
469// m_log.DebugFormat("[SERVER BASE]: Found {0}", gitRefPointerPath);
470
471 string rawPointer = "";
472
473 using (StreamReader pointerFile = File.OpenText(gitRefPointerPath))
474 rawPointer = pointerFile.ReadLine();
475
476// m_log.DebugFormat("[SERVER BASE]: rawPointer [{0}]", rawPointer);
477
478 Match m = Regex.Match(rawPointer, "^ref: (.+)$");
479
480 if (m.Success)
481 {
482// m_log.DebugFormat("[SERVER BASE]: Matched [{0}]", m.Groups[1].Value);
483
484 string gitRef = m.Groups[1].Value;
485 string gitRefPath = gitDir + gitRef;
486 if (File.Exists(gitRefPath))
487 {
488// m_log.DebugFormat("[SERVER BASE]: Found gitRefPath [{0}]", gitRefPath);
489
490 using (StreamReader refFile = File.OpenText(gitRefPath))
491 {
492 string gitHash = refFile.ReadLine();
493 m_version += gitHash.Substring(0, 7);
494 }
495 }
496 }
497 }
498 else
499 {
500 // Remove the else logic when subversion mirror is no longer used
501 if (File.Exists(svnRevisionFileName))
502 {
503 StreamReader RevisionFile = File.OpenText(svnRevisionFileName);
504 buildVersion = RevisionFile.ReadLine();
505 buildVersion.Trim();
506 RevisionFile.Close();
507 }
508
509 if (string.IsNullOrEmpty(buildVersion) && File.Exists(svnFileName))
510 {
511 StreamReader EntriesFile = File.OpenText(svnFileName);
512 inputLine = EntriesFile.ReadLine();
513 while (inputLine != null)
514 {
515 // using the dir svn revision at the top of entries file
516 strcmp = String.Compare(inputLine, "dir");
517 if (strcmp == 0)
518 {
519 buildVersion = EntriesFile.ReadLine();
520 break;
521 }
522 else
523 {
524 inputLine = EntriesFile.ReadLine();
525 }
526 }
527 EntriesFile.Close();
528 }
529
530 m_version += string.IsNullOrEmpty(buildVersion) ? " " : ("." + buildVersion + " ").Substring(0, 6);
531 }
532 }
533
534 protected string GetVersionText()
535 {
536 return String.Format("Version: {0} (interface version {1})", m_version, VersionInfo.MajorInterfaceVersion);
537 }
538
539 /// <summary>
540 /// Console output is only possible if a console has been established.
541 /// That is something that cannot be determined within this class. So
542 /// all attempts to use the console MUST be verified.
543 /// </summary>
544 /// <param name="msg"></param>
545 protected void Notice(string msg)
546 {
547 if (m_console != null)
548 {
549 m_console.Output(msg);
550 }
551 }
552
553 /// <summary>
554 /// Console output is only possible if a console has been established.
555 /// That is something that cannot be determined within this class. So
556 /// all attempts to use the console MUST be verified.
557 /// </summary>
558 /// <param name="format"></param>
559 /// <param name="components"></param>
560 protected void Notice(string format, params object[] components)
561 {
562 if (m_console != null)
563 m_console.OutputFormat(format, components);
564 }
57 } 565 }
58} \ No newline at end of file 566} \ No newline at end of file