ServiceManager.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. using System.ServiceProcess;
  5. using System.Xml;
  6. using System.IO;
  7. public class OpenGridMasterService : System.ServiceProcess.ServiceBase {
  8. private Thread ServiceWorkerThread;
  9. public OpenGridMasterService()
  10. {
  11. CanPauseAndContinue = false;
  12. ServiceName = "OpenGridServices-master";
  13. }
  14. private void InitializeComponent()
  15. {
  16. this.CanPauseAndContinue = false;
  17. this.CanShutdown = true;
  18. this.ServiceName = "OpenGridServices-master";
  19. }
  20. protected override void OnStart(string[] args)
  21. {
  22. ServiceWorkerThread = new Thread(new ThreadStart(MainServiceThread));
  23. ServiceWorkerThread.Start();
  24. }
  25. protected override void OnStop()
  26. {
  27. ServiceWorkerThread.Abort();
  28. }
  29. private void MainServiceThread()
  30. {
  31. try {
  32. StreamReader reader=new StreamReader("opengrid-master-cfg.xml");
  33. string configxml = reader.ReadToEnd();
  34. XmlDocument doc = new XmlDocument();
  35. doc.LoadXml(configxml);
  36. XmlNode rootnode = doc.FirstChild;
  37. if (rootnode.Name != "regions")
  38. {
  39. EventLog.WriteEntry("ERROR! bad XML in opengrid-master-cfg.xml - expected regions tag");
  40. Console.WriteLine("Sorry, could not startup the service - please check your opengrid-master-cfg.xml file: missing regions tag");
  41. (new ServiceController("OpenGridServices-master")).Stop();
  42. }
  43. for(int i=0; i<=rootnode.ChildNodes.Count; i++)
  44. {
  45. if(rootnode.ChildNodes.Item(i).Name != "region") {
  46. EventLog.WriteEntry("nonfatal error - unexpected tag inside regions block of opengrid-master-cfg.xml");
  47. (new ServiceController("OpenGridServices-master")).Stop();
  48. }
  49. }
  50. } catch(Exception e) {
  51. Console.WriteLine(e.ToString());
  52. (new ServiceController("OpenGridServices-master")).Stop();
  53. }
  54. }
  55. private static string SetupGrid()
  56. {
  57. Console.WriteLine("Running external program (OpenGridServices.GridServer.exe) to configure the grid server");
  58. Process p = new Process();
  59. p.StartInfo.Arguments = "-setuponly";
  60. p.StartInfo.FileName = "OpenGridServices.GridServer.exe";
  61. p.Start();
  62. return "<gridserver />"; // we let the gridserver handle it's own setup
  63. }
  64. private static string SetupUser()
  65. {
  66. return "<user></user>";
  67. }
  68. private static string SetupAsset()
  69. {
  70. return "<asset></asset>";
  71. }
  72. private static string SetupRegion()
  73. {
  74. return "<regions></regions>";
  75. }
  76. public static void InitSetup()
  77. {
  78. string choice="";
  79. string GridInfo;
  80. string UserInfo;
  81. string AssetInfo;
  82. string RegionInfo;
  83. bool grid=false;
  84. bool user=false;
  85. bool asset=false;
  86. bool region=false;
  87. while(choice!="OK")
  88. {
  89. Console.Clear();
  90. Console.WriteLine("Please select the components you would like to run on this server:\n");
  91. Console.WriteLine("1 - [" + (grid ? "X" : " ") + "] Grid server - this service handles co-ordinates of regions/sims on the grid");
  92. Console.WriteLine("2 - [" + (user ? "X" : " ") + "] User server - this service handles user login, profiles, inventory and IM");
  93. Console.WriteLine("3 - [" + (asset ? "X" : " ") + "] Asset server - this service handles storage of assets such as textures, objects, sounds, scripts");
  94. 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");
  95. Console.Write("Type a number to toggle a choice or type OK to accept your current choices: ");
  96. choice = Console.ReadLine();
  97. switch(choice)
  98. {
  99. case "1":
  100. grid = (!grid);
  101. break;
  102. case "2":
  103. user = (!user);
  104. break;
  105. case "3":
  106. asset = (!asset);
  107. break;
  108. case "4":
  109. region = (!region);
  110. break;
  111. }
  112. }
  113. if(grid) GridInfo = SetupGrid();
  114. if(user) UserInfo = SetupUser();
  115. if(asset) AssetInfo = SetupAsset();
  116. if(region) RegionInfo = SetupRegion();
  117. }
  118. public static void Main()
  119. {
  120. if(!File.Exists("opengrid-master-cfg.xml"))
  121. {
  122. Console.WriteLine("Could not find a config file, running initial setup");
  123. InitSetup();
  124. }
  125. Console.WriteLine("Starting up OGS master service");
  126. try {
  127. ServiceBase.Run(new OpenGridMasterService());
  128. } catch(Exception e) {
  129. Console.WriteLine("THIS SHOULD NEVER HAPPEN!!!!!!!!!!!!!!!!!!!!!");
  130. Console.WriteLine(e.ToString());
  131. }
  132. }
  133. }