-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPingMonitor.java
194 lines (168 loc) · 4.01 KB
/
PingMonitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class PingMonitor{
public static void main(String[] args) {
PingWindow window = new PingWindow("Ping Monitor");
window.setVisible(true);
window.setSize(400,400);
}
}
class PingFetcher implements Runnable{
private int ping;
private Thread t;
private String site;
public int getPing(String site) {
this.site = site;
t = new Thread(this, "PingFetcher Thread");
t.start();
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
System.out.println("Thread Interrupted");
}
// t.stop(); //stop searching for ping if time exceeds 1000ms for system
return ping;
}
public void run() {
try {
Process p = Runtime.getRuntime().exec("ping " + site + " -n 1");
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String s = "";
// reading output stream of the command
while ((s = inputStream.readLine()) != null) {
if(s.indexOf("could not find host")!=-1 || s.indexOf("timed out")!=-1){
ping = -1;
}
int start = s.indexOf("time=");
if(start!=-1){
int end = s.indexOf("ms");
s = s.substring(start+5,end);
ping = Integer.parseInt(s);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
class PingWindow extends Frame{
private int ping;
private int averagePing;
private int pingData[];
private int high=200, mid=100, low=0;
private PingFetcher pinger;
public PingWindow(String title){
super(title);
pinger = new PingFetcher();
pingData = new int[20];
for(int i = 0; i < 20; i++){
pingData[i] = -2;
}
// pingData[0] = 0;
MyTimerTask myTask = new MyTimerTask();
Timer myTimer = new Timer();
myTimer.schedule(myTask, 1000, 1000);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
myTimer.cancel();
System.exit(0);
}
});
}
public void paint(Graphics g){
g.fillRect(40,55,342,205);
int oldLineX = 40, oldLineY;
if(pingData[0]==-1){
oldLineY = 60;
}
else{
oldLineY = 260-pingData[0];
}
for(int i = 0; i < 20; i++){
if(pingData[i]==-2){
continue;
}
if(pingData[i]==-1){
g.setColor(Color.red);
g.drawLine(oldLineX, oldLineY, oldLineX=40+i*18, oldLineY=60);
continue;
}
g.setColor(Color.green);
g.drawLine(oldLineX, oldLineY, oldLineX=40+i*18, oldLineY=260-pingData[i]);
}
g.setColor(Color.black);
g.setFont(new Font("Arial",Font.PLAIN,10));
g.drawString(Integer.toString(high),20,70);
g.drawString(Integer.toString(mid),20,165);
g.drawString(Integer.toString(low),30,255);
//Ping : 20 | Average Ping : 23
int loss = getLossPercentage();
g.setFont(new Font("Arial",Font.PLAIN,15));
g.drawString("Ping : "+ping+"ms",20,290);
g.drawString("Average Ping : "+getAveragePing()+"ms",20,320);
g.drawString("Loss Percentage : "+loss+"%",20,350);
g.drawString("NetHealth",265,290);
if(loss<=5){
g.setColor(Color.green);
}
else if(loss<=20){
g.setColor(Color.yellow);
}
else if(loss<30){
g.setColor(Color.orange);
}
else{
g.setColor(Color.red);
}
g.fillRect(280,300,30,30);
}
public int getAveragePing(){
int totalPing = 0;
int noOfPing = 20;
for(int i = 0; i < 20; i++){
if(pingData[i] == -1 || pingData[i] == -2){
noOfPing--;
continue;
}
totalPing += pingData[i];
}
if(noOfPing==0){
return -1;
}
return totalPing/noOfPing;
}
public int getLossPercentage(){
int totalPing = 20;
int lossPing = 0;
for(int i = 0; i < 20; i++){
if(pingData[i] == -2){
totalPing--;
continue;
}
if(pingData[i] == -1){
lossPing++;
continue;
}
}
if(totalPing==0){
return -1;
}
return lossPing*100/totalPing;
}
class MyTimerTask extends TimerTask {
public void run() {
ping = pinger.getPing("google.com");
for(int i = 19; i > 0; i--){
pingData[i] = pingData[i-1];
}
pingData[0] = ping;
repaint();
}
}
}