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 ;
14
12
*/
15
13
public class Ping {
16
14
15
+ // Only try ping using the java method
16
+ public static final int PING_JAVA = 0 ;
17
+
18
+ // Only try ping using the native method (will only work if native ping binary is found)
19
+ public static final int PING_NATIVE = 1 ;
20
+
21
+ // Use a hybrid ping that will attempt to use native binary but fallback to using java method
22
+ // if it's not found.
23
+ public static final int PING_HYBRID = 2 ;
24
+
17
25
// This class is not to be instantiated
18
26
private Ping () {
19
27
}
20
28
21
- public interface PingListener {
29
+ public interface PingListener {
22
30
void onResult (PingResult pingResult );
23
31
void onFinished (PingStats pingStats );
24
32
void onError (Exception e );
@@ -40,52 +48,57 @@ public interface PingListener{
40
48
* @param address - Address to be pinged
41
49
* @return this object to allow chaining
42
50
*/
43
- public static Ping onAddress (@ NonNull String address ) {
51
+ public static Ping onAddress (String address ) {
44
52
Ping ping = new Ping ();
45
53
ping .setAddressString (address );
46
54
return ping ;
47
55
}
48
56
49
57
/**
50
58
* Set the address to ping
59
+ *
51
60
* @param ia - Address to be pinged
52
61
* @return this object to allow chaining
53
62
*/
54
- public static Ping onAddress (@ NonNull InetAddress ia ) {
63
+ public static Ping onAddress (InetAddress ia ) {
55
64
Ping ping = new Ping ();
56
65
ping .setAddress (ia );
57
66
return ping ;
58
67
}
59
68
60
69
/**
61
70
* Set the timeout
71
+ *
62
72
* @param timeOutMillis - the timeout for each ping in milliseconds
63
73
* @return this object to allow chaining
64
74
*/
65
- public Ping setTimeOutMillis (int timeOutMillis ){
66
- if (timeOutMillis < 0 ) throw new IllegalArgumentException ("Times cannot be less than 0" );
75
+ public Ping setTimeOutMillis (int timeOutMillis ) {
76
+ if (timeOutMillis < 0 ) throw new IllegalArgumentException ("Times cannot be less than 0" );
67
77
this .timeOutMillis = timeOutMillis ;
68
78
return this ;
69
79
}
70
80
71
81
/**
72
82
* Set the delay between each ping
83
+ *
73
84
* @param delayBetweenScansMillis - the timeout for each ping in milliseconds
74
85
* @return this object to allow chaining
75
86
*/
76
- public Ping setDelayMillis (int delayBetweenScansMillis ){
77
- if (delayBetweenScansMillis <0 ) throw new IllegalArgumentException ("Delay cannot be less than 0" );
87
+ public Ping setDelayMillis (int delayBetweenScansMillis ) {
88
+ if (delayBetweenScansMillis < 0 )
89
+ throw new IllegalArgumentException ("Delay cannot be less than 0" );
78
90
this .delayBetweenScansMillis = delayBetweenScansMillis ;
79
91
return this ;
80
92
}
81
93
82
94
/**
83
95
* Set number of times to ping the address
96
+ *
84
97
* @param noTimes - number of times, 0 = continuous
85
98
* @return this object to allow chaining
86
99
*/
87
- public Ping setTimes (int noTimes ){
88
- if (noTimes < 0 ) throw new IllegalArgumentException ("Times cannot be less than 0" );
100
+ public Ping setTimes (int noTimes ) {
101
+ if (noTimes < 0 ) throw new IllegalArgumentException ("Times cannot be less than 0" );
89
102
this .times = noTimes ;
90
103
return this ;
91
104
}
@@ -96,6 +109,7 @@ private void setAddress(InetAddress address) {
96
109
97
110
/**
98
111
* Set the address string which will be resolved to an address by resolveAddressString()
112
+ *
99
113
* @param addressString - String of the address to be pinged
100
114
*/
101
115
private void setAddressString (String addressString ) {
@@ -108,7 +122,7 @@ private void setAddressString(String addressString) {
108
122
* @throws UnknownHostException - if host cannot be found
109
123
*/
110
124
private void resolveAddressString () throws UnknownHostException {
111
- if (address == null && addressString !=null ){
125
+ if (address == null && addressString != null ) {
112
126
address = InetAddress .getByName (addressString );
113
127
}
114
128
}
@@ -125,6 +139,7 @@ public void cancel() {
125
139
*
126
140
* Note that this should be performed on a background thread as it will perform a network
127
141
* request
142
+ *
128
143
* @return - ping result
129
144
*/
130
145
public PingResult doPing () throws UnknownHostException {
@@ -135,10 +150,11 @@ public PingResult doPing() throws UnknownHostException {
135
150
136
151
/**
137
152
* Perform an asynchronous ping
153
+ *
138
154
* @param pingListener - the listener to fire PingResults to.
139
155
* @return - this so we can cancel if needed
140
156
*/
141
- public Ping doPing (final PingListener pingListener ){
157
+ public Ping doPing (final PingListener pingListener ) {
142
158
143
159
new Thread (new Runnable () {
144
160
@ Override
@@ -151,7 +167,7 @@ public void run() {
151
167
return ;
152
168
}
153
169
154
- if (address == null ){
170
+ if (address == null ) {
155
171
pingListener .onError (new NullPointerException ("Address is null" ));
156
172
return ;
157
173
}
@@ -166,24 +182,23 @@ public void run() {
166
182
int noPings = times ;
167
183
168
184
// times == 0 is the case that we can continuous scanning
169
- while (noPings > 0 || times == 0 ){
185
+ while (noPings > 0 || times == 0 ) {
170
186
PingResult pingResult = PingTools .doPing (address , timeOutMillis );
171
187
172
- if (pingListener != null ){
188
+ if (pingListener != null ) {
173
189
pingListener .onResult (pingResult );
174
190
}
175
191
176
192
// Update ping stats
177
193
pingsCompleted ++;
178
194
179
- if (pingResult .hasError ()){
195
+ if (pingResult .hasError ()) {
180
196
noLostPackets ++;
181
- }
182
- else {
197
+ } else {
183
198
float timeTaken = pingResult .getTimeTaken ();
184
199
totalPingTime += timeTaken ;
185
- if (maxPingTime == - 1 || timeTaken > maxPingTime ) maxPingTime = timeTaken ;
186
- if (minPingTime == - 1 || timeTaken < minPingTime ) minPingTime = timeTaken ;
200
+ if (maxPingTime == -1 || timeTaken > maxPingTime ) maxPingTime = timeTaken ;
201
+ if (minPingTime == -1 || timeTaken < minPingTime ) minPingTime = timeTaken ;
187
202
}
188
203
189
204
noPings --;
@@ -196,12 +211,12 @@ public void run() {
196
211
}
197
212
}
198
213
199
- if (pingListener != null ){
214
+ if (pingListener != null ) {
200
215
pingListener .onFinished (new PingStats (address , pingsCompleted , noLostPackets , totalPingTime , minPingTime , maxPingTime ));
201
216
}
202
217
}
203
218
}).start ();
204
219
return this ;
205
220
}
206
221
207
- }
222
+ }
0 commit comments