opensim-development – 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.IO;
31 using System.Reflection;
32 using System.Threading;
33 using System.Text;
34 using System.Xml;
35 using OpenSim.Framework;
36 using OpenSim.Framework.Console;
37 using OpenSim.Framework.Monitoring;
38 using OpenSim.Framework.Servers;
39 using log4net;
40 using log4net.Config;
41 using log4net.Appender;
42 using log4net.Core;
43 using log4net.Repository;
44 using Nini.Config;
45  
46 namespace OpenSim.Server.Base
47 {
48 public class ServicesServerBase : ServerBase
49 {
50 // Logger
51 //
52 private static readonly ILog m_log =
53 LogManager.GetLogger(
54 MethodBase.GetCurrentMethod().DeclaringType);
55  
56 // Command line args
57 //
58 protected string[] m_Arguments;
59  
60 public string ConfigDirectory
61 {
62 get;
63 private set;
64 }
65  
66 // Run flag
67 //
68 private bool m_Running = true;
69  
70 // Handle all the automagical stuff
71 //
72 public ServicesServerBase(string prompt, string[] args) : base()
73 {
74 // Save raw arguments
75 //
76 m_Arguments = args;
77  
78 // Read command line
79 //
80 ArgvConfigSource argvConfig = new ArgvConfigSource(args);
81  
82 argvConfig.AddSwitch("Startup", "console", "c");
83 argvConfig.AddSwitch("Startup", "logfile", "l");
84 argvConfig.AddSwitch("Startup", "inifile", "i");
85 argvConfig.AddSwitch("Startup", "prompt", "p");
86 argvConfig.AddSwitch("Startup", "logconfig", "g");
87  
88 // Automagically create the ini file name
89 //
90 string fileName = Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location);
91 string iniFile = fileName + ".ini";
92 string logConfig = null;
93  
94 IConfig startupConfig = argvConfig.Configs["Startup"];
95 if (startupConfig != null)
96 {
97 // Check if a file name was given on the command line
98 //
99 iniFile = startupConfig.GetString("inifile", iniFile);
100 //
101 // Check if a prompt was given on the command line
102 prompt = startupConfig.GetString("prompt", prompt);
103 //
104 // Check for a Log4Net config file on the command line
105 logConfig =startupConfig.GetString("logconfig",logConfig);
106 }
107  
108 // Find out of the file name is a URI and remote load it
109 // if it's possible. Load it as a local file otherwise.
110 //
111 Uri configUri;
112  
113 try
114 {
115 if (Uri.TryCreate(iniFile, UriKind.Absolute, out configUri) &&
116 configUri.Scheme == Uri.UriSchemeHttp)
117 {
118 XmlReader r = XmlReader.Create(iniFile);
119 Config = new XmlConfigSource(r);
120 }
121 else
122 {
123 Config = new IniConfigSource(iniFile);
124 }
125 }
126 catch (Exception e)
127 {
128 System.Console.WriteLine("Error reading from config source. {0}", e.Message);
129 Environment.Exit(1);
130 }
131  
132 // Merge the configuration from the command line into the
133 // loaded file
134 //
135 Config.Merge(argvConfig);
136  
137 // Refresh the startupConfig post merge
138 //
139 if (Config.Configs["Startup"] != null)
140 {
141 startupConfig = Config.Configs["Startup"];
142 }
143  
144 ConfigDirectory = startupConfig.GetString("ConfigDirectory", ".");
145  
146 prompt = startupConfig.GetString("Prompt", prompt);
147  
148 // Allow derived classes to load config before the console is
149 // opened.
150 //
151 ReadConfig();
152  
153 // Create main console
154 //
155 string consoleType = "local";
156 if (startupConfig != null)
157 consoleType = startupConfig.GetString("console", consoleType);
158  
159 if (consoleType == "basic")
160 {
161 MainConsole.Instance = new CommandConsole(prompt);
162 }
163 else if (consoleType == "rest")
164 {
165 MainConsole.Instance = new RemoteConsole(prompt);
166 ((RemoteConsole)MainConsole.Instance).ReadConfig(Config);
167 }
168 else
169 {
170 MainConsole.Instance = new LocalConsole(prompt);
171 }
172  
173 m_console = MainConsole.Instance;
174  
175 if (logConfig != null)
176 {
177 FileInfo cfg = new FileInfo(logConfig);
178 XmlConfigurator.Configure(cfg);
179 }
180 else
181 {
182 XmlConfigurator.Configure();
183 }
184  
185 LogEnvironmentInformation();
186 RegisterCommonAppenders(startupConfig);
187  
188 if (startupConfig.GetString("PIDFile", String.Empty) != String.Empty)
189 {
190 CreatePIDFile(startupConfig.GetString("PIDFile"));
191 }
192  
193 RegisterCommonCommands();
194 RegisterCommonComponents(Config);
195  
196 // Allow derived classes to perform initialization that
197 // needs to be done after the console has opened
198 //
199 Initialise();
200 }
201  
202 public bool Running
203 {
204 get { return m_Running; }
205 }
206  
207 public virtual int Run()
208 {
209 Watchdog.Enabled = true;
210 MemoryWatchdog.Enabled = true;
211  
212 while (m_Running)
213 {
214 try
215 {
216 MainConsole.Instance.Prompt();
217 }
218 catch (Exception e)
219 {
220 m_log.ErrorFormat("Command error: {0}", e);
221 }
222 }
223  
224 RemovePIDFile();
225  
226 return 0;
227 }
228  
229 protected override void ShutdownSpecific()
230 {
231 m_Running = false;
232 m_log.Info("[CONSOLE] Quitting");
233  
234 base.ShutdownSpecific();
235 }
236  
237 protected virtual void ReadConfig()
238 {
239 }
240  
241 protected virtual void Initialise()
242 {
243 }
244 }
245 }