2007-05-13 21:01:21 +00:00
using System ;
2007-05-14 01:50:13 +00:00
using System.Diagnostics ;
2007-05-13 21:01:21 +00:00
using System.Threading ;
using System.ServiceProcess ;
2007-05-14 00:08:52 +00:00
using System.Xml ;
using System.IO ;
2007-05-13 21:01:21 +00:00
public class OpenGridMasterService : System . ServiceProcess . ServiceBase {
private Thread ServiceWorkerThread ;
public OpenGridMasterService ( )
{
CanPauseAndContinue = false ;
ServiceName = "OpenGridServices-master" ;
}
private void InitializeComponent ( )
{
this . CanPauseAndContinue = false ;
this . CanShutdown = true ;
this . ServiceName = "OpenGridServices-master" ;
}
protected override void OnStart ( string [ ] args )
{
2007-05-14 00:08:52 +00:00
ServiceWorkerThread = new Thread ( new ThreadStart ( MainServiceThread ) ) ;
ServiceWorkerThread . Start ( ) ;
}
protected override void OnStop ( )
{
ServiceWorkerThread . Abort ( ) ;
}
private void MainServiceThread ( )
{
try {
StreamReader reader = new StreamReader ( "opengrid-master-cfg.xml" ) ;
string configxml = reader . ReadToEnd ( ) ;
XmlDocument doc = new XmlDocument ( ) ;
doc . LoadXml ( configxml ) ;
XmlNode rootnode = doc . FirstChild ;
if ( rootnode . Name ! = "regions" )
{
EventLog . WriteEntry ( "ERROR! bad XML in opengrid-master-cfg.xml - expected regions tag" ) ;
Console . WriteLine ( "Sorry, could not startup the service - please check your opengrid-master-cfg.xml file: missing regions tag" ) ;
( new ServiceController ( "OpenGridServices-master" ) ) . Stop ( ) ;
}
for ( int i = 0 ; i < = rootnode . ChildNodes . Count ; i + + )
{
if ( rootnode . ChildNodes . Item ( i ) . Name ! = "region" ) {
EventLog . WriteEntry ( "nonfatal error - unexpected tag inside regions block of opengrid-master-cfg.xml" ) ;
( new ServiceController ( "OpenGridServices-master" ) ) . Stop ( ) ;
}
}
} catch ( Exception e ) {
Console . WriteLine ( e . ToString ( ) ) ;
( new ServiceController ( "OpenGridServices-master" ) ) . Stop ( ) ;
}
2007-05-13 21:01:21 +00:00
}
2007-05-14 01:38:52 +00:00
private static string SetupGrid ( )
{
Console . WriteLine ( "Running external program (OpenGridServices.GridServer.exe) to configure the grid server" ) ;
2007-05-14 01:50:13 +00:00
Process p = new Process ( ) ;
p . StartInfo . Arguments = "-setuponly" ;
p . StartInfo . FileName = "OpenGridServices.GridServer.exe" ;
p . Start ( ) ;
return "<gridserver />" ; // we let the gridserver handle it's own setup
2007-05-14 01:38:52 +00:00
}
private static string SetupUser ( )
{
return "<user></user>" ;
}
private static string SetupAsset ( )
{
return "<asset></asset>" ;
}
private static string SetupRegion ( )
{
return "<regions></regions>" ;
}
2007-05-14 01:20:10 +00:00
public static void InitSetup ( )
{
string choice = "" ;
2007-05-14 01:38:52 +00:00
string GridInfo ;
string UserInfo ;
string AssetInfo ;
string RegionInfo ;
2007-05-14 01:20:10 +00:00
bool grid = false ;
bool user = false ;
bool asset = false ;
bool region = false ;
while ( choice ! = "OK" )
{
Console . Clear ( ) ;
Console . WriteLine ( "Please select the components you would like to run on this server:\n" ) ;
Console . WriteLine ( "1 - [" + ( grid ? "X" : " " ) + "] Grid server - this service handles co-ordinates of regions/sims on the grid" ) ;
Console . WriteLine ( "2 - [" + ( user ? "X" : " " ) + "] User server - this service handles user login, profiles, inventory and IM" ) ;
Console . WriteLine ( "3 - [" + ( asset ? "X" : " " ) + "] Asset server - this service handles storage of assets such as textures, objects, sounds, scripts" ) ;
Console . WriteLine ( "4 - [" + ( region ? "X" : " " ) + "] Region server - this is the main opensim server and can run without the above services, it handles physics simulation, terrain, building and other such features" ) ;
Console . Write ( "Type a number to toggle a choice or type OK to accept your current choices: " ) ;
choice = Console . ReadLine ( ) ;
switch ( choice )
{
case "1" :
grid = ( ! grid ) ;
break ;
case "2" :
user = ( ! user ) ;
break ;
case "3" :
asset = ( ! asset ) ;
break ;
case "4" :
region = ( ! region ) ;
break ;
}
}
2007-05-14 01:38:52 +00:00
if ( grid ) GridInfo = SetupGrid ( ) ;
if ( user ) UserInfo = SetupUser ( ) ;
if ( asset ) AssetInfo = SetupAsset ( ) ;
if ( region ) RegionInfo = SetupRegion ( ) ;
2007-05-14 01:20:10 +00:00
}
2007-05-13 21:01:21 +00:00
public static void Main ( )
{
2007-05-14 01:20:10 +00:00
if ( ! File . Exists ( "opengrid-master-cfg.xml" ) )
{
Console . WriteLine ( "Could not find a config file, running initial setup" ) ;
InitSetup ( ) ;
}
2007-05-14 00:08:52 +00:00
Console . WriteLine ( "Starting up OGS master service" ) ;
2007-05-14 00:16:44 +00:00
try {
ServiceBase . Run ( new OpenGridMasterService ( ) ) ;
} catch ( Exception e ) {
Console . WriteLine ( "THIS SHOULD NEVER HAPPEN!!!!!!!!!!!!!!!!!!!!!" ) ;
Console . WriteLine ( e . ToString ( ) ) ;
}
2007-05-13 21:01:21 +00:00
}
}