Nullptr finish race arrow + race xml issues + Host Discovery + Scaling Race Acceptance Criteria: When a game starts I should see it reflected in the host lobby within 5 seconds. When a game ends it should not remain in the host lobby for more than 5 seconds. Changing the windspeed and the milliseconds value on Event.java line 124 should make the race larger or smaller. This should fix the race xml issues where the player didn't have a boat / last player joining not seeing their boat. Somehow the client was receiving xml files out of order, and it didn't check the order, so it would use race xml file 1, then replace it with race xml file 0, which was out of date. See merge request !54main
commit
951c3771fa
@ -0,0 +1,70 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* A helper class that has functions to read information from a url to json object.
|
||||
*/
|
||||
public class JsonReader {
|
||||
|
||||
/**
|
||||
* Reads all data from a Reader
|
||||
* @param rd reader to read from
|
||||
* @return string that the reader has currently read
|
||||
* @throws IOException if the reader is invalid
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a Json Object from a URL
|
||||
* @param url url to read from
|
||||
* @return JSONObject that has been read
|
||||
* @throws IOException if the reader cannot obtain information
|
||||
* @throws JSONException if the read information is not json parsable.
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a Json Array from a URL
|
||||
* @param url url to read from
|
||||
* @return JSONArray that has been read
|
||||
* @throws IOException if the reader cannot obtain information
|
||||
* @throws JSONException if the read information is not json parsable.
|
||||
*/
|
||||
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,42 @@
|
||||
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();
|
||||
|
||||
/**
|
||||
* Get all the matches that have been running in the past 5 seconds.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
while(!Thread.interrupted()) {
|
||||
try {
|
||||
JSONArray cons = JsonReader.readJsonFromUrlArray("http://api.umbrasheep.com/seng/get_matches/");
|
||||
connections.clear();
|
||||
|
||||
for (int i = 0; i < cons.length(); i++) {
|
||||
JSONObject con = (JSONObject) cons.get(i);
|
||||
//using "ip_address" will give their public ip
|
||||
connections.add(new RaceConnection((String) con.get("local_ip"), con.getInt("port"), "Boat Game"));
|
||||
}
|
||||
|
||||
Thread.sleep(5000);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,115 @@
|
||||
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.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Creates an Http connection that hosts a game
|
||||
*/
|
||||
public class HttpMatchBrowserHost extends Thread {
|
||||
private HttpClient httpClient;
|
||||
private List<NameValuePair> params;
|
||||
|
||||
public static HttpMatchBrowserHost httpMatchBrowserHost = null;
|
||||
|
||||
/**
|
||||
* Constructor, this sends out the creation message of the race.
|
||||
* the thread should be run as soon as possible as the race is only valid for 5 seconds
|
||||
* until it requires a heartbeat from the start function.
|
||||
* @throws IOException if the hosting url is unreachable.
|
||||
*/
|
||||
public HttpMatchBrowserHost() throws IOException {
|
||||
httpMatchBrowserHost = this;
|
||||
httpClient = HttpClients.createDefault();
|
||||
|
||||
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
|
||||
boolean matches = false;
|
||||
String ip = "";
|
||||
Pattern ipPattern = Pattern.compile("192.168.1.*");
|
||||
while(e.hasMoreElements())
|
||||
{
|
||||
if (matches){
|
||||
break;
|
||||
}
|
||||
NetworkInterface n = (NetworkInterface) e.nextElement();
|
||||
Enumeration<InetAddress> ee = n.getInetAddresses();
|
||||
while (ee.hasMoreElements())
|
||||
{
|
||||
InetAddress i = ee.nextElement();
|
||||
matches = ipPattern.matcher(i.getHostAddress()).matches();
|
||||
if (matches){
|
||||
ip = i.getHostAddress();
|
||||
//System.out.println(ip);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Request parameters and other properties.
|
||||
params = new ArrayList<>(2);
|
||||
params.add(new BasicNameValuePair("ip", ip));
|
||||
params.add(new BasicNameValuePair("port", "4942"));
|
||||
params.add(new BasicNameValuePair("magic", "Thomas and Seng"));
|
||||
|
||||
sendHttp("http://api.umbrasheep.com/seng/registermatch/");
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a post to a server.
|
||||
* @param domain url of to send to
|
||||
* @throws IOException if the url is unreachable.
|
||||
*/
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* THe host starts sending out heartbeat messages every 2 seconds.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
while(!Thread.interrupted()){
|
||||
try {
|
||||
sendHttp("http://api.umbrasheep.com/seng/keep_match_alive/");
|
||||
Thread.sleep(2000);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue