You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.5 KiB
66 lines
1.5 KiB
package com.speculos.myapplicationoc;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
public class ThreadPing extends Thread {
|
|
|
|
/* Ce ThreadPing n'est pas un "ping" à proprement parler
|
|
* on réalise un call GET sur http://domain.dev/api
|
|
* pour savoir si le serveur répond
|
|
* */
|
|
|
|
public static Boolean isReachable;
|
|
private String url;
|
|
|
|
// Constructeur
|
|
public ThreadPing(String url){
|
|
this.url = url;
|
|
}
|
|
|
|
private int delay = 3000; // temps entre chaque ping 5sec
|
|
|
|
private PingListener listen = null;
|
|
|
|
public void addListener(PingListener listen){
|
|
this.listen = listen;
|
|
}
|
|
|
|
public void run() {
|
|
|
|
while(true) {
|
|
try {
|
|
String apiResponse = "";
|
|
try {
|
|
apiResponse = APICommandes.getMethod(url + "/api");
|
|
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
if (apiResponse != "") {
|
|
|
|
// Si PING OK declenche l'event ping(isReachable)
|
|
isReachable = true;
|
|
if(listen != null) listen.ping(isReachable);
|
|
|
|
} else {
|
|
|
|
isReachable = false;
|
|
if(listen != null) listen.ping(isReachable);
|
|
|
|
}
|
|
} catch (Exception e) {
|
|
System.out.println("Exception:" + e.getMessage());
|
|
|
|
}
|
|
|
|
try {
|
|
sleep(delay);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|