ServiceManager.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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 OpenSim 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.Diagnostics;
  30. using System.Threading;
  31. using System.ServiceProcess;
  32. using System.Xml;
  33. using System.IO;
  34. using libsecondlife;
  35. using OpenSim.GenericConfig;
  36. public class OpenGridMasterService : System.ServiceProcess.ServiceBase {
  37. private Thread ServiceWorkerThread;
  38. private static string GridURL; // URL of grid server
  39. private static string GridSimKey; // key sent from Grid>Sim
  40. private static string SimGridKey; // key sent Sim>Grid
  41. private static string AssetURL; // URL of asset server
  42. private static string UserSendKey; // key sent from user>sim
  43. private static string UserRecvKey; // key sent from sim>user
  44. public OpenGridMasterService()
  45. {
  46. CanPauseAndContinue = false;
  47. ServiceName = "OpenGridServices-master";
  48. }
  49. private void InitializeComponent()
  50. {
  51. this.CanPauseAndContinue = false;
  52. this.CanShutdown = true;
  53. this.ServiceName = "OpenGridServices-master";
  54. }
  55. protected override void OnStart(string[] args)
  56. {
  57. ServiceWorkerThread = new Thread(new ThreadStart(MainServiceThread));
  58. ServiceWorkerThread.Start();
  59. }
  60. protected override void OnStop()
  61. {
  62. ServiceWorkerThread.Abort();
  63. }
  64. private void MainServiceThread()
  65. {
  66. try {
  67. StreamReader reader=new StreamReader("opengrid-master-cfg.xml");
  68. string configxml = reader.ReadToEnd();
  69. XmlDocument doc = new XmlDocument();
  70. doc.LoadXml(configxml);
  71. XmlNode rootnode = doc.FirstChild;
  72. if (rootnode.Name != "regions")
  73. {
  74. EventLog.WriteEntry("ERROR! bad XML in opengrid-master-cfg.xml - expected regions tag");
  75. Console.WriteLine("Sorry, could not startup the service - please check your opengrid-master-cfg.xml file: missing regions tag");
  76. (new ServiceController("OpenGridServices-master")).Stop();
  77. }
  78. for(int i=0; i<=rootnode.ChildNodes.Count; i++)
  79. {
  80. if(rootnode.ChildNodes.Item(i).Name != "region") {
  81. EventLog.WriteEntry("nonfatal error - unexpected tag inside regions block of opengrid-master-cfg.xml");
  82. (new ServiceController("OpenGridServices-master")).Stop();
  83. }
  84. }
  85. } catch(Exception e) {
  86. Console.WriteLine(e.ToString());
  87. (new ServiceController("OpenGridServices-master")).Stop();
  88. }
  89. }
  90. private static string SetupGrid()
  91. {
  92. Console.WriteLine("Running external program (OpenGridServices.GridServer.exe) to configure the grid server");
  93. try {
  94. Process p = new Process();
  95. p.StartInfo.Arguments = "-setuponly";
  96. p.StartInfo.FileName = "OpenGridServices.GridServer.exe";
  97. p.Start();
  98. p.StartInfo.Arguments = "-dumpxmlconf";
  99. p.Start();
  100. XmlConfig GridConf = new XmlConfig("opengrid-cfg.xml");
  101. GridConf.LoadData();
  102. GridURL="http://" + GridConf.GetAttribute("ListenAddr") + ":" + GridConf.GetAttribute("ListenPort") + "/";
  103. StreamReader reader=new StreamReader("opengrid-cfg.xml");
  104. string configxml = reader.ReadToEnd();
  105. return configxml;
  106. } catch(Exception e) {
  107. Console.WriteLine("An error occurred while running the grid server, please rectify it and try again");
  108. Console.WriteLine(e.ToString());
  109. Environment.Exit(1);
  110. }
  111. return "";
  112. }
  113. private static string SetupUser()
  114. {
  115. return "<user></user>";
  116. }
  117. private static string SetupAsset()
  118. {
  119. return "<asset></asset>";
  120. }
  121. private static string SetupRegion()
  122. {
  123. string regionname;
  124. ulong regionlocx;
  125. ulong regionlocy;
  126. string default_terrain;
  127. uint terrain_multiplier;
  128. uint baseport;
  129. string listenaddr;
  130. string simconfigxml;
  131. LLUUID SimUUID;
  132. Console.WriteLine("Setting up region servers");
  133. Console.Write("Please specify a path to store your region data (e.g /etc/opensim/regions: ");
  134. string regionpath=Console.ReadLine();
  135. Console.Write("How many regions would you like to configure now? ");
  136. int numofregions=Convert.ToInt16(Console.ReadLine());
  137. Console.Write("What port should the region servers start listening at (first region is normally 9000, then 9001 the second etc, both TCP+UDP): ");
  138. baseport=Convert.ToUInt16(Console.ReadLine());
  139. listenaddr=Console.ReadLine();
  140. Console.WriteLine("Now ready to configure regions, please answer the questions about each region in turn");
  141. for(int i=0; i<=numofregions; i++) {
  142. Console.WriteLine("Configuring region number " + i.ToString());
  143. Console.Write("Region name: ");
  144. regionname=Console.ReadLine();
  145. Console.Write("Region location X: ");
  146. regionlocx=(ulong)Convert.ToUInt32(Console.ReadLine());
  147. Console.Write("Region location Y: ");
  148. regionlocy=(ulong)Convert.ToUInt32(Console.ReadLine());
  149. Console.Write("Default terrain file: ");
  150. default_terrain=Console.ReadLine();
  151. terrain_multiplier=Convert.ToUInt16(Console.ReadLine());
  152. SimUUID=LLUUID.Random();
  153. simconfigxml="<Root><Config SimUUID=\"" + SimUUID.ToString() + "\" SimName=\"" + regionname + "\" SimLocationX=\"" + regionlocx.ToString() + "\" SimLocationY=\"" + regionlocy.ToString() + "\" Datastore=\"" + Path.Combine(regionpath,(SimUUID.ToString()+"localworld.yap")) + "\" SimListenPort=\"" + (baseport+i).ToString() + "\" SimListenAddress=\"" + listenaddr + "\" TerrainFile=\"" + default_terrain + "\" TerrainMultiplier=\"" + terrain_multiplier.ToString() + "\" GridServerURL=\"\" GridSendKey=\"\" GridRecvKey=\"\" AssetServerURL=\"\" /></Root>";
  154. }
  155. return "<regions></regions>";
  156. }
  157. public static void InitSetup()
  158. {
  159. string choice="";
  160. string GridInfo;
  161. string UserInfo;
  162. string AssetInfo;
  163. string RegionInfo;
  164. bool grid=false;
  165. bool user=false;
  166. bool asset=false;
  167. bool region=false;
  168. while(choice!="OK")
  169. {
  170. Console.Clear();
  171. Console.WriteLine("Please select the components you would like to run on this server:\n");
  172. Console.WriteLine("1 - [" + (grid ? "X" : " ") + "] Grid server - this service handles co-ordinates of regions/sims on the grid");
  173. Console.WriteLine("2 - [" + (user ? "X" : " ") + "] User server - this service handles user login, profiles, inventory and IM");
  174. Console.WriteLine("3 - [" + (asset ? "X" : " ") + "] Asset server - this service handles storage of assets such as textures, objects, sounds, scripts");
  175. 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");
  176. Console.Write("Type a number to toggle a choice or type OK to accept your current choices: ");
  177. choice = Console.ReadLine();
  178. switch(choice)
  179. {
  180. case "1":
  181. grid = (!grid);
  182. break;
  183. case "2":
  184. user = (!user);
  185. break;
  186. case "3":
  187. asset = (!asset);
  188. break;
  189. case "4":
  190. region = (!region);
  191. break;
  192. }
  193. }
  194. if(grid) GridInfo = SetupGrid();
  195. if(user) UserInfo = SetupUser();
  196. if(asset) AssetInfo = SetupAsset();
  197. if(region) RegionInfo = SetupRegion();
  198. }
  199. public static void Main()
  200. {
  201. if(!File.Exists("opengrid-master-cfg.xml"))
  202. {
  203. Console.WriteLine("Could not find a config file, running initial setup");
  204. InitSetup();
  205. }
  206. Console.WriteLine("Starting up OGS master service");
  207. try {
  208. ServiceBase.Run(new OpenGridMasterService());
  209. } catch(Exception e) {
  210. Console.WriteLine("An error occured while initialising OGS master service.");
  211. Console.WriteLine(e.ToString());
  212. }
  213. }
  214. }