Skip to content

Commit

Permalink
Added async methods for download and processing
Browse files Browse the repository at this point in the history
  • Loading branch information
morpheusxx committed Jun 19, 2014
1 parent 6783cca commit c426231
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
29 changes: 22 additions & 7 deletions Source/LogoManager.Tester/Program.cs
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MediaPortal.LogoManager.Effects;

namespace MediaPortal.LogoManager.Tester
Expand Down Expand Up @@ -29,7 +31,16 @@ static void Main(string[] args)
ProcessFile(processor, design, "1_plus_1_International", effects);

// Test 2: From repository
ProcessStream(processor, design, "zdf", effects);
using (var repo = new LogoRepository { RepositoryUrl = REPOSITORY_URL })
{
// Parallel async processing
var results = repo.Download(new[] { "zdf hd", "animal planet hd", "discovery channel" });
Task.WaitAll(results.Select(channelAndStream => ProcessStream(processor, channelAndStream.Value, design, channelAndStream.Key, effects)).ToArray());

// Synchronous processing
var stream = repo.Download("zdf");
ProcessStream(processor, stream, design, "zdf", effects);
}
}

static void ProcessFile(LogoProcessor processor, string design, string channelName, List<AbstractEffect> effects)
Expand All @@ -40,15 +51,19 @@ static void ProcessFile(LogoProcessor processor, string design, string channelNa
processor.CreateLogo(design, logoFile, saveFileName, effects);
}

static void ProcessStream(LogoProcessor processor, string design, string channelName, List<AbstractEffect> effects)
static void ProcessStream(LogoProcessor processor, Stream logo, string design, string channelName, List<AbstractEffect> effects)
{
string saveFileName = string.Format("{0}_{1}.png", design, channelName); // Processed file

using (var repo = new LogoRepository { RepositoryUrl = REPOSITORY_URL })
using (Stream logo = repo.Download(channelName).Result)
{
using (logo)
processor.CreateLogo(design, logo, saveFileName, effects);
}
}

static async Task ProcessStream(LogoProcessor processor, Task<Stream> logo, string design, string channelName, List<AbstractEffect> effects)
{
string saveFileName = string.Format("{0}_{1}.png", design, channelName); // Processed file
Stream logoStream = await logo;
using (logoStream)
processor.CreateLogo(design, logoStream, saveFileName, effects);
}
}
}
36 changes: 31 additions & 5 deletions Source/LogoManager/LogoRepository.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
Expand All @@ -10,16 +11,41 @@ namespace MediaPortal.LogoManager
{
public class LogoRepository : IDisposable
{
private HttpClient _httpClient;
private readonly HttpClient _httpClient;

public LogoRepository()
{
_httpClient = new HttpClient();
}

public string RepositoryUrl { get; set; }

public async Task<Stream> Download(string channelName)
public Stream Download(string channelName)
{
var res = Download(new[] { channelName });
return res.ContainsKey(channelName) ? res.Values.First().Result : null;
}

public Dictionary<string, Task<Stream>> Download(string[] channelName)
{
var logoUrls = Lookup(channelName);
return logoUrls.Result.ToDictionary(logoNameUrl => logoNameUrl.Key, logoNameUrl => DownloadLogoAsync(logoNameUrl.Value));
}

public Task<Dictionary<string, string>> Lookup(string channelName)
{
return Lookup(new[] { channelName });
}

public async Task<Dictionary<string, string>> Lookup(string[] channelNames)
{
ChannelManagerClient client = new ChannelManagerClient(new BasicHttpBinding(), new EndpointAddress(string.Format("{0}ChannelManager.svc", RepositoryUrl)));
var logoUrls = client.GetLogos(new[] { channelName }, string.Empty);
_httpClient = new HttpClient();
return await _httpClient.GetStreamAsync(string.Format("{0}Logos/{1}.png", RepositoryUrl, logoUrls.Values.FirstOrDefault()));
return await client.GetLogosAsync(channelNames, string.Empty);
}

private async Task<Stream> DownloadLogoAsync(string logoId)
{
return await _httpClient.GetStreamAsync(string.Format("{0}Logos/{1}.png", RepositoryUrl, logoId));
}

public void Dispose()
Expand Down

0 comments on commit c426231

Please sign in to comment.