Refactor.
This commit is contained in:
146
Program.cs
146
Program.cs
@@ -13,54 +13,133 @@ namespace IPMonitor
|
||||
{
|
||||
class Program
|
||||
{
|
||||
//public static String GetPublicIPAddress()
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// String address = "";
|
||||
// WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
|
||||
// using (WebResponse response = request.GetResponse())
|
||||
// using (StreamReader stream = new StreamReader(response.GetResponseStream()))
|
||||
// {
|
||||
// address = stream.ReadToEnd();
|
||||
// }
|
||||
|
||||
// int first = address.IndexOf("Address: ") + 9;
|
||||
// int last = address.LastIndexOf("</body>");
|
||||
// address = address.Substring(first, last - first);
|
||||
|
||||
// return address;
|
||||
// }
|
||||
// catch(Exception exception)
|
||||
// {
|
||||
// String message = String.Format("[IPMonitor::GetPublicIPAddress] Exception:{0}",exception.ToString());
|
||||
// MDTrace.WriteLine(LogLevel.DEBUG,message);
|
||||
// SendSMSEmail("IPMonitor encountered an issue retrieving.");
|
||||
// return null;
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
public static String GetPublicIPAddress()
|
||||
{
|
||||
String address = "";
|
||||
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
|
||||
using (WebResponse response = request.GetResponse())
|
||||
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
|
||||
{
|
||||
address = stream.ReadToEnd();
|
||||
}
|
||||
int MAX_RETRIES=5;
|
||||
int TIMEOUT_BETWEEN_ATTEMPTS=30000;
|
||||
try
|
||||
{
|
||||
String address = null;
|
||||
String request="http://checkip.dyndns.org/";
|
||||
|
||||
for(int index=0;index<MAX_RETRIES && null==address;index++)
|
||||
{
|
||||
address = GetHttpRequest(request);
|
||||
if(null==address)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor::GetPublicIPAddress] Request failed {0}. Will retry after {1} (ms) ",request,TIMEOUT_BETWEEN_ATTEMPTS));
|
||||
try{Thread.Sleep(TIMEOUT_BETWEEN_ATTEMPTS);}catch{;}
|
||||
}
|
||||
}
|
||||
int first = address.IndexOf("Address: ") + 9;
|
||||
int last = address.LastIndexOf("</body>");
|
||||
address = address.Substring(first, last - first);
|
||||
|
||||
int first = address.IndexOf("Address: ") + 9;
|
||||
int last = address.LastIndexOf("</body>");
|
||||
address = address.Substring(first, last - first);
|
||||
|
||||
return address;
|
||||
return address;
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
String message = String.Format("[IPMonitor::GetPublicIPAddress] Exception:{0}",exception.ToString());
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,message);
|
||||
SendSMSEmail("IPMonitor encountered an issue retrieving.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static String GetHttpRequest(String strRequest)
|
||||
{
|
||||
try
|
||||
{
|
||||
WebRequest request = WebRequest.Create(strRequest);
|
||||
using (WebResponse response = request.GetResponse())
|
||||
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
|
||||
{
|
||||
return stream.ReadToEnd();
|
||||
}
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor:GetHttpRequest] failed with {0}",exception.ToString()));
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void UpdateIPAddress(String ipAddress)
|
||||
{
|
||||
|
||||
|
||||
String strPathFileName="ipaddress.txt";
|
||||
if(!File.Exists(strPathFileName))
|
||||
try
|
||||
{
|
||||
WriteFile(strPathFileName, ipAddress);
|
||||
SendSMSEmail(String.Format("IPMonitor IPAddress {0}",ipAddress));
|
||||
}
|
||||
else
|
||||
{
|
||||
String currentIPAddress=ReadFile(strPathFileName);
|
||||
if(null!=currentIPAddress && !currentIPAddress.Equals(ipAddress))
|
||||
String strPathFileName="ipaddress.txt";
|
||||
if(!File.Exists(strPathFileName))
|
||||
{
|
||||
WriteFile(strPathFileName,ipAddress);
|
||||
WriteFile(strPathFileName, ipAddress);
|
||||
SendSMSEmail(String.Format("IPMonitor IPAddress {0}",ipAddress));
|
||||
}
|
||||
else
|
||||
{
|
||||
String currentIPAddress=ReadFile(strPathFileName);
|
||||
if(null!=currentIPAddress && !currentIPAddress.Equals(ipAddress))
|
||||
{
|
||||
WriteFile(strPathFileName,ipAddress);
|
||||
SendSMSEmail(String.Format("IPMonitor IPAddress {0}",ipAddress));
|
||||
}
|
||||
else if(null==currentIPAddress)
|
||||
{
|
||||
SendSMSEmail("IPMonitor "+ipAddress+ ". IPMonitor encountered an issue reading the IPAddress file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor::UpdateIPAddress] Exception:{0}",exception.ToString()));
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteFile(String strPathFileName,String ipAddress)
|
||||
{
|
||||
if(File.Exists(strPathFileName))File.Delete(strPathFileName);
|
||||
FileStream fileStream=new FileStream(strPathFileName,FileMode.Create,FileAccess.Write,FileShare.Read);
|
||||
StreamWriter streamWriter=new StreamWriter(fileStream);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Creating address file:{0}",strPathFileName));
|
||||
streamWriter.WriteLine(ipAddress);
|
||||
streamWriter.Flush();
|
||||
streamWriter.Close();
|
||||
streamWriter.Dispose();
|
||||
try
|
||||
{
|
||||
if(File.Exists(strPathFileName))File.Delete(strPathFileName);
|
||||
FileStream fileStream=new FileStream(strPathFileName,FileMode.Create,FileAccess.Write,FileShare.Read);
|
||||
StreamWriter streamWriter=new StreamWriter(fileStream);
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Creating address file:{0}",strPathFileName));
|
||||
streamWriter.WriteLine(ipAddress);
|
||||
streamWriter.Flush();
|
||||
streamWriter.Close();
|
||||
streamWriter.Dispose();
|
||||
}
|
||||
catch(Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor::WriteFile] Exception:{0}",exception.ToString()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public static String ReadFile(String strPathFileName)
|
||||
@@ -106,16 +185,17 @@ namespace IPMonitor
|
||||
|
||||
Profiler profiler=new Profiler();
|
||||
profiler.Start();
|
||||
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[IPMonitor::Start] {0}",Directory.GetCurrentDirectory()));
|
||||
while(true)
|
||||
{
|
||||
String ipAddress=GetPublicIPAddress();
|
||||
UpdateIPAddress(ipAddress);
|
||||
try{Thread.Sleep(interval_ms);}catch{;}
|
||||
if(profiler.Split()>=notify_ms)
|
||||
{
|
||||
SendSMSEmail(String.Format("IPMonitor IPAddress {0}",ipAddress));
|
||||
profiler.Start();
|
||||
}
|
||||
try{Thread.Sleep(interval_ms);}catch{;}
|
||||
}
|
||||
}
|
||||
catch(Exception exception)
|
||||
|
||||
Reference in New Issue
Block a user