-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeServer.cs
201 lines (178 loc) · 5.93 KB
/
NodeServer.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using Microsoft.Build.Utilities;
using NetNoder.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
namespace NetNoder
{
public class NodeServer
{
public string CommandLineArguments
{
get
{
var commandLineBuilder = new CommandLineBuilder();
var json = AutoCamelCase ? JsonConvertCamelCase(Settings) : JsonConvert.SerializeObject(Settings);
commandLineBuilder.AppendFileNameIfNotNull(PrepareJsonForCommandLine(json));
return commandLineBuilder.ToString();
}
}
public bool AutoCamelCase { get; set; }
public NodeServerSettings Settings;
public NodeServer()
{
AutoCamelCase = true;
}
public NodeServer(NodeServerSettings settings): this()
{
Settings = settings;
}
public string Address
{
get { return GetAddress(); }
}
public bool Monitoring { get; protected set; }
protected Thread MonitoringThead { get; set; }
private Stream Post(string route, object data = null, int timeout = 100000)
{
return HttpPostJson(Address + route.TrimStart('/'), data, timeout);
}
public void Monitor()
{
MonitoringThead = new Thread(() =>
{
try
{
Thread.CurrentThread.IsBackground = true;
while (true)
{
try
{
Start();
Thread.Sleep(Settings.MonitorInterval);
}
catch
{
}
}
}
catch
{
}
});
MonitoringThead.Start();
Monitoring = true;
}
public void StopMonitoring()
{
if (MonitoringThead != null)
{
MonitoringThead.Abort();
}
Monitoring = false;
}
public bool Ping()
{
try
{
Post("netnoderping");
return true;
}
catch (Exception ex)
{
// If the error is anything else then it must
// mean that the server is running
return ex.Message != "Unable to connect to the remote server";
}
}
public bool Start()
{
try
{
if (Ping()) return true;
var codeFile = new FileInfo(Settings.EntryPointFilePath);
if (!codeFile.Exists)
{
throw new Exception("Code file does not exist");
}
var startupCommand = "node " + codeFile.Name + " --netnoder " + CommandLineArguments;
#if DEBUG
startupCommand += " --debug";
#endif
var app = new Process
{
StartInfo = new ProcessStartInfo("cmd", "/c " + startupCommand)
{
WorkingDirectory = codeFile.DirectoryName,
UseShellExecute = true,
WindowStyle = Settings.WindowStyle
}
};
if (Settings.WindowStyle == ProcessWindowStyle.Hidden)
{
app.StartInfo.CreateNoWindow = true;
}
app.Start();
if (Settings.ExpectedServerStartupTime > 0)
{
Thread.Sleep(Settings.ExpectedServerStartupTime);
}
return Ping();
}
catch (Exception ex)
{
throw new Exception("Error Starting Node Server at:" + Settings.EntryPointFilePath, ex);
}
}
public bool Stop()
{
try
{
var response = new StreamReader(Post("netnoderkill", new {pw = Settings.KillPassword})).ReadToEnd();
return response == "Accepted";
}
catch (Exception ex)
{
return ex.Message == "Unable to connect to the remote server";
}
}
protected string GetAddress()
{
return Settings.Location.Protocol + "://" + Settings.Location.Host +
(Settings.Location.Port == 0 ? "" : ":" + Settings.Location.Port) + "/";
}
private static Stream HttpPostJson(string url, object data = null, int timeout = 100000)
{
var http = (HttpWebRequest) WebRequest.Create(new Uri(url));
http.ContentType = "application/json";
http.Method = "POST";
http.Timeout = timeout;
var encoding = new ASCIIEncoding();
var json = JsonConvertCamelCase(data ?? new { });
var bytes = encoding.GetBytes(json);
var newStream = http.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
var response = http.GetResponse();
return response.GetResponseStream();
}
private static string JsonConvertCamelCase(object value)
{
return JsonConvert.SerializeObject(value, Formatting.None, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
}
private static string PrepareJsonForCommandLine(string json)
{
return json.Replace("\"", "<netnoderdq>").Replace("'", "<netnodersq>");
}
}
}