3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
7 using System
.Collections
;
8 using System
.Collections
.Generic
;
9 using System
.ComponentModel
;
10 using System
.Configuration
.Install
;
12 using System
.ServiceProcess
;
13 using System
.Threading
;
15 using Wv
.NDesk
.Options
;
20 public class VersaServiceInstaller
: Installer
22 public static bool auth
= false;
23 public static string servname
= "Versaplex";
24 ServiceProcessInstaller spi
;
27 public VersaServiceInstaller()
29 spi
= new ServiceProcessInstaller();
30 si
= new ServiceInstaller();
32 // if auth, leave these alone and the ServiceInstaller should prompt
36 spi
.Account
= ServiceAccount
.LocalSystem
;
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
);
57 WvLog
.recv
= new WvLogFile(wv
.PathJoin(VersaMain
.mydir
,
60 log
.print("Initializing.\n");
67 Versaplexd
.Go(VersaMain
.config_path
,
68 "tcp:host=127.0.0.1,port=5561",
69 new string[] { "tcp:0.0.0.0:5561" }
);
73 log
.print(WvLog
.L
.Error
, "Exception: {0}\n", e
.Message
);
77 protected override void OnStart(string[] args
)
79 log
.print("Starting.\n");
85 protected override void OnStop()
87 log
.print("Stopping...\n");
89 Versaplexd
.want_to_die
= true;
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");
114 static void Uninstall()
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 { }
132 = new AssemblyInstaller(typeof(VersaService
).Assembly
,
133 new string[] { "/logfile" }
))
135 inst
.UseNewContext
= true;
137 IDictionary state
= new Hashtable();
145 try { inst.Rollback(state); }
catch { }
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;
165 delegate(string v
) { install = true; }
)
167 delegate(string v
) { uninstall = true; }
)
169 delegate(string v
) { VersaServiceInstaller.servname = v; }
)
171 delegate(string v
) { VersaServiceInstaller.auth = true; }
)
173 delegate(string v
) { ShowHelp(); }
)
179 if (!File
.Exists(config_path
))
180 throw new Exception(wv
.fmt("Config file missing: {0}",
187 ServiceBase
.Run(new ServiceBase
[] { new VersaService() }
);
192 wv
.printerr("versaplex: {0}\n", e
.Message
);