parent
9d1145b298
commit
928f5e31f0
@ -0,0 +1,50 @@
|
||||
package shared.utils;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/**
|
||||
* Created by Gondr on 23/05/2017.
|
||||
*/
|
||||
public class JsonReader {
|
||||
|
||||
private static String readAll(Reader rd) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int cp;
|
||||
while ((cp = rd.read()) != -1) {
|
||||
sb.append((char) cp);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
|
||||
InputStream is = new URL(url).openStream();
|
||||
try {
|
||||
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
|
||||
String jsonText = readAll(rd);
|
||||
JSONObject json = new JSONObject(jsonText);
|
||||
return json;
|
||||
} catch (JSONException e) {
|
||||
return null;
|
||||
} finally {
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static JSONArray readJsonFromUrlArray(String url) throws IOException, JSONException {
|
||||
InputStream is = new URL(url).openStream();
|
||||
try {
|
||||
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
|
||||
String jsonText = readAll(rd);
|
||||
JSONArray json = new JSONArray(jsonText);
|
||||
return json;
|
||||
} finally {
|
||||
is.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
package visualiser.network;
|
||||
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import shared.utils.JsonReader;
|
||||
import visualiser.model.RaceConnection;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by Gondr on 19/09/2017.
|
||||
*/
|
||||
public class HttpMatchBrowserClient extends Thread {
|
||||
public ObservableList<RaceConnection> connections = FXCollections.observableArrayList();
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while(!Thread.interrupted()) {
|
||||
try {
|
||||
JSONArray cons = JsonReader.readJsonFromUrlArray("http://api.umbrasheep.com/seng/get_matches/");
|
||||
System.out.println("Got stuff");
|
||||
System.out.println(cons.toString());
|
||||
for (int i = 0; i < cons.length(); i++) {
|
||||
JSONObject con = (JSONObject) cons.get(i);
|
||||
connections.add(new RaceConnection((String) con.get("ip_address"), con.getInt("port"), "Boat Game"));
|
||||
}
|
||||
|
||||
Thread.sleep(5000);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,75 @@
|
||||
package visualiser.network;
|
||||
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by Gondr on 19/09/2017.
|
||||
*/
|
||||
public class HttpMatchBrowserHost extends Thread {
|
||||
private HttpClient httpClient;
|
||||
private List<NameValuePair> params;
|
||||
|
||||
public static HttpMatchBrowserHost httpMatchBrowserHost = null;
|
||||
|
||||
public HttpMatchBrowserHost() throws IOException {
|
||||
httpMatchBrowserHost = this;
|
||||
httpClient = HttpClients.createDefault();
|
||||
|
||||
// Request parameters and other properties.
|
||||
params = new ArrayList<>(2);
|
||||
params.add(new BasicNameValuePair("port", "4942"));
|
||||
params.add(new BasicNameValuePair("magic", "Thomas and Seng"));
|
||||
|
||||
sendHttp("http://api.umbrasheep.com/seng/registermatch/");
|
||||
System.out.println("Register Match");
|
||||
}
|
||||
|
||||
public void sendHttp(String domain) throws IOException {
|
||||
HttpPost httppost = new HttpPost(domain);
|
||||
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
|
||||
|
||||
//Execute and get the response.
|
||||
HttpResponse response = httpClient.execute(httppost);
|
||||
HttpEntity entity = response.getEntity();
|
||||
|
||||
if (entity != null) {
|
||||
InputStream instream = entity.getContent();
|
||||
try {
|
||||
// do something useful
|
||||
} finally {
|
||||
instream.close();
|
||||
}
|
||||
} else {
|
||||
throw new IOException("No Response from Host");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while(!Thread.interrupted()){
|
||||
try {
|
||||
sendHttp("http://api.umbrasheep.com/seng/keep_match_alive/");
|
||||
System.out.println("Keep match ALive");
|
||||
Thread.sleep(2000);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue