ln.skyscanner/crawl/service/ICMP.cs

71 lines
2.2 KiB
C#
Raw Normal View History

2019-03-26 12:53:42 +01:00
// /**
// * File: ICMP.cs
// * Author: haraldwolff
// *
// * This file and it's content is copyrighted by the Author and / or copyright holder.
// * Any use wihtout proper permission is illegal and may lead to legal actions.
// *
// *
// **/
using System;
using System.Net.NetworkInformation;
using ln.skyscanner.crawl.service;
namespace ln.skyscanner.crawl.tests
{
public class ICMP : CrawlService
{
2019-04-17 18:00:58 +02:00
public static Ping Ping { get; private set; } = new Ping();
2019-04-11 13:05:07 +02:00
2019-03-26 12:53:42 +01:00
public ICMP()
:base("ping")
{}
public override bool Check(Crawl crawl)
{
int nSuccess = 0;
long roundTripTime = 0;
for (int n = 0; n < 10; n++)
{
//HostCrawl.setState("ICMP check [{0}/10]", n);
2019-04-11 13:05:07 +02:00
PingReply pingReply = Ping.Send(crawl.Host.PrimaryIP, 500);
2019-03-26 12:53:42 +01:00
if (pingReply.Status == IPStatus.Success)
{
nSuccess++;
roundTripTime += pingReply.RoundtripTime;
}
else if ((n > 3) && (nSuccess == 0))
{
break;
}
}
if (nSuccess > 0)
{
roundTripTime /= nSuccess;
crawl.Host.SetHint("ping.success", true);
crawl.Host.SetHint("ping.rta", (int)roundTripTime);
crawl.Host.SetHint("ping.out_of_ten", nSuccess);
crawl.Host.LastSeen = DateTime.Now;
2019-04-04 19:34:19 +02:00
if (crawl.Host.FirstSeen < new DateTime(1970, 1, 2))
crawl.Host.FirstSeen = DateTime.Now;
2019-03-26 12:53:42 +01:00
}
else
{
crawl.Host.SetHint("ping.success", false);
crawl.Host.SetHint("ping.rta", null);
crawl.Host.SetHint("ping.out_of_ten", 0);
}
return true;
}
public override bool HostProvidesOption(Crawl crawl, params object[] parameters)
{
return crawl.Host.GetHint("ping.success", false);
}
}
}