Adding pipe formatter + some clean up

This commit is contained in:
Pawel Kadluczka 2016-11-06 23:42:37 -08:00 committed by moozzyk
parent b5cef59448
commit 7d67629245
5 changed files with 75 additions and 15 deletions

View File

@ -1,7 +1,4 @@
using System; using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace SocialWeather namespace SocialWeather

View File

@ -0,0 +1,63 @@
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace SocialWeather
{
public class PipeWeatherStreamFormatter : IStreamFormatter<WeatherReport>
{
public async Task<WeatherReport> ReadAsync(Stream stream)
{
var sr = new StreamReader(stream);
var line = await sr.ReadLineAsync();
if (line == null)
{
return null;
}
var tokens = line.Split('|');
int temperature;
long reportTime = long.MinValue;
Weather weather = (Weather)(-1);
string zipCode = tokens.Length > 3 ? tokens[3] : string.Empty;
if(tokens.Length == 0 || !int.TryParse(tokens[0], out temperature))
{
temperature = int.MinValue;
}
if (tokens.Length < 2 || !long.TryParse(tokens[1], out reportTime))
{
temperature = int.MinValue;
}
if (tokens.Length < 3 || !Enum.TryParse<Weather>(tokens[2], out weather))
{
weather = (Weather)(-1);
}
return new WeatherReport
{
Temperature = temperature,
ReportTime = reportTime,
Weather = weather,
ZipCode = zipCode
};
}
public async Task WriteAsync(WeatherReport report, Stream stream)
{
var sw = new StreamWriter(stream);
var line = $"{report.Temperature}|{report.ReportTime}|{(int)report.Weather}|{report.ZipCode ?? string.Empty}";
Encoding utf8 = Encoding.UTF8;
var encodedBytes = utf8.GetBytes(line);
var convertedBytes = Encoding.Convert(Encoding.UTF8, Encoding.ASCII, encodedBytes);
await sw.WriteLineAsync(Encoding.ASCII.GetString(convertedBytes));
await sw.FlushAsync();
}
}
}

View File

@ -8,19 +8,17 @@ namespace SocialWeather
{ {
public class Startup public class Startup
{ {
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddRouting(); services.AddRouting();
services.AddSingleton<SocialWeatherEndPoint>(); services.AddSingleton<SocialWeatherEndPoint>();
services.AddTransient<PersistentConnectionLifeTimeManager>(); services.AddTransient<PersistentConnectionLifeTimeManager>();
services.AddSingleton(typeof(JsonStreamFormatter<>), typeof(JsonStreamFormatter<>)); services.AddSingleton(typeof(JsonStreamFormatter<>), typeof(JsonStreamFormatter<>));
services.AddSingleton<PipeWeatherStreamFormatter>();
services.AddSingleton<ProtobufWeatherStreamFormatter>(); services.AddSingleton<ProtobufWeatherStreamFormatter>();
services.AddSingleton<FormatterResolver>(); services.AddSingleton<FormatterResolver>();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{ {
loggerFactory.AddConsole(); loggerFactory.AddConsole();
@ -36,11 +34,7 @@ namespace SocialWeather
var formatterResolver = app.ApplicationServices.GetRequiredService<FormatterResolver>(); var formatterResolver = app.ApplicationServices.GetRequiredService<FormatterResolver>();
formatterResolver.AddFormatter<WeatherReport, JsonStreamFormatter<WeatherReport>>("json"); formatterResolver.AddFormatter<WeatherReport, JsonStreamFormatter<WeatherReport>>("json");
formatterResolver.AddFormatter<WeatherReport, ProtobufWeatherStreamFormatter>("protobuf"); formatterResolver.AddFormatter<WeatherReport, ProtobufWeatherStreamFormatter>("protobuf");
formatterResolver.AddFormatter<WeatherReport, PipeWeatherStreamFormatter>("pipe");
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
} }
} }
} }

View File

@ -9,6 +9,6 @@
<handlers> <handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers> </handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/> <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
</system.webServer> </system.webServer>
</configuration> </configuration>

View File

@ -21,7 +21,14 @@
</form> </form>
<div> <div>
<h3>Weather reports</h3> <h3>Weather reports</h3>
<table id="reportsTable"></table> <table id="reportsTable">
<tr>
<th>ZipCode</th>
<th>Temperature</th>
<th>Weather</th>
<th>Updated on:</th>
</tr>
</table>
</div> </div>
<div> <div>
<p>Status: <span id="status" /></p> <p>Status: <span id="status" /></p>
@ -87,7 +94,6 @@
ReportTime: Date.now(), ReportTime: Date.now(),
ZipCode: get('zipCode').value ZipCode: get('zipCode').value
})); }));
event.preventDefault(); event.preventDefault();
}); });
}); });