Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Shtirlitz.Reporter.PingReporter
{
public interface IPingReporterProvider
{
bool Next();
PingResult Current { get; }
double Weight { get; }
}
}
79 changes: 79 additions & 0 deletions Shtirlitz.Library/Reporter/PingReporter/PingProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System.Net.NetworkInformation;

namespace Shtirlitz.Reporter.PingReporter
{
public class PingProvider : IPingReporterProvider
{
public PingResult Current { get; private set; }
public double Weight
{
get
{
var result = 1;
if (_count > 0)
result = 100 * _currCount / _count;
return result;
}
}

public PingProvider(string host, int count)
{
_host = host;
_count = count;
_currCount = count;
}

public bool Next()
{
var result = false;

if (_currCount > 0)
{
result = SendPing();
_currCount--;
}

return result;
}

private bool SendPing()
{
var result = false;

using (var ping = new Ping())
{
var pingResult = ping.Send(_host);
_send++;
if (pingResult != null)
{
if (pingResult.Status == IPStatus.Success)
_receive++;

Current = new PingResult
{
Host = _host,
Send = _send,
Receive = _receive,
Lost = _lost,
RoundtripTime = pingResult.RoundtripTime
};

result = true;
}
else
{
_lost++;
}
}
return result;
}

private readonly string _host;
private readonly int _count;
private int _currCount;

private int _send;
private int _receive;
private int _lost;
}
}
33 changes: 33 additions & 0 deletions Shtirlitz.Library/Reporter/PingReporter/PingReporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.IO;
using System.Threading;
using Shtirlitz.Common;

namespace Shtirlitz.Reporter.PingReporter
{
public class PingReport : IReporter
{
public double Weight => _provider.Weight;
public string Name => "Ping/Trace reporter";

public PingReport(IPingReporterProvider provider)
{
_provider = provider;
}

public void Report(string rootPath, CancellationToken cancellationToken, SimpleProgressCallback progressCallback = null)
{
while (_provider.Next())
{
if (cancellationToken.IsCancellationRequested)
break;

var message = $"Host {_provider.Current.Host} Send {_provider.Current.Send} Receive {_provider.Current.Receive} Lost {_provider.Current.Lost}\r\n";
File.AppendAllText(rootPath, message);

progressCallback?.Invoke(Weight);
}
}

private readonly IPingReporterProvider _provider;
}
}
12 changes: 12 additions & 0 deletions Shtirlitz.Library/Reporter/PingReporter/PingResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

namespace Shtirlitz.Reporter.PingReporter
{
public class PingResult
{
public string Host { get; set; }
public int Send { get; set; }
public int Receive { get; set; }
public int Lost { get; set; }
public long RoundtripTime { get; set; }
}
}
143 changes: 143 additions & 0 deletions Shtirlitz.Library/Reporter/PingReporter/TraceProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;

namespace Shtirlitz.Reporter.PingReporter
{
public class TraceProvider : IPingReporterProvider
{
public PingResult Current { get; private set; }
public double Weight
{
get
{
var result = 1;
if (_count > 0)
result = 100 * _currCount / _count;
return result;
}
}

public TraceProvider(string host, int jumps, int timeout, int count)
{
_host = host;
_jumps = jumps;
_timeout = timeout;
_count = count;
_currCount = count;

_stat = new Dictionary<IPAddress, PingResult>();
_lost = new Dictionary<int, int>();
}

public bool Next()
{
var result = false;

if (_currCount > 0)
{
var isfinal = SendPing();

_currJumps++;
if (_currJumps == _jumps
|| isfinal)
{
_currJumps = 0;
_currCount--;
}

result = true;
}

return result;
}

private readonly string _host;
private readonly int _jumps;
private readonly int _timeout;
private readonly Dictionary<IPAddress, PingResult> _stat;
private readonly Dictionary<int, int> _lost;
private readonly int _count;
private int _currCount;

private int _currJumps;

private bool SendPing()
{
var result = false;

ResetCurrent();
using (var ping = new Ping())
{
var pingResult = ping.Send(_host, _timeout, new byte[0], new PingOptions() {Ttl = _currJumps + 1});
if (pingResult?.Status == IPStatus.Success
|| pingResult?.Status == IPStatus.TtlExpired)
{
var curr = UpdateStat(pingResult);
UpdateCurrent(curr);

result = pingResult.Status == IPStatus.Success;
}
else
{
UpdateLost();
}
}

return result;
}

private PingResult UpdateStat(PingReply pingResult)
{
PingResult curr;
if (!_stat.TryGetValue(pingResult.Address, out curr))
{
curr = new PingResult
{
Host = pingResult.Address.ToString(),
Send = 1,
Receive = 1,
RoundtripTime = pingResult.RoundtripTime
};

_stat[pingResult.Address] = curr;
}
else
{
curr.Send++;
curr.Receive++;
}
return curr;
}

private void UpdateLost()
{
int lost;
if (_lost.TryGetValue(_currJumps, out lost))
_lost[_currJumps] = lost + 1;
else
_lost[_currJumps] = 1;
}

private void ResetCurrent()
{
int lost;
if (_lost.TryGetValue(_currJumps, out lost))
Current = new PingResult { Host = _currJumps.ToString(), Send = lost + 1, Lost = lost + 1 };
else
Current = new PingResult { Host = _currJumps.ToString(), Send = 1, Lost = 1 };
}

private void UpdateCurrent(PingResult source)
{
Current = new PingResult
{
Host = source.Host,
Send = source.Send,
Receive = source.Receive,
Lost = source.Lost,
RoundtripTime = source.RoundtripTime
};
}
}
}
16 changes: 11 additions & 5 deletions Shtirlitz.Library/Shtirlitz.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Ionic.Zip">
<HintPath>$(SolutionDir)packages\DotNetZip.1.9.1.8\lib\net20\Ionic.Zip.dll</HintPath>
<Reference Include="DotNetZip, Version=1.10.1.0, Culture=neutral, PublicKeyToken=6583c7c814667745, processorArchitecture=MSIL">
<HintPath>..\packages\DotNetZip.1.10.1\lib\net20\DotNetZip.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -44,6 +45,11 @@
<ItemGroup>
<Compile Include="Archiver\DotNetZipArchiver.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Reporter\PingReporter\IPingReporterProvider.cs" />
<Compile Include="Reporter\PingReporter\PingProvider.cs" />
<Compile Include="Reporter\PingReporter\PingReporter.cs" />
<Compile Include="Reporter\PingReporter\PingResult.cs" />
<Compile Include="Reporter\PingReporter\TraceProvider.cs" />
<Compile Include="Reporter\ThreadsDumpReporter\ThreadDump.cs" />
<Compile Include="Reporter\ThreadsDumpReporter\ThreadsDumpReporter.cs" />
<Compile Include="Reporter\TimeReporter\CurrentTimeProvider.cs" />
Expand All @@ -53,15 +59,15 @@
<Compile Include="Sender\ArchiveRemover.cs" />
<Compile Include="Sender\ClipboardCopier.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Shtirlitz\Shtirlitz.csproj">
<Project>{813AEDD8-7C91-41F0-A060-F000530168F6}</Project>
<Name>Shtirlitz</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
2 changes: 1 addition & 1 deletion Shtirlitz.Library/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DotNetZip" version="1.9.1.8" targetFramework="net40-Client" />
<package id="DotNetZip" version="1.10.1" targetFramework="net40-client" />
</packages>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using Shtirlitz.Reporter.PingReporter;

namespace Shtirlitz.Tests.Reporter.PingReporter.Common
{
public class PingResultCompareer : IComparer<PingResult>
{
public int Compare(PingResult x, PingResult y)
{
var result = string.Compare(x.Host, y.Host, StringComparison.Ordinal);
if (result == 0)
result = x.Send.CompareTo(y.Send);
if (result == 0)
result = x.Receive.CompareTo(y.Receive);
if (result == 0)
result = x.Lost.CompareTo(y.Lost);

return result;
}
}
}
Loading