netmonitor/NetOpenVPNGUI/Runner.cs

87 lines
2.6 KiB
C#
Raw Normal View History

2011-01-27 00:35:09 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Windows.Forms;
namespace NetOpenVPNGUI
{
class Runner
{
private NotifyIcon trayIcon;
private UdpClient net_send;
private IPEndPoint net_send_port;
private UdpClient net_gets;
private IPEndPoint net_gets_port;
public Runner()
{
this.trayIcon = new System.Windows.Forms.NotifyIcon();
this.start();
this.draw_icon(false);
if (!this.check_service("Apache2.2"))
this.run_service("Apache2.2");
else
this.draw_icon(true);
this.wait();
}
private void wait()
{
while (true)
{
System.Threading.Thread.Sleep(100);
}
}
private void draw_icon(bool p)
{
this.trayIcon.Visible = true;
if(p)
this.trayIcon.Icon = Resources.icon_ok;
else
this.trayIcon.Icon = Resources.icon_no;
}
private void run_service(string p)
{
byte[] data = Encoding.UTF8.GetBytes("service start " + p);
this.net_send.Send(data, data.Length, this.net_send_port);
String ret = Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port));
this.trayIcon.BalloonTipIcon = ToolTipIcon.Info;
if (Boolean.Parse(ret))
{
this.trayIcon.BalloonTipText = "Service " + p + " Started";
this.draw_icon(true);
}
else
{
this.trayIcon.BalloonTipText = "Service " + p + " NOT Started";
this.draw_icon(false);
}
this.trayIcon.ShowBalloonTip(200);
}
private bool check_service(string p)
{
byte[] data = Encoding.UTF8.GetBytes("service status " + p);
this.net_send.Send(data, data.Length, this.net_send_port);
int ret = int.Parse(Encoding.UTF8.GetString(net_gets.Receive(ref net_gets_port)));
if (ret == 1)
return true;
return false;
}
private void start()
{
this.net_send = new UdpClient();
this.net_send_port = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 34523);
this.net_gets = new UdpClient(34524);
this.net_gets_port = new IPEndPoint(IPAddress.Loopback, 0);
}
}
}