76 lines
2.4 KiB
C#
76 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using SafeMobileLib;
|
|
using System.Net.NetworkInformation;
|
|
|
|
namespace SDRgateway
|
|
{
|
|
class PingThread
|
|
{
|
|
string ip;
|
|
private int interval;
|
|
bool status;
|
|
private Thread t_Ping;
|
|
/// <summary>
|
|
/// ping class
|
|
/// </summary>
|
|
/// <param name="ip">dest IP</param>
|
|
/// <param name="interval">interval for pings in seconds</param>
|
|
public PingThread(string ip, int interval)
|
|
{
|
|
this.ip = ip;
|
|
this.interval = interval;
|
|
status = false;
|
|
|
|
t_Ping = new Thread(new ThreadStart(Worker));
|
|
t_Ping.IsBackground = true;
|
|
t_Ping.Start();
|
|
}
|
|
|
|
public void Worker()
|
|
{
|
|
SM.Debug("PingThread started for " + ip + " interval:" + interval);
|
|
int count = 0;
|
|
while (true)
|
|
{
|
|
try
|
|
{
|
|
Thread.Sleep(Program.cfg.SDR_ping * 1000);
|
|
DateTime time = DateTime.Now;
|
|
//SM.Debug(ip + " ping sent!");
|
|
Ping pingSender = new Ping();
|
|
PingReply pReply = pingSender.Send(ip);
|
|
if (pReply.Status == IPStatus.Success)
|
|
{
|
|
if (status == false) //going from OFF->ON
|
|
OnNewPingComplete(true, ip);
|
|
status = true;
|
|
}
|
|
else //if (pReply.Status == IPStatus.TimedOut)
|
|
{
|
|
if (status == true) //going from ON->OFF
|
|
OnNewPingComplete(false, ip);
|
|
status = false;
|
|
}
|
|
if (count++ % 10 == 0)
|
|
SM.Debug(String.Format("Gateway {0} is {1}", ip, (status ? "ONLINE" : "OFFLINE")));
|
|
//SM.Debug(ip + " ping delta:" + (DateTime.Now -time) + " status:" + status);
|
|
|
|
|
|
}
|
|
catch
|
|
{
|
|
Console.WriteLine("*** Could not ping: " + ip + " *****");
|
|
}
|
|
Thread.Sleep(interval);
|
|
}
|
|
}
|
|
|
|
public delegate void NewPingCompleteDEl(bool status, string ip);
|
|
public event NewPingCompleteDEl OnNewPingComplete;
|
|
}
|
|
}
|