Skip to content

Commit

Permalink
WinGui: Building out the Web API needed for the background worker pro…
Browse files Browse the repository at this point in the history
…cess. Includes initial support for encoding, logging and basic utility commands.
  • Loading branch information
sr55 committed Dec 27, 2019
1 parent c8e4c8c commit 0ca1b03
Show file tree
Hide file tree
Showing 11 changed files with 515 additions and 29 deletions.
68 changes: 65 additions & 3 deletions win/CS/HandBrake.Worker/ApiRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,39 @@

namespace HandBrake.Worker
{
using System;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;

using HandBrake.Interop.Interop;
using HandBrake.Interop.Interop.Json.State;
using HandBrake.Interop.Utilities;
using HandBrake.Worker.Logging;
using HandBrake.Worker.Logging.Interfaces;
using HandBrake.Worker.Logging.Models;

using Newtonsoft.Json;

public class ApiRouter
{
private readonly JsonSerializerSettings jsonNetSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
private HandBrakeInstance handbrakeInstance;
private ILogHandler logHandler;

public string Initialise(HttpListenerRequest request)
public string Initialise(int verbosity)
{
if (this.handbrakeInstance == null)
{
this.handbrakeInstance = new HandBrakeInstance();
}

// TODO support verbosity
this.handbrakeInstance.Initialize(1, true); // TODO enable user setting support for nohardware
if (this.logHandler == null)
{
this.logHandler = new LogHandler();
}

this.handbrakeInstance.Initialize(verbosity, true);

return null;
}
Expand All @@ -48,6 +58,7 @@ public string StartEncode(HttpListenerRequest request)
{
string requestPostData = GetRequestPostData(request);

Console.WriteLine(requestPostData);
this.handbrakeInstance.StartEncode(requestPostData);

return null;
Expand Down Expand Up @@ -92,6 +103,57 @@ public string SetConfiguration(HttpListenerRequest request)
return null;
}


/* Logging API */

// POST - JSON
public string ConfigureLogging(HttpListenerRequest request)
{
string requestPostData = GetRequestPostData(request);

JsonSerializerSettings settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
};
LogHandlerConfig config;
if (!string.IsNullOrEmpty(requestPostData))
{
config = JsonConvert.DeserializeObject<LogHandlerConfig>(requestPostData, settings);
}
else
{
config = new LogHandlerConfig(false, null, false, "Logging Not Configured!");
}

this.logHandler.ConfigureLogging(config);
return null;
}

// GET
public string GetFullLog(HttpListenerRequest request)
{
return this.logHandler.GetFullLog();
}

// POST
public string GetLogFromIndex(HttpListenerRequest request)
{
string requestPostData = GetRequestPostData(request);

if (int.TryParse(requestPostData, out int index))
{
return this.logHandler.GetLogFromIndex(index);
}

return null;
}

// POST
public string GetLatestLogIndex(HttpListenerRequest request)
{
return this.logHandler.GetLatestLogIndex().ToString();
}

private static string GetRequestPostData(HttpListenerRequest request)
{
if (!request.HasEntityBody)
Expand Down
2 changes: 1 addition & 1 deletion win/CS/HandBrake.Worker/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
</configuration>
16 changes: 12 additions & 4 deletions win/CS/HandBrake.Worker/HandBrake.Worker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<OutputType>Exe</OutputType>
<RootNamespace>HandBrake.Worker</RootNamespace>
<AssemblyName>HandBrake.Worker</AssemblyName>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
Expand All @@ -33,6 +33,9 @@
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>true</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>handbrakepineapple.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -43,16 +46,17 @@
<ItemGroup>
<Compile Include="ApiRouter.cs" />
<Compile Include="HttpServer.cs" />
<Compile Include="Logging\Interfaces\ILogHandler.cs" />
<Compile Include="Logging\LogHandler.cs" />
<Compile Include="Logging\Models\LogHandlerConfig.cs" />
<Compile Include="Logging\Models\LogMessage.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="gong-wpf-dragdrop">
<Version>2.0.1</Version>
</PackageReference>
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.2</Version>
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
Expand All @@ -64,5 +68,9 @@
<Name>HandBrake.Interop</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="handbrakepineapple.ico" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
4 changes: 3 additions & 1 deletion win/CS/HandBrake.Worker/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public HttpServer(Dictionary<string, Func<HttpListenerRequest, string>> apiCalls
Console.WriteLine(Environment.NewLine + "Available APIs: ");
foreach (KeyValuePair<string, Func<HttpListenerRequest, string>> api in apiCalls)
{
string url = string.Format("http://localhost:{0}/{1}/", port, api.Key);
string url = string.Format("http://127.0.0.1:{0}/{1}/", port, api.Key);
this.httpListener.Prefixes.Add(url);
Console.WriteLine(url);
}
Expand Down Expand Up @@ -66,6 +66,8 @@ public void Run()
{
string path = context.Request.RawUrl.TrimStart('/').TrimEnd('/');

Console.WriteLine("Handling call to: " + path);

if (this.apiHandlers.TryGetValue(path, out var actionToPerform))
{
string rstr = actionToPerform(context.Request);
Expand Down
43 changes: 43 additions & 0 deletions win/CS/HandBrake.Worker/Logging/Interfaces/ILogHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ILogHandler.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>
// Defines the ILogHandler type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace HandBrake.Worker.Logging.Interfaces
{
using HandBrake.Worker.Logging.Models;

public interface ILogHandler
{
/// <summary>
/// Enable logging for this worker process.
/// </summary>
/// <param name="config">
/// Configuration for the logger.
/// </param>
/// <remarks>
/// If this is not called, all log messages from libhb will be ignored.
/// </remarks>
void ConfigureLogging(LogHandlerConfig config);

string GetFullLog();

long GetLatestLogIndex();

/// <summary>
/// Get the log data from a given index
/// </summary>
/// <param name="index">index is zero based</param>
/// <returns>Full log as a string</returns>
string GetLogFromIndex(int index);

/// <summary>
/// Empty the log cache and reset the log handler to defaults.
/// </summary>
void Reset();
}
}
Loading

0 comments on commit 0ca1b03

Please sign in to comment.