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 ;
@@ -18,9 +16,11 @@ public class Ping {
1816 private Ping () {
1917 }
2018
21- public interface PingListener {
19+ public interface PingListener {
2220 void onResult (PingResult pingResult );
21+
2322 void onFinished (PingStats pingStats );
23+
2424 void onError (Exception e );
2525 }
2626
@@ -33,59 +33,64 @@ public interface PingListener{
3333
3434 /**
3535 * Set the address to ping
36- *
36+ * <p>
3737 * Note that a lookup is not performed here so that we do not accidentally perform a network
3838 * request on the UI thread.
3939 *
4040 * @param address - Address to be pinged
4141 * @return this object to allow chaining
4242 */
43- public static Ping onAddress (@ NonNull String address ) {
43+ public static Ping onAddress (String address ) {
4444 Ping ping = new Ping ();
4545 ping .setAddressString (address );
4646 return ping ;
4747 }
4848
4949 /**
5050 * Set the address to ping
51+ *
5152 * @param ia - Address to be pinged
5253 * @return this object to allow chaining
5354 */
54- public static Ping onAddress (@ NonNull InetAddress ia ) {
55+ public static Ping onAddress (InetAddress ia ) {
5556 Ping ping = new Ping ();
5657 ping .setAddress (ia );
5758 return ping ;
5859 }
5960
6061 /**
6162 * Set the timeout
63+ *
6264 * @param timeOutMillis - the timeout for each ping in milliseconds
6365 * @return this object to allow chaining
6466 */
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" );
6769 this .timeOutMillis = timeOutMillis ;
6870 return this ;
6971 }
7072
7173 /**
7274 * Set the delay between each ping
75+ *
7376 * @param delayBetweenScansMillis - the timeout for each ping in milliseconds
7477 * @return this object to allow chaining
7578 */
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" );
7882 this .delayBetweenScansMillis = delayBetweenScansMillis ;
7983 return this ;
8084 }
8185
8286 /**
8387 * Set number of times to ping the address
88+ *
8489 * @param noTimes - number of times, 0 = continuous
8590 * @return this object to allow chaining
8691 */
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" );
8994 this .times = noTimes ;
9095 return this ;
9196 }
@@ -96,6 +101,7 @@ private void setAddress(InetAddress address) {
96101
97102 /**
98103 * Set the address string which will be resolved to an address by resolveAddressString()
104+ *
99105 * @param addressString - String of the address to be pinged
100106 */
101107 private void setAddressString (String addressString ) {
@@ -108,7 +114,7 @@ private void setAddressString(String addressString) {
108114 * @throws UnknownHostException - if host cannot be found
109115 */
110116 private void resolveAddressString () throws UnknownHostException {
111- if (address == null && addressString !=null ){
117+ if (address == null && addressString != null ) {
112118 address = InetAddress .getByName (addressString );
113119 }
114120 }
@@ -122,9 +128,10 @@ public void cancel() {
122128
123129 /**
124130 * Perform a synchronous ping and return a result, will ignore number of times.
125- *
131+ * <p>
126132 * Note that this should be performed on a background thread as it will perform a network
127133 * request
134+ *
128135 * @return - ping result
129136 */
130137 public PingResult doPing () throws UnknownHostException {
@@ -135,10 +142,11 @@ public PingResult doPing() throws UnknownHostException {
135142
136143 /**
137144 * Perform an asynchronous ping
145+ *
138146 * @param pingListener - the listener to fire PingResults to.
139147 * @return - this so we can cancel if needed
140148 */
141- public Ping doPing (final PingListener pingListener ){
149+ public Ping doPing (final PingListener pingListener ) {
142150
143151 new Thread (new Runnable () {
144152 @ Override
@@ -151,7 +159,7 @@ public void run() {
151159 return ;
152160 }
153161
154- if (address == null ){
162+ if (address == null ) {
155163 pingListener .onError (new NullPointerException ("Address is null" ));
156164 return ;
157165 }
@@ -166,24 +174,23 @@ public void run() {
166174 int noPings = times ;
167175
168176 // times == 0 is the case that we can continuous scanning
169- while (noPings > 0 || times == 0 ){
177+ while (noPings > 0 || times == 0 ) {
170178 PingResult pingResult = PingTools .doPing (address , timeOutMillis );
171179
172- if (pingListener != null ){
180+ if (pingListener != null ) {
173181 pingListener .onResult (pingResult );
174182 }
175183
176184 // Update ping stats
177185 pingsCompleted ++;
178186
179- if (pingResult .hasError ()){
187+ if (pingResult .hasError ()) {
180188 noLostPackets ++;
181- }
182- else {
189+ } else {
183190 float timeTaken = pingResult .getTimeTaken ();
184191 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 ;
187194 }
188195
189196 noPings --;
@@ -196,7 +203,7 @@ public void run() {
196203 }
197204 }
198205
199- if (pingListener != null ){
206+ if (pingListener != null ) {
200207 pingListener .onFinished (new PingStats (address , pingsCompleted , noLostPackets , totalPingTime , minPingTime , maxPingTime ));
201208 }
202209 }
0 commit comments