Adding pipe formatter + some clean up
This commit is contained in:
parent
b5cef59448
commit
7d67629245
|
|
@ -1,7 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SocialWeather
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -8,19 +8,17 @@ namespace SocialWeather
|
|||
{
|
||||
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)
|
||||
{
|
||||
services.AddRouting();
|
||||
services.AddSingleton<SocialWeatherEndPoint>();
|
||||
services.AddTransient<PersistentConnectionLifeTimeManager>();
|
||||
services.AddSingleton(typeof(JsonStreamFormatter<>), typeof(JsonStreamFormatter<>));
|
||||
services.AddSingleton<PipeWeatherStreamFormatter>();
|
||||
services.AddSingleton<ProtobufWeatherStreamFormatter>();
|
||||
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)
|
||||
{
|
||||
loggerFactory.AddConsole();
|
||||
|
|
@ -36,11 +34,7 @@ namespace SocialWeather
|
|||
var formatterResolver = app.ApplicationServices.GetRequiredService<FormatterResolver>();
|
||||
formatterResolver.AddFormatter<WeatherReport, JsonStreamFormatter<WeatherReport>>("json");
|
||||
formatterResolver.AddFormatter<WeatherReport, ProtobufWeatherStreamFormatter>("protobuf");
|
||||
|
||||
app.Run(async (context) =>
|
||||
{
|
||||
await context.Response.WriteAsync("Hello World!");
|
||||
});
|
||||
formatterResolver.AddFormatter<WeatherReport, PipeWeatherStreamFormatter>("pipe");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,6 @@
|
|||
<handlers>
|
||||
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
|
||||
</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>
|
||||
</configuration>
|
||||
|
|
|
|||
|
|
@ -21,7 +21,14 @@
|
|||
</form>
|
||||
<div>
|
||||
<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>
|
||||
<p>Status: <span id="status" /></p>
|
||||
|
|
@ -87,7 +94,6 @@
|
|||
ReportTime: Date.now(),
|
||||
ZipCode: get('zipCode').value
|
||||
}));
|
||||
|
||||
event.preventDefault();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue