1
1
package com .stealthcopter .networktools ;
2
2
3
- import android .support .annotation .NonNull ;
4
-
5
3
import com .stealthcopter .networktools .ping .PingResult ;
6
4
import com .stealthcopter .networktools .ping .PingStats ;
7
5
import com .stealthcopter .networktools .ping .PingTools ;
@@ -18,9 +16,11 @@ public class Ping {
18
16
private Ping () {
19
17
}
20
18
21
- public interface PingListener {
19
+ public interface PingListener {
22
20
void onResult (PingResult pingResult );
21
+
23
22
void onFinished (PingStats pingStats );
23
+
24
24
void onError (Exception e );
25
25
}
26
26
@@ -33,59 +33,64 @@ public interface PingListener{
33
33
34
34
/**
35
35
* Set the address to ping
36
- *
36
+ * <p>
37
37
* Note that a lookup is not performed here so that we do not accidentally perform a network
38
38
* request on the UI thread.
39
39
*
40
40
* @param address - Address to be pinged
41
41
* @return this object to allow chaining
42
42
*/
43
- public static Ping onAddress (@ NonNull String address ) {
43
+ public static Ping onAddress (String address ) {
44
44
Ping ping = new Ping ();
45
45
ping .setAddressString (address );
46
46
return ping ;
47
47
}
48
48
49
49
/**
50
50
* Set the address to ping
51
+ *
51
52
* @param ia - Address to be pinged
52
53
* @return this object to allow chaining
53
54
*/
54
- public static Ping onAddress (@ NonNull InetAddress ia ) {
55
+ public static Ping onAddress (InetAddress ia ) {
55
56
Ping ping = new Ping ();
56
57
ping .setAddress (ia );
57
58
return ping ;
58
59
}
59
60
60
61
/**
61
62
* Set the timeout
63
+ *
62
64
* @param timeOutMillis - the timeout for each ping in milliseconds
63
65
* @return this object to allow chaining
64
66
*/
65
- public Ping setTimeOutMillis (int timeOutMillis ){
66
- if (timeOutMillis < 0 ) throw new IllegalArgumentException ("Times cannot be less than 0" );
67
+ public Ping setTimeOutMillis (int timeOutMillis ) {
68
+ if (timeOutMillis < 0 ) throw new IllegalArgumentException ("Times cannot be less than 0" );
67
69
this .timeOutMillis = timeOutMillis ;
68
70
return this ;
69
71
}
70
72
71
73
/**
72
74
* Set the delay between each ping
75
+ *
73
76
* @param delayBetweenScansMillis - the timeout for each ping in milliseconds
74
77
* @return this object to allow chaining
75
78
*/
76
- public Ping setDelayMillis (int delayBetweenScansMillis ){
77
- if (delayBetweenScansMillis <0 ) throw new IllegalArgumentException ("Delay cannot be less than 0" );
79
+ public Ping setDelayMillis (int delayBetweenScansMillis ) {
80
+ if (delayBetweenScansMillis < 0 )
81
+ throw new IllegalArgumentException ("Delay cannot be less than 0" );
78
82
this .delayBetweenScansMillis = delayBetweenScansMillis ;
79
83
return this ;
80
84
}
81
85
82
86
/**
83
87
* Set number of times to ping the address
88
+ *
84
89
* @param noTimes - number of times, 0 = continuous
85
90
* @return this object to allow chaining
86
91
*/
87
- public Ping setTimes (int noTimes ){
88
- if (noTimes < 0 ) throw new IllegalArgumentException ("Times cannot be less than 0" );
92
+ public Ping setTimes (int noTimes ) {
93
+ if (noTimes < 0 ) throw new IllegalArgumentException ("Times cannot be less than 0" );
89
94
this .times = noTimes ;
90
95
return this ;
91
96
}
@@ -96,6 +101,7 @@ private void setAddress(InetAddress address) {
96
101
97
102
/**
98
103
* Set the address string which will be resolved to an address by resolveAddressString()
104
+ *
99
105
* @param addressString - String of the address to be pinged
100
106
*/
101
107
private void setAddressString (String addressString ) {
@@ -108,7 +114,7 @@ private void setAddressString(String addressString) {
108
114
* @throws UnknownHostException - if host cannot be found
109
115
*/
110
116
private void resolveAddressString () throws UnknownHostException {
111
- if (address == null && addressString !=null ){
117
+ if (address == null && addressString != null ) {
112
118
address = InetAddress .getByName (addressString );
113
119
}
114
120
}
@@ -122,9 +128,10 @@ public void cancel() {
122
128
123
129
/**
124
130
* Perform a synchronous ping and return a result, will ignore number of times.
125
- *
131
+ * <p>
126
132
* Note that this should be performed on a background thread as it will perform a network
127
133
* request
134
+ *
128
135
* @return - ping result
129
136
*/
130
137
public PingResult doPing () throws UnknownHostException {
@@ -135,10 +142,11 @@ public PingResult doPing() throws UnknownHostException {
135
142
136
143
/**
137
144
* Perform an asynchronous ping
145
+ *
138
146
* @param pingListener - the listener to fire PingResults to.
139
147
* @return - this so we can cancel if needed
140
148
*/
141
- public Ping doPing (final PingListener pingListener ){
149
+ public Ping doPing (final PingListener pingListener ) {
142
150
143
151
new Thread (new Runnable () {
144
152
@ Override
@@ -151,7 +159,7 @@ public void run() {
151
159
return ;
152
160
}
153
161
154
- if (address == null ){
162
+ if (address == null ) {
155
163
pingListener .onError (new NullPointerException ("Address is null" ));
156
164
return ;
157
165
}
@@ -166,24 +174,23 @@ public void run() {
166
174
int noPings = times ;
167
175
168
176
// times == 0 is the case that we can continuous scanning
169
- while (noPings > 0 || times == 0 ){
177
+ while (noPings > 0 || times == 0 ) {
170
178
PingResult pingResult = PingTools .doPing (address , timeOutMillis );
171
179
172
- if (pingListener != null ){
180
+ if (pingListener != null ) {
173
181
pingListener .onResult (pingResult );
174
182
}
175
183
176
184
// Update ping stats
177
185
pingsCompleted ++;
178
186
179
- if (pingResult .hasError ()){
187
+ if (pingResult .hasError ()) {
180
188
noLostPackets ++;
181
- }
182
- else {
189
+ } else {
183
190
float timeTaken = pingResult .getTimeTaken ();
184
191
totalPingTime += timeTaken ;
185
- if (maxPingTime == - 1 || timeTaken > maxPingTime ) maxPingTime = timeTaken ;
186
- if (minPingTime == - 1 || timeTaken < minPingTime ) minPingTime = timeTaken ;
192
+ if (maxPingTime == -1 || timeTaken > maxPingTime ) maxPingTime = timeTaken ;
193
+ if (minPingTime == -1 || timeTaken < minPingTime ) minPingTime = timeTaken ;
187
194
}
188
195
189
196
noPings --;
@@ -196,7 +203,7 @@ public void run() {
196
203
}
197
204
}
198
205
199
- if (pingListener != null ){
206
+ if (pingListener != null ) {
200
207
pingListener .onFinished (new PingStats (address , pingsCompleted , noLostPackets , totalPingTime , minPingTime , maxPingTime ));
201
208
}
202
209
}
0 commit comments