11package com .stealthcopter .networktools ;
22
3- import android .support .annotation .NonNull ;
4-
53import com .stealthcopter .networktools .ping .PingResult ;
64import com .stealthcopter .networktools .ping .PingStats ;
75import com .stealthcopter .networktools .ping .PingTools ;
1412 */
1513public class Ping {
1614
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+
1725 // This class is not to be instantiated
1826 private Ping () {
1927 }
2028
21- public interface PingListener {
29+ public interface PingListener {
2230 void onResult (PingResult pingResult );
2331 void onFinished (PingStats pingStats );
2432 void onError (Exception e );
@@ -40,52 +48,57 @@ public interface PingListener{
4048 * @param address - Address to be pinged
4149 * @return this object to allow chaining
4250 */
43- public static Ping onAddress (@ NonNull String address ) {
51+ public static Ping onAddress (String address ) {
4452 Ping ping = new Ping ();
4553 ping .setAddressString (address );
4654 return ping ;
4755 }
4856
4957 /**
5058 * Set the address to ping
59+ *
5160 * @param ia - Address to be pinged
5261 * @return this object to allow chaining
5362 */
54- public static Ping onAddress (@ NonNull InetAddress ia ) {
63+ public static Ping onAddress (InetAddress ia ) {
5564 Ping ping = new Ping ();
5665 ping .setAddress (ia );
5766 return ping ;
5867 }
5968
6069 /**
6170 * Set the timeout
71+ *
6272 * @param timeOutMillis - the timeout for each ping in milliseconds
6373 * @return this object to allow chaining
6474 */
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" );
6777 this .timeOutMillis = timeOutMillis ;
6878 return this ;
6979 }
7080
7181 /**
7282 * Set the delay between each ping
83+ *
7384 * @param delayBetweenScansMillis - the timeout for each ping in milliseconds
7485 * @return this object to allow chaining
7586 */
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" );
7890 this .delayBetweenScansMillis = delayBetweenScansMillis ;
7991 return this ;
8092 }
8193
8294 /**
8395 * Set number of times to ping the address
96+ *
8497 * @param noTimes - number of times, 0 = continuous
8598 * @return this object to allow chaining
8699 */
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" );
89102 this .times = noTimes ;
90103 return this ;
91104 }
@@ -96,6 +109,7 @@ private void setAddress(InetAddress address) {
96109
97110 /**
98111 * Set the address string which will be resolved to an address by resolveAddressString()
112+ *
99113 * @param addressString - String of the address to be pinged
100114 */
101115 private void setAddressString (String addressString ) {
@@ -108,7 +122,7 @@ private void setAddressString(String addressString) {
108122 * @throws UnknownHostException - if host cannot be found
109123 */
110124 private void resolveAddressString () throws UnknownHostException {
111- if (address == null && addressString !=null ){
125+ if (address == null && addressString != null ) {
112126 address = InetAddress .getByName (addressString );
113127 }
114128 }
@@ -125,6 +139,7 @@ public void cancel() {
125139 *
126140 * Note that this should be performed on a background thread as it will perform a network
127141 * request
142+ *
128143 * @return - ping result
129144 */
130145 public PingResult doPing () throws UnknownHostException {
@@ -135,10 +150,11 @@ public PingResult doPing() throws UnknownHostException {
135150
136151 /**
137152 * Perform an asynchronous ping
153+ *
138154 * @param pingListener - the listener to fire PingResults to.
139155 * @return - this so we can cancel if needed
140156 */
141- public Ping doPing (final PingListener pingListener ){
157+ public Ping doPing (final PingListener pingListener ) {
142158
143159 new Thread (new Runnable () {
144160 @ Override
@@ -151,7 +167,7 @@ public void run() {
151167 return ;
152168 }
153169
154- if (address == null ){
170+ if (address == null ) {
155171 pingListener .onError (new NullPointerException ("Address is null" ));
156172 return ;
157173 }
@@ -166,24 +182,23 @@ public void run() {
166182 int noPings = times ;
167183
168184 // times == 0 is the case that we can continuous scanning
169- while (noPings > 0 || times == 0 ){
185+ while (noPings > 0 || times == 0 ) {
170186 PingResult pingResult = PingTools .doPing (address , timeOutMillis );
171187
172- if (pingListener != null ){
188+ if (pingListener != null ) {
173189 pingListener .onResult (pingResult );
174190 }
175191
176192 // Update ping stats
177193 pingsCompleted ++;
178194
179- if (pingResult .hasError ()){
195+ if (pingResult .hasError ()) {
180196 noLostPackets ++;
181- }
182- else {
197+ } else {
183198 float timeTaken = pingResult .getTimeTaken ();
184199 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 ;
187202 }
188203
189204 noPings --;
@@ -196,12 +211,12 @@ public void run() {
196211 }
197212 }
198213
199- if (pingListener != null ){
214+ if (pingListener != null ) {
200215 pingListener .onFinished (new PingStats (address , pingsCompleted , noLostPackets , totalPingTime , minPingTime , maxPingTime ));
201216 }
202217 }
203218 }).start ();
204219 return this ;
205220 }
206221
207- }
222+ }
0 commit comments