This repository has been archived by the owner on May 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
DaemonManager.cs
208 lines (163 loc) · 6.36 KB
/
DaemonManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#region header
// Wabash - DaemonManager.cs
//
// Alistair J. R. Young
// Arkane Systems
//
// Copyright Arkane Systems 2012-2016. All rights reserved.
//
// Created: 2016-10-16 1:13 AM
#endregion
#region using
using System ;
using System.Diagnostics ;
using System.IO ;
using System.Threading ;
using System.Threading.Tasks ;
using ArkaneSystems.Wabash.Properties ;
using PostSharp.Patterns.Model ;
using PostSharp.Patterns.Threading ;
#endregion
#region header
// Wabash - DaemonManager.cs
//
// Alistair J. R. Young
// Arkane Systems
//
// Copyright Arkane Systems 2012-2016. All rights reserved.
//
// Created: 2016-10-16 1:13 AM
#endregion
namespace ArkaneSystems.Wabash
{
[Synchronized]
public sealed class DaemonManager
{
private const string WslProcess = @"bash.exe" ;
private const string WslArguments = @"-c /usr/bin/start-wabashd" ;
private const int CompatibleDaemonVersion = 4 ;
[Reference]
private readonly Wabash owner ;
[Reference]
private StreamWriter channel ;
[Reference]
private Process wabashd ;
public DaemonManager (Wabash owner) { this.owner = owner ; }
// Start the daemon.
[Reentrant]
public async Task Start ()
{
// Start the WSL process.
this.wabashd = Process.Start (new ProcessStartInfo
{
FileName =
Path.Combine (
Environment.GetFolderPath (
Environment.SpecialFolder
.System),
DaemonManager.WslProcess),
Arguments = DaemonManager.WslArguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true
}) ;
Thread.Sleep (500) ;
if (this.wabashd.HasExited)
{
this.owner.Die (
"Could not start wabashd. Is it already running? If not, is an old lock file (/tmp/wabashd.exe.lock) present?") ;
return ;
}
// Start the reader loop listener.
this.InputReader (this.wabashd.StandardOutput) ;
// Set the channel.
this.channel = this.wabashd.StandardInput ;
// Fire up the server-starter loop.
this.StartServices () ;
}
[Background]
private void StartServices ()
{
foreach (string service in Settings.Default.Services)
{
this.channel.WriteLine ($"strt {service}") ;
Thread.Sleep(250);
}
}
// Stop the daemon.
[Reentrant]
public async Task Stop () => this.channel.WriteLine ("stop") ;
// Ping the daemon.
[Reentrant]
public async Task Ping () => this.channel.WriteLine ("ping") ;
// Daemon input reader loop function.
[Background]
[ExplicitlySynchronized]
private void InputReader (StreamReader output)
{
while (true)
{
string recv = output.ReadLine ().Trim () ;
// Place the string in the log window
this.owner.WriteLogString (recv) ;
// Check length
if (recv.Length < 4)
continue ;
// Action the message
string action = recv.Substring (0, 4) ;
string[] split = recv.Split (' ') ;
switch (action)
{
case "vers":
// version message
int version = int.Parse (split[1]) ;
if (version != DaemonManager.CompatibleDaemonVersion)
{
this.owner.Die ("Incompatible daemon version detected.") ;
}
break ;
case "sess":
// status message
int sessions = int.Parse (split[1]) ;
int daemons = int.Parse (split[3]) ;
this.owner.UpdateCounts (sessions, daemons) ;
break ;
case "stop":
// termination message
this.owner.Exit () ;
// needed to prevent exception when thread terminates.
Thread.CurrentThread.Suspend () ;
continue ;
case "pong":
// ping response
this.owner.Message ("Successfully pinged daemon.") ;
break ;
case "svup":
// service start response
this.owner.Message (recv.Remove (0, 5)) ;
break ;
case "shel":
// Shell identification
this.owner.Shell = split[1] ;
break ;
default:
// error, unknown, or garbled message
try
{
this.wabashd.Kill () ;
}
catch
{
// ignored; if we can't kill it, or its already dead, we can't do anything.
}
this.owner.Die ($"Error, unknown message, or garbled channel: {recv}") ;
// needed to prevent exception when thread terminates.
Thread.CurrentThread.Suspend () ;
continue ;
}
}
// If we fall through the while, we exit function and quit the thread.
}
}
}