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
*/
private static void extractXmlMessage(StreamPacket packet) {
xmlObject = new XMLParser();
byte[] payload = packet.getPayload();
int messageType = payload[9];
long messageLength = bytesToLong(Arrays.copyOfRange(payload, 12, 14));
String xmlMessage = new String(
(Arrays.copyOfRange(payload, 14, (int) (14 + messageLength)))).trim();
System.out.println(xmlMessage);
//Create XML document Object
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
@@ -376,11 +375,12 @@ public class ClientPacketParser {
double lon = ((180d * (double) rawLon) / Math.pow(2, 31));
long heading = bytesToLong(Arrays.copyOfRange(payload, 28, 30));
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
if (deviceType == 1){
Yacht boat = boats.get((int) boatId);
boat.setVelocity(groundSpeed);
// boat.setVelocity(groundSpeed);
BoatPositionPacket boatPacket = new BoatPositionPacket(boatId, timeValid, lat, lon, heading, groundSpeed);
//add a new priority que to the boatLocations HashMap
@@ -1,29 +1,27 @@
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.Random;
import seng302.client.ClientPacketParser;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import seng302.models.Player;
import seng302.models.Yacht;
import seng302.models.stream.packets.PacketType;
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.Regatta;
import seng302.models.xml.XMLGenerator;
import seng302.server.messages.*;
import seng302.models.xml.XMLGenerator;
import seng302.server.messages.*;
import java.io.*;
import java.net.Socket;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
import seng302.utilities.GeoPoint;
import seng302.server.messages.BoatActionType;
import seng302.server.messages.BoatLocationMessage;
import seng302.server.messages.Message;
import seng302.server.messages.XMLMessage;
import seng302.server.messages.XMLMessageSubType;
/**
* 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 connected = true;
private Boolean updateClient = true;
private Boolean intiialisedRace = false;
private Boolean initialisedRace = false;
private Integer seqNo;
private Integer sourceId;
@@ -58,10 +56,11 @@ public class ServerToClientThread extends Thread {
System.out.println("IO error in server thread upon grabbing streams");
}
// threeWayHandshake();
GameState.addPlayer(new Player(socket));
Random rand = new Random();
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;
}
@@ -73,21 +72,22 @@ public class ServerToClientThread extends Thread {
//System.out.print(".");
try {
if (intiialisedRace) {
if (initialisedRace) {
sendSetupMessages();
initialisedRace = false;
}
//Perform a write if it is time to as delegated by the MainServerThread
if (updateClient) {
// 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");
sendMessage(chatterMessage);
sendBoatLocationPackets();
// ChatterMessage chatterMessage = new ChatterMessage(4, 14, "Hello, it's me");
// sendMessage(chatterMessage);
// try {
// GameState.outputState(os);
// } catch (IOException e) {
// System.out.println("IO error in server thread upon writing to output stream");
// }
sendBoatLocationPackets();
updateClient = false;
}
@@ -151,6 +151,7 @@ public class ServerToClientThread extends Thread {
xmlMessage = new XMLMessage(xml.getRaceAsXml(), XMLMessageSubType.RACE, xml.getRaceAsXml().length());
sendMessage(xmlMessage);
}
public void updateClient() {
@@ -195,7 +196,7 @@ public class ServerToClientThread extends Thread {
public void initialiseRace(){
intiialisedRace = true;
initialisedRace = true;
}
@@ -245,7 +246,11 @@ public class ServerToClientThread extends Thread {
private void sendBoatLocationPackets(){
ArrayList<Yacht> yachts = new ArrayList<>(GameState.getYachts().values());
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);
}
}
+2 -2
View File
@@ -17,9 +17,9 @@ public class Player {
private Integer lastMarkPassed;
public Player(Socket socket) {
public Player(Socket socket, Yacht yacht) {
this.socket = socket;
yacht = new Yacht("test", 12.2, "asd", 12);
this.yacht = yacht;
}
public Socket getSocket() {
+3
View File
@@ -86,6 +86,9 @@ public class Yacht {
this.boatName = boatName;
this.country = country;
this.position = "-";
this.location = new GeoPoint(0.0, 0.0);
this.heading = 0.0;
this.velocity = 0.0;
}
/**