Skip to content

Commit

Permalink
Added Basic Win32 Test Client to make sure everything works
Browse files Browse the repository at this point in the history
Consider it a bit of a reference client. Some refactoring needs to happen to the project so that models can be shared between the server and this project.
  • Loading branch information
pgodwin committed Jul 23, 2017
1 parent 65b43a2 commit 515fe24
Show file tree
Hide file tree
Showing 27 changed files with 1,836 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,6 @@ __pycache__/
*.xsd.cs
/YouTubeProxy/ExternalBin/ffmpeg.exe
YouTubeProxy/obj/Debug/YouTubeProxy.csproj.FileListAbsolute.txt
QTConverter/obj/Debug/QTConverter.csproj.FileListAbsolute.txt
BMFF/BMFF/MatrixIO.IO/obj/Debug/MatrixIO.IO.csproj.FileListAbsolute.txt
QTCommon/obj/Debug/QTCommon.csproj.FileListAbsolute.txt
11 changes: 10 additions & 1 deletion 68kTubeProxy.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QTConverter", "QTConverter\QTConverter.csproj", "{A99D53E3-D57C-4059-8AAA-979E2F9E9E1F}"
EndProject
Expand All @@ -13,6 +13,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MatrixIO.IO", "BMFF\BMFF\Ma
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MatrixIO.IO.Bmff", "BMFF\BMFF\MatrixIO.IO.Bmff\MatrixIO.IO.Bmff.csproj", "{4A565C90-1850-4A15-816D-E332981735AB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Win32Client", "Win32Client\Win32Client.csproj", "{ABEA9A3E-E9B0-417B-8DD1-76865FF57CD5}"
ProjectSection(ProjectDependencies) = postProject
{3782F686-0E09-4E91-94EA-729B09F5CAC4} = {3782F686-0E09-4E91-94EA-729B09F5CAC4}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -39,6 +44,10 @@ Global
{4A565C90-1850-4A15-816D-E332981735AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A565C90-1850-4A15-816D-E332981735AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4A565C90-1850-4A15-816D-E332981735AB}.Release|Any CPU.Build.0 = Release|Any CPU
{ABEA9A3E-E9B0-417B-8DD1-76865FF57CD5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ABEA9A3E-E9B0-417B-8DD1-76865FF57CD5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ABEA9A3E-E9B0-417B-8DD1-76865FF57CD5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ABEA9A3E-E9B0-417B-8DD1-76865FF57CD5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 6 additions & 0 deletions Win32Client/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
43 changes: 43 additions & 0 deletions Win32Client/Client/YouTubeProxyClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Win32Client.Helpers;
using Win32Client.Models;

namespace Win32Client.Client
{
public class YouTubeProxyClient
{
private WebClient client;
const char separator = '|';



public YouTubeProxyClient(string baseUrl)
{
client = new WebClient();
client.BaseAddress = baseUrl;
}

public List<SearchResultModel> Search(string terms)
{
var csvResult = client.DownloadData(string.Format("api/Search/{0}", Uri.EscapeDataString(terms))).MacRomanToString();

var results = MacDeserialiser.Deserialize<SearchResultModel>(csvResult);

return results.ToList();
}

public List<ConversionStatusModel> GetVideo(string videoId, string profile)
{
var csvResult = client.DownloadData(string.Format("api/Video/{0}/?profile={1}", videoId, profile)).MacRomanToString();

var results = MacDeserialiser.Deserialize<ConversionStatusModel>(csvResult);

return results.ToList();
}
}
}
57 changes: 57 additions & 0 deletions Win32Client/ClientSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Win32Client
{
public class ClientSettings
{
const string SettingsFile = "ClientSettings.json";

public static ClientSettings GetSettings()
{
if (File.Exists(SettingsFile))
{
try
{
return JsonConvert.DeserializeObject<ClientSettings>(File.ReadAllText(SettingsFile));
}
catch (Exception ex)
{
// Log it?
}
}
return ClientSettings.DefaultSettings;
}



public static ClientSettings DefaultSettings
{
get
{
return new ClientSettings
{
BaseUrl = "http://localhost:9000/",
Profile = "h263"
};
}
}

public string BaseUrl
{
get;set;
}

public string Profile { get; set; }

public void SaveSettings()
{
File.WriteAllText(SettingsFile, JsonConvert.SerializeObject(this));
}
}
}
29 changes: 29 additions & 0 deletions Win32Client/Helpers/ControlHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Win32Client.Helpers
{
public static class ControlHelper
{
/// <summary>
/// Executes the Action asynchronously on the UI thread, does not block execution on the calling thread.
/// </summary>
/// <param name="control"></param>
/// <param name="code"></param>
public static void UIThread(this Control @this, Action code)
{
if (@this.InvokeRequired)
{
@this.BeginInvoke(code);
}
else
{
code.Invoke();
}
}
}
}
72 changes: 72 additions & 0 deletions Win32Client/Helpers/ResultDeserialiser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Win32Client.Helpers
{
public static class MacDeserialiser
{
const char CsvSeparator = '|';

public static IEnumerable<T> Deserialize<T>(string input)
{
var fields =
(from mi in typeof(T).GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)
where new[] { MemberTypes.Field, MemberTypes.Property }.Contains(mi.MemberType)
let orderAttr = (ColumnOrderAttribute)Attribute.GetCustomAttribute(mi, typeof(ColumnOrderAttribute))
orderby orderAttr == null ? int.MaxValue : orderAttr.Order, mi.Name
select mi).ToArray();

var lines = input.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
var result = new List<T>();
foreach (var line in lines)
{
var item = line.Split(CsvSeparator);
var record = (T)Activator.CreateInstance(typeof(T));

for (int i = 0; i < fields.Length; i++)
{
var field = fields[i];
SetValue(field, record, item[i]);
}
result.Add(record);
}

return result;
}

private static void SetValue<T>(MemberInfo field, T record, string value)
{
if (field is FieldInfo)
{
var fi = (FieldInfo)field;
fi.SetValue(record, Convert.ChangeType(value, fi.FieldType));
}
else if (field is PropertyInfo)
{
var pi = (PropertyInfo)field;
if (pi.PropertyType.IsEnum)
pi.SetValue(record, Enum.Parse(pi.PropertyType, value));
else
pi.SetValue(record, Convert.ChangeType(value, pi.PropertyType));
}
else
{
throw new Exception("Unhandled case.");
}
}

}

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class ColumnOrderAttribute : Attribute
{
public int Order { get; private set; }
public ColumnOrderAttribute(int order) { Order = order; }
}
}

22 changes: 22 additions & 0 deletions Win32Client/Helpers/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Win32Client.Helpers
{
public static class StringExtensions
{
/// <summary>
/// The Mac Results are in MacRoman encoding, return it back to UTF
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string MacRomanToString(this byte[] source)
{
return System.Text.Encoding.GetEncoding(10000).GetString(source);
}

}
}
37 changes: 37 additions & 0 deletions Win32Client/Models/ConversionStatusModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Win32Client.Helpers;

namespace Win32Client.Models
{
public class ConversionStatusModel
{
[ColumnOrder(1)]
public string VideoId { get; set; }

[ColumnOrder(2)]
public StatusCodes Status { get; set; }

[ColumnOrder(3)]
public string Error { get; set; }

/// <summary>
/// Duration in seconds
/// </summary>
[ColumnOrder(4)]
public int Duration { get; set; }

[ColumnOrder(5)]
public int Progress { get; set; }

public enum StatusCodes
{
Error,
Encoding,
ReadyForDownload
}
}
}
28 changes: 28 additions & 0 deletions Win32Client/Models/SearchResultModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Win32Client.Helpers;

namespace Win32Client.Models
{
public class SearchResultModel
{

[ColumnOrderAttribute(1)]
public string VideoId { get; set; }

[ColumnOrderAttribute(2)]
public string Title { get; set; }

[ColumnOrderAttribute(3)]
public string Description { get; set; }

[ColumnOrderAttribute(4)]
public string Url { get; set; }

[ColumnOrderAttribute(5)]
public string ThumbUrl { get; set; }
}
}
22 changes: 22 additions & 0 deletions Win32Client/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Win32Client
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Search());
}
}
}
Loading

0 comments on commit 515fe24

Please sign in to comment.