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
|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Program.cs" company="HandBrake Project (http://handbrake.fr)">
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
// Manage the HandBrake Worker Process Service.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrake.Worker
{
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using HandBrake.Interop.Interop;
using HandBrake.Worker.Routing;
using HandBrake.Worker.Services;
using HandBrake.Worker.Services.Interfaces;
using HandBrake.Worker.Utilities;
public class Program
{
private static readonly ITokenService TokenService = new TokenService();
private static ApiRouter router;
private static ManualResetEvent manualResetEvent = new ManualResetEvent(false);
public static void Main(string[] args)
{
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
Portable.Initialise();
if (!Portable.IsProcessIsolationEnabled())
{
Console.WriteLine("Worker is disabled in portable.ini");
Console.WriteLine("Press 'Enter' to exit");
Console.Read();
return;
}
int port = 8037; // Default Port;
string token;
if (args.Length != 0)
{
foreach (string argument in args)
{
if (argument.StartsWith("--port"))
{
string value = argument.TrimStart("--port=".ToCharArray());
if (int.TryParse(value, out var parsedPort))
{
port = parsedPort;
}
}
if (argument.StartsWith("--token"))
{
token = argument.TrimStart("--token=".ToCharArray());
TokenService.RegisterToken(token);
}
}
}
Console.WriteLine("Worker: Starting HandBrake Engine ...");
router = new ApiRouter();
router.TerminationEvent += Router_TerminationEvent;
Console.WriteLine("Worker: Starting Web Server on port {0} ...", port);
Dictionary<string, Func<HttpListenerRequest, string>> apiHandlers = RegisterApiHandlers();
HttpServer webServer = new HttpServer(apiHandlers, port, TokenService);
if (webServer.Run())
{
Console.WriteLine("Worker: Server Started");
manualResetEvent.WaitOne();
webServer.Stop();
}
else
{
Console.WriteLine("Worker: Failed to start. Exiting ...");
}
}
private static void CurrentDomain_ProcessExit(object sender, EventArgs e)
{
HandBrakeUtils.DisposeGlobal();
}
private static Dictionary<string, Func<HttpListenerRequest, string>> RegisterApiHandlers()
{
Dictionary<string, Func<HttpListenerRequest, string>> apiHandlers =
new Dictionary<string, Func<HttpListenerRequest, string>>();
// Process Handling
apiHandlers.Add("Shutdown", ShutdownServer);
apiHandlers.Add("IsTokenSet", IsTokenSet);
apiHandlers.Add("RegisterToken", RegisterToken);
apiHandlers.Add("Version", router.GetVersionInfo);
// Logging
apiHandlers.Add("GetAllLogMessages", router.GetAllLogMessages);
apiHandlers.Add("GetLogMessagesFromIndex", router.GetLogMessagesFromIndex);
apiHandlers.Add("ResetLogging", router.ResetLogging);
// HandBrake APIs
apiHandlers.Add("StartEncode", router.StartEncode);
apiHandlers.Add("PauseEncode", router.PauseEncode);
apiHandlers.Add("ResumeEncode", router.ResumeEncode);
apiHandlers.Add("StopEncode", router.StopEncode);
apiHandlers.Add("PollEncodeProgress", router.PollEncodeProgress);
return apiHandlers;
}
private static string RegisterToken(HttpListenerRequest request)
{
string requestPostData = HttpUtilities.GetRequestPostData(request);
if (!string.IsNullOrEmpty(requestPostData))
{
TokenService.RegisterToken(requestPostData);
return true.ToString();
}
return false.ToString();
}
private static string IsTokenSet(HttpListenerRequest arg)
{
return TokenService.IsTokenSet().ToString();
}
private static void Router_TerminationEvent(object sender, EventArgs e)
{
Console.WriteLine("Worker: Termination event received. ");
manualResetEvent.Set();
Environment.Exit(0);
}
private static string ShutdownServer(HttpListenerRequest request)
{
manualResetEvent.Set();
return "Server Terminated";
}
}
}
|