Allow schema files that are missing checksums on the !!SCHEMAMATIC line.
[versaplex.git] / versaplexd / versaplexd-svc.cs
blob42d448c0f764c376e428a52b3f574f660564ef02
1 /*
2 * Versaplex:
3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
5 */
6 using System;
7 using System.Collections;
8 using System.Collections.Generic;
9 using System.ComponentModel;
10 using System.Configuration.Install;
11 using System.IO;
12 using System.ServiceProcess;
13 using System.Threading;
14 using Wv;
15 using Wv.NDesk.Options;
16 using Wv.Extensions;
19 [RunInstaller(true)]
20 public class VersaServiceInstaller : Installer
22 public static bool auth = false;
23 public static string servname = "Versaplex";
24 ServiceProcessInstaller spi;
25 ServiceInstaller si;
27 public VersaServiceInstaller()
29 spi = new ServiceProcessInstaller();
30 si = new ServiceInstaller();
32 // if auth, leave these alone and the ServiceInstaller should prompt
33 // for authorization.
34 if (!auth)
36 spi.Account = ServiceAccount.LocalSystem;
37 spi.Password = null;
38 spi.Username = null;
41 si.ServiceName = servname;
42 si.Description = "Provides a secure connection to SQL Server";
43 si.StartType = ServiceStartMode.Automatic;
45 Installers.AddRange(new Installer[] { si, spi });
50 public class VersaService : ServiceBase
52 WvLog log = new WvLog("versaplexd-svc", WvLog.L.Info);
53 Thread t = null;
55 public VersaService()
57 WvLog.recv = new WvLogFile(wv.PathJoin(VersaMain.mydir,
58 "versaplex.log"),
59 "a");
60 log.print("Initializing.\n");
63 void go()
65 try
67 Versaplexd.Go(VersaMain.config_path,
68 "tcp:host=127.0.0.1,port=5561",
69 new string[] { "tcp:0.0.0.0:5561" });
71 catch (Exception e)
73 log.print(WvLog.L.Error, "Exception: {0}\n", e.Message);
77 protected override void OnStart(string[] args)
79 log.print("Starting.\n");
80 wv.assert(t == null);
81 t = new Thread(go);
82 t.Start();
85 protected override void OnStop()
87 log.print("Stopping...\n");
88 wv.assert(t != null);
89 Versaplexd.want_to_die = true;
91 t.Join();
92 log.print("Stopped.\n");
95 protected override void OnContinue()
97 log.print("Continuing.");
102 public static class VersaMain
104 static void ShowHelp()
106 wv.printerr("Usage: versaplexd-svc <-i | -u>\n" +
107 " -i: install as a Windows service\n" +
108 " -u: uninstall Windows service\n" +
109 " -n: service name (default=Versaplex)\n" +
110 " -A: prompt for account/password info\n");
111 Environment.Exit(1);
114 static void Uninstall()
116 using (var inst
117 = new AssemblyInstaller(typeof(VersaService).Assembly,
118 new string[] { "/logfile" }))
120 inst.UseNewContext = true;
121 inst.Uninstall(new Hashtable());
125 static void Install()
127 // We might already be installed, so uninstall first. But we might
128 // *not* be installed, which is an error, so ignore it.
129 try { Uninstall(); } catch { }
131 using (var inst
132 = new AssemblyInstaller(typeof(VersaService).Assembly,
133 new string[] { "/logfile" }))
135 inst.UseNewContext = true;
137 IDictionary state = new Hashtable();
140 inst.Install(state);
141 inst.Commit(state);
143 catch
145 try { inst.Rollback(state); } catch { }
146 throw;
150 new ServiceController(VersaServiceInstaller.servname).Start();
153 internal static string mydir
154 = Path.GetDirectoryName(typeof(VersaMain).Assembly.Location);
155 internal static string config_path
156 = wv.PathJoin(mydir, "versaplexd.ini");
158 public static int Main(string[] args)
162 bool install = false, uninstall = false;
163 new OptionSet()
164 .Add("i|install",
165 delegate(string v) { install = true; })
166 .Add("u|uninstall",
167 delegate(string v) { uninstall = true; })
168 .Add("n|name=",
169 delegate(string v) { VersaServiceInstaller.servname = v; })
170 .Add("A|authorize",
171 delegate(string v) { VersaServiceInstaller.auth = true; })
172 .Add("?|h|help",
173 delegate(string v) { ShowHelp(); })
174 .Parse(args);
176 if (install)
179 if (!File.Exists(config_path))
180 throw new Exception(wv.fmt("Config file missing: {0}",
181 config_path));
182 Install();
184 else if (uninstall)
185 Uninstall();
186 else
187 ServiceBase.Run(new ServiceBase[] { new VersaService() });
188 return 0;
190 catch (Exception e)
192 wv.printerr("versaplex: {0}\n", e.Message);
193 return 99;