Trying to fix the boat information that is being sent over.

Current issue is that a broken pipe error keeps occurring potentially due to how the messages are being sent so quickly that the previous message may have not finished sending. Also only caused the xm packets to be sent on trigger when a new client connects.

#story[1047]
This commit is contained in:
Kusal Ekanayake
2017-07-23 16:22:59 +12:00
parent 33fae9d69a
commit 2e4382bff6
4 changed files with 38 additions and 30 deletions
@@ -267,14 +267,13 @@ public class ClientPacketParser {
* @param packet Packet parsed in to use the payload * @param packet Packet parsed in to use the payload
*/ */
private static void extractXmlMessage(StreamPacket packet) { private static void extractXmlMessage(StreamPacket packet) {
xmlObject = new XMLParser();
byte[] payload = packet.getPayload(); byte[] payload = packet.getPayload();
int messageType = payload[9]; int messageType = payload[9];
long messageLength = bytesToLong(Arrays.copyOfRange(payload, 12, 14)); long messageLength = bytesToLong(Arrays.copyOfRange(payload, 12, 14));
String xmlMessage = new String( String xmlMessage = new String(
(Arrays.copyOfRange(payload, 14, (int) (14 + messageLength)))).trim(); (Arrays.copyOfRange(payload, 14, (int) (14 + messageLength)))).trim();
System.out.println(xmlMessage);
//Create XML document Object //Create XML document Object
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null; DocumentBuilder db = null;
@@ -376,11 +375,12 @@ public class ClientPacketParser {
double lon = ((180d * (double) rawLon) / Math.pow(2, 31)); double lon = ((180d * (double) rawLon) / Math.pow(2, 31));
long heading = bytesToLong(Arrays.copyOfRange(payload, 28, 30)); long heading = bytesToLong(Arrays.copyOfRange(payload, 28, 30));
double groundSpeed = bytesToLong(Arrays.copyOfRange(payload, 38, 40)) / 1000.0; double groundSpeed = bytesToLong(Arrays.copyOfRange(payload, 38, 40)) / 1000.0;
System.out.println("boats = " + boats.values());
System.out.println("boats = " + boats.keySet());
//type 1 is a racing yacht and type 3 is a mark, needed for updating positions of the mark and boat //type 1 is a racing yacht and type 3 is a mark, needed for updating positions of the mark and boat
if (deviceType == 1){ if (deviceType == 1){
Yacht boat = boats.get((int) boatId); Yacht boat = boats.get((int) boatId);
boat.setVelocity(groundSpeed); // boat.setVelocity(groundSpeed);
BoatPositionPacket boatPacket = new BoatPositionPacket(boatId, timeValid, lat, lon, heading, groundSpeed); BoatPositionPacket boatPacket = new BoatPositionPacket(boatId, timeValid, lat, lon, heading, groundSpeed);
//add a new priority que to the boatLocations HashMap //add a new priority que to the boatLocations HashMap
@@ -1,29 +1,27 @@
package seng302.gameServer; package seng302.gameServer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random; import java.util.Random;
import seng302.client.ClientPacketParser; import java.util.zip.CRC32;
import java.util.zip.Checksum;
import seng302.models.Player; import seng302.models.Player;
import seng302.models.Yacht; import seng302.models.Yacht;
import seng302.models.stream.packets.PacketType; import seng302.models.stream.packets.PacketType;
import seng302.models.stream.packets.StreamPacket; import seng302.models.stream.packets.StreamPacket;
import seng302.server.messages.ChatterMessage;
import seng302.server.messages.Heartbeat;
import seng302.server.messages.BoatActionType;
import seng302.server.messages.Message;
import seng302.models.xml.Race; import seng302.models.xml.Race;
import seng302.models.xml.Regatta; import seng302.models.xml.Regatta;
import seng302.models.xml.XMLGenerator; import seng302.models.xml.XMLGenerator;
import seng302.server.messages.*; import seng302.server.messages.BoatActionType;
import seng302.models.xml.XMLGenerator; import seng302.server.messages.BoatLocationMessage;
import seng302.server.messages.*; import seng302.server.messages.Message;
import seng302.server.messages.XMLMessage;
import java.io.*; import seng302.server.messages.XMLMessageSubType;
import java.net.Socket;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import seng302.utilities.GeoPoint;
/** /**
* A class describing a single connection to a Client for the purposes of sending and receiving on its own thread. * A class describing a single connection to a Client for the purposes of sending and receiving on its own thread.
@@ -42,7 +40,7 @@ public class ServerToClientThread extends Thread {
private Boolean userIdentified = false; private Boolean userIdentified = false;
private Boolean connected = true; private Boolean connected = true;
private Boolean updateClient = true; private Boolean updateClient = true;
private Boolean intiialisedRace = false; private Boolean initialisedRace = false;
private Integer seqNo; private Integer seqNo;
private Integer sourceId; private Integer sourceId;
@@ -58,10 +56,11 @@ public class ServerToClientThread extends Thread {
System.out.println("IO error in server thread upon grabbing streams"); System.out.println("IO error in server thread upon grabbing streams");
} }
// threeWayHandshake(); // threeWayHandshake();
GameState.addPlayer(new Player(socket));
Random rand = new Random(); Random rand = new Random();
sourceId = rand.nextInt(100000); sourceId = rand.nextInt(100000);
GameState.addYacht(sourceId, new Yacht("Kappa", "Kap", new GeoPoint(0.0, 0.0), 0.0)); Yacht yacht = new Yacht("Yacht", sourceId, sourceId.toString(), "Kap", "Kappa", "NZ");
GameState.addYacht(sourceId, yacht);
GameState.addPlayer(new Player(socket, yacht));
seqNo = 0; seqNo = 0;
} }
@@ -73,21 +72,22 @@ public class ServerToClientThread extends Thread {
//System.out.print("."); //System.out.print(".");
try { try {
if (intiialisedRace) { if (initialisedRace) {
sendSetupMessages(); sendSetupMessages();
initialisedRace = false;
} }
//Perform a write if it is time to as delegated by the MainServerThread //Perform a write if it is time to as delegated by the MainServerThread
if (updateClient) { if (updateClient) {
// TODO: 13/07/17 wmu16 - Write out game state - some function that would write all appropriate messages to this output stream // TODO: 13/07/17 wmu16 - Write out game state - some function that would write all appropriate messages to this output stream
ChatterMessage chatterMessage = new ChatterMessage(4, 14, "Hello, it's me"); // ChatterMessage chatterMessage = new ChatterMessage(4, 14, "Hello, it's me");
sendMessage(chatterMessage); // sendMessage(chatterMessage);
sendBoatLocationPackets();
// try { // try {
// GameState.outputState(os); // GameState.outputState(os);
// } catch (IOException e) { // } catch (IOException e) {
// System.out.println("IO error in server thread upon writing to output stream"); // System.out.println("IO error in server thread upon writing to output stream");
// } // }
sendBoatLocationPackets();
updateClient = false; updateClient = false;
} }
@@ -151,6 +151,7 @@ public class ServerToClientThread extends Thread {
xmlMessage = new XMLMessage(xml.getRaceAsXml(), XMLMessageSubType.RACE, xml.getRaceAsXml().length()); xmlMessage = new XMLMessage(xml.getRaceAsXml(), XMLMessageSubType.RACE, xml.getRaceAsXml().length());
sendMessage(xmlMessage); sendMessage(xmlMessage);
} }
public void updateClient() { public void updateClient() {
@@ -195,7 +196,7 @@ public class ServerToClientThread extends Thread {
public void initialiseRace(){ public void initialiseRace(){
intiialisedRace = true; initialisedRace = true;
} }
@@ -245,7 +246,11 @@ public class ServerToClientThread extends Thread {
private void sendBoatLocationPackets(){ private void sendBoatLocationPackets(){
ArrayList<Yacht> yachts = new ArrayList<>(GameState.getYachts().values()); ArrayList<Yacht> yachts = new ArrayList<>(GameState.getYachts().values());
for (Yacht yacht: yachts){ for (Yacht yacht: yachts){
BoatLocationMessage boatLocationMessage = new BoatLocationMessage(sourceId,getSeqNo(), yacht.getLocation().getLat(), yacht.getLocation().getLng(), yacht.getHeading(), (long) yacht.getVelocity()); BoatLocationMessage boatLocationMessage = new BoatLocationMessage(sourceId, getSeqNo(), yacht.getLocation().getLat(), yacht.getLocation().getLng(), yacht.getHeading(), (long) yacht.getVelocity());
// System.out.println("yacht.getLocation().getLat() = " + yacht.getLocation().getLat());
// System.out.println("yacht.getLocation().getLng() = " + yacht.getLocation().getLng());
// System.out.println("yacht.getBoatName() = " + yacht.getBoatName());
// System.out.println("yacht = " + sourceId);
sendMessage(boatLocationMessage); sendMessage(boatLocationMessage);
} }
} }
+2 -2
View File
@@ -17,9 +17,9 @@ public class Player {
private Integer lastMarkPassed; private Integer lastMarkPassed;
public Player(Socket socket) { public Player(Socket socket, Yacht yacht) {
this.socket = socket; this.socket = socket;
yacht = new Yacht("test", 12.2, "asd", 12); this.yacht = yacht;
} }
public Socket getSocket() { public Socket getSocket() {
+3
View File
@@ -86,6 +86,9 @@ public class Yacht {
this.boatName = boatName; this.boatName = boatName;
this.country = country; this.country = country;
this.position = "-"; this.position = "-";
this.location = new GeoPoint(0.0, 0.0);
this.heading = 0.0;
this.velocity = 0.0;
} }
/** /**