Fixed blocking issue
- Fixed byte stream error where it would reinitilise every run #story[782]
This commit is contained in:
@@ -9,6 +9,7 @@ import seng302.Networking.Utils.*;
|
|||||||
import javax.xml.parsers.ParserConfigurationException;
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
import java.nio.Buffer;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.ByteOrder;
|
import java.nio.ByteOrder;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
@@ -50,11 +51,13 @@ public class VisualiserInput implements Runnable
|
|||||||
|
|
||||||
private RaceStartStatus raceStartStatus;
|
private RaceStartStatus raceStartStatus;
|
||||||
|
|
||||||
|
private BufferedInputStream inStream;
|
||||||
|
|
||||||
public VisualiserInput(Socket socket, StreamedCourse course) throws IOException{
|
public VisualiserInput(Socket socket, StreamedCourse course) throws IOException{
|
||||||
|
|
||||||
this.connectionSocket = socket;
|
this.connectionSocket = socket;
|
||||||
// this.connectionSocket = new Socket("livedata.americascup.com",4941);
|
// this.connectionSocket = new Socket("livedata.americascup.com",4941);
|
||||||
|
this.inStream = new BufferedInputStream(connectionSocket.getInputStream());
|
||||||
this.course = course;
|
this.course = course;
|
||||||
this.boatLocation = new HashMap<>();
|
this.boatLocation = new HashMap<>();
|
||||||
this.boatStatus = new HashMap<>();
|
this.boatStatus = new HashMap<>();
|
||||||
@@ -143,11 +146,11 @@ public class VisualiserInput implements Runnable
|
|||||||
/**
|
/**
|
||||||
* Takes an inputStream and reads the first 15 bytes (the header) and gets the message length
|
* Takes an inputStream and reads the first 15 bytes (the header) and gets the message length
|
||||||
* for the whole message then reads that and returns the byte array
|
* for the whole message then reads that and returns the byte array
|
||||||
* @param inStream inputStream from socket
|
|
||||||
* @return encoded binary messsage bytes
|
* @return encoded binary messsage bytes
|
||||||
* @throws IOException made by the inputstream reading
|
* @throws IOException made by the inputstream reading
|
||||||
*/
|
*/
|
||||||
private static byte[] getBytes(InputStream inStream) throws IOException {
|
private byte[] getBytes() throws IOException {
|
||||||
|
inStream.mark(0);
|
||||||
if (inStream.available() < 15) return null;//if there is not enough bytes foer the headerr
|
if (inStream.available() < 15) return null;//if there is not enough bytes foer the headerr
|
||||||
byte[] headerBytes = new byte[15];
|
byte[] headerBytes = new byte[15];
|
||||||
inStream.read(headerBytes);
|
inStream.read(headerBytes);
|
||||||
@@ -170,11 +173,8 @@ public class VisualiserInput implements Runnable
|
|||||||
//receiver loop that gets the input
|
//receiver loop that gets the input
|
||||||
boolean receiverLoop = true;
|
boolean receiverLoop = true;
|
||||||
while(receiverLoop) {
|
while(receiverLoop) {
|
||||||
//gets the input from the socket
|
|
||||||
InputStream inFromClient = connectionSocket.getInputStream();
|
|
||||||
|
|
||||||
//converts the input into a byte array that can be read by the decoder
|
//converts the input into a byte array that can be read by the decoder
|
||||||
byte[] binaryMessage = getBytes(inFromClient);
|
byte[] binaryMessage = getBytes();
|
||||||
//if there is no bytes read.
|
//if there is no bytes read.
|
||||||
if (binaryMessage == null){
|
if (binaryMessage == null){
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
Reference in New Issue
Block a user