Hopefully final version of Hosting #story[1188]

This commit is contained in:
Fan-Wu Yang
2017-09-19 02:15:34 +12:00
parent 9d1145b298
commit 928f5e31f0
8 changed files with 207 additions and 11 deletions
@@ -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();
}
}
}
}