mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
Getting boat locations from race simulator & bug fixes
- Boat locations that are generated by the simulator are sent to the client as they happen - Fixed heading and lat/lon encoding - Fixed a bug where the header wasn't included in the sent byte stream - Fixed the format of data as it's sent to the client. - Data is now sent using a channel - Removed tests that don't work with channels Tags: #story[829]
This commit is contained in:
@@ -4,35 +4,44 @@ import seng302.server.messages.Message;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.nio.channels.Channels;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class StreamingServerSocket {
|
||||
private java.net.ServerSocket socket;
|
||||
private Socket client;
|
||||
private ServerSocketChannel socket;
|
||||
private SocketChannel client;
|
||||
private List<Socket> clients;
|
||||
private short seqNum;
|
||||
private boolean isServerStarted;
|
||||
|
||||
StreamingServerSocket(int port) throws IOException{
|
||||
socket = new java.net.ServerSocket(port);
|
||||
socket = ServerSocketChannel.open();
|
||||
socket.socket().bind(new InetSocketAddress("localhost", port));
|
||||
clients = new ArrayList<>();
|
||||
socket.setSoTimeout(10000);
|
||||
//socket.setSoTimeout(10000);
|
||||
seqNum = 0;
|
||||
isServerStarted = false;
|
||||
}
|
||||
|
||||
void start(){
|
||||
System.out.println("Listening For Connections");
|
||||
ServerThread.serverLog("Listening For Connections",0);
|
||||
try {
|
||||
client = socket.accept();
|
||||
} catch (IOException e) {
|
||||
e.getMessage();
|
||||
}
|
||||
if (client == null){
|
||||
if (client.socket() == null){
|
||||
start();
|
||||
}
|
||||
else{
|
||||
System.out.println("client connected from " + client.getInetAddress());
|
||||
isServerStarted = true;
|
||||
ServerThread.serverLog("client connected from " + client.socket().getInetAddress(),0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,8 +50,9 @@ class StreamingServerSocket {
|
||||
return;
|
||||
}
|
||||
|
||||
DataOutputStream outputStream = new DataOutputStream(client.getOutputStream());
|
||||
message.send(outputStream);
|
||||
//DataOutputStream outputStream = new DataOutputStream(client.getOutputStream());
|
||||
//System.out.println(client);
|
||||
message.send(client);
|
||||
|
||||
seqNum++;
|
||||
}
|
||||
@@ -50,4 +60,8 @@ class StreamingServerSocket {
|
||||
public short getSequenceNumber(){
|
||||
return seqNum;
|
||||
}
|
||||
|
||||
public boolean isStarted(){
|
||||
return isServerStarted;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user