Skip to content

Commit

Permalink
Add some logs
Browse files Browse the repository at this point in the history
  • Loading branch information
zokker13 committed Feb 11, 2018
1 parent 258dc35 commit 8b0dbd4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
2 changes: 2 additions & 0 deletions RatherWeird/RatherWeird/OnlinePlayers.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using RatherWeird.Utility;

namespace RatherWeird
{
Expand Down Expand Up @@ -41,6 +42,7 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs
{
e.Cancel = true;
Hide();
Logger.Debug("Closing of playerwindow was supressed");
}
}

Expand Down
45 changes: 35 additions & 10 deletions RatherWeird/RatherWeird/Ra3Playerlist.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
using System.Windows.Shapes;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Security;
using System.Threading;
using Newtonsoft.Json;
using RatherWeird.Utility;

namespace RatherWeird
{
Expand All @@ -40,10 +42,12 @@ public Ra3Playerlist()

public void StartReoccuringTask()
{
Logger.Info("Starting Reoccuring task");
_tokenSource = new CancellationTokenSource();

async void Repeated(Task _)
{
Logger.Debug("Attempting to download and insert player statistics");
CncGeneralInfo info = await GatherData();
InsertData(info);
await Task.Delay(1000 * 60, _tokenSource.Token).ContinueWith(Repeated, _tokenSource.Token);
Expand All @@ -54,24 +58,46 @@ async void Repeated(Task _)

public void StopReoccuringTask()
{
Logger.Info("Stopping Reoccuring task for shutdown");
_tokenSource.Cancel();
}

private async Task<CncGeneralInfo> GatherData()
{
CncGeneralInfo info;
WebRequest req = WebRequest.CreateHttp(Constants.CncOnlinePlayerInfo);
CncGeneralInfo info = new CncGeneralInfo();

var res = await req.GetResponseAsync();
HttpWebResponse response = (HttpWebResponse) res;
try
{
WebRequest req = WebRequest.CreateHttp(Constants.CncOnlinePlayerInfo);

var res = await req.GetResponseAsync();
HttpWebResponse response = (HttpWebResponse) res;

using (Stream streamResponse = response.GetResponseStream())
using (StreamReader sr = new StreamReader(streamResponse))
using (Stream streamResponse = response.GetResponseStream())
using (StreamReader sr =
new StreamReader(streamResponse ?? throw new InvalidOperationException("streamResponse was null")))
{
string body = await sr.ReadToEndAsync();
info = JsonConvert.DeserializeObject<CncGeneralInfo>(body);
}
}
catch (SecurityException err)
{
Logger.Error($"ER.. Could not connect to Server due to permission issues. err: {err.Message}");
}
catch (ProtocolViolationException err)
{
string body = sr.ReadToEnd();
info = JsonConvert.DeserializeObject<CncGeneralInfo>(body);
Logger.Error($"ER.. Could not get any response stream. err: {err.Message}");
}

catch (InvalidOperationException err)
{
Logger.Error($"ER.. Seems like the responsestream was null for some reason. err: {err.Message}");
}
catch (Exception err)
{
Logger.Fatal($"ER.. Seems like something unexpected happened. err: {err.Message}");
}

return info;

}
Expand Down Expand Up @@ -102,7 +128,6 @@ private void InsertData(CncGeneralInfo info)
lblPlayers.Content = $"Players: {_ra3Users.Count}";
}
});

}

public static void AddSorted<T>(IList<T> list, T item)
Expand Down
1 change: 1 addition & 0 deletions RatherWeird/RatherWeird/RatherWeird.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down

0 comments on commit 8b0dbd4

Please sign in to comment.