mirror of
https://github.com/michaelrausch/Party-Parrots-At-Sea.git
synced 2026-05-09 14:28:43 +00:00
WIP: Implemented a temporary workaround to send an instance test to client server upon connection.
Still needs reengineering to change socket channels for sending to ouput stream in the message class. Only client to server "working". #story[1047] #pair[hyi25, wmu16] #pair[cir27, zyt10]
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
package seng302.server;
|
||||
|
||||
import java.io.IOException;
|
||||
import seng302.server.messages.BoatActionMessage;
|
||||
|
||||
/**
|
||||
* Created by kre39 on 13/07/17.
|
||||
*/
|
||||
public class ClientTransmitterThread implements Runnable {
|
||||
private StreamingServerSocket server;
|
||||
private final int PORT_NUMBER = 4951;
|
||||
private static final int LOG_LEVEL = 1;
|
||||
|
||||
public ClientTransmitterThread(String threadName){
|
||||
Thread runner = new Thread(this, threadName);
|
||||
runner.setDaemon(true);
|
||||
runner.start();
|
||||
|
||||
}
|
||||
|
||||
static void serverLog(String message, int logLevel){
|
||||
if(logLevel <= LOG_LEVEL){
|
||||
System.out.println("[SERVER] " + message);
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try{
|
||||
// Needs to connect to the server: Currently no server is being connect so the boat action keys are not being sent
|
||||
server = new StreamingServerSocket(PORT_NUMBER);
|
||||
}
|
||||
catch (IOException e){
|
||||
serverLog("Failed to bind socket: " + e.getMessage(), 0);
|
||||
}
|
||||
|
||||
// Wait for client to connect
|
||||
server.start();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the post-start race course information
|
||||
*/
|
||||
public void sendBoatActionMessage(BoatActionMessage boatActionMessage) {
|
||||
try {
|
||||
server.send(boatActionMessage);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,13 +13,13 @@ import java.nio.channels.WritableByteChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class StreamingServerSocket {
|
||||
public class StreamingServerSocket {
|
||||
private ServerSocketChannel socket;
|
||||
private SocketChannel client;
|
||||
private short seqNum;
|
||||
private boolean isServerStarted;
|
||||
|
||||
StreamingServerSocket(int port) throws IOException{
|
||||
public StreamingServerSocket(int port) throws IOException{
|
||||
socket = ServerSocketChannel.open();
|
||||
socket.socket().bind(new InetSocketAddress("localhost", port));
|
||||
//socket.setSoTimeout(10000);
|
||||
@@ -27,7 +27,7 @@ class StreamingServerSocket {
|
||||
isServerStarted = false;
|
||||
}
|
||||
|
||||
void start(){
|
||||
public void start(){
|
||||
try {
|
||||
client = socket.accept();
|
||||
} catch (IOException e) {
|
||||
@@ -41,7 +41,7 @@ class StreamingServerSocket {
|
||||
}
|
||||
}
|
||||
|
||||
void send(Message message) throws IOException{
|
||||
public void send(Message message) throws IOException{
|
||||
if (client == null){
|
||||
return;
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package seng302.server.messages;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
|
||||
/**
|
||||
@@ -27,7 +28,7 @@ public class BoatActionMessage extends Message{
|
||||
* @param outputStream The output stream to send the message
|
||||
*/
|
||||
public void send(SocketChannel outputStream) throws IOException {
|
||||
System.out.println("Sending boat action type: " + actionType.toString());
|
||||
System.out.println("[CLIENT] Sending boat action type: " + actionType.toString());
|
||||
allocateBuffer();
|
||||
writeHeaderToBuffer();
|
||||
// Write message fields
|
||||
@@ -37,4 +38,15 @@ public class BoatActionMessage extends Message{
|
||||
|
||||
outputStream.write(getBuffer());
|
||||
}
|
||||
|
||||
|
||||
public ByteBuffer stealBuffer() {
|
||||
allocateBuffer();
|
||||
writeHeaderToBuffer();
|
||||
// Write message fields
|
||||
putInt((int) BoatActionType.getBoatPacketType(actionType), 1);
|
||||
writeCRC();
|
||||
rewind();
|
||||
return getBuffer();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,12 +5,22 @@ package seng302.server.messages;
|
||||
*/
|
||||
public enum BoatActionType {
|
||||
|
||||
VMG,
|
||||
SAILS_IN,
|
||||
SAILS_OUT,
|
||||
TACK_GYBE,
|
||||
UPWIND,
|
||||
DOWNWIND;
|
||||
VMG(1),
|
||||
SAILS_IN(2),
|
||||
SAILS_OUT(3),
|
||||
TACK_GYBE(4),
|
||||
UPWIND(5),
|
||||
DOWNWIND(6);
|
||||
|
||||
private int type;
|
||||
|
||||
BoatActionType(int type){
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public int getType(){
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public static Short getBoatPacketType(BoatActionType type){
|
||||
switch (type){
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package seng302.server.messages;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
|
||||
public class BoatLocationMessage extends Message {
|
||||
|
||||
Reference in New Issue
Block a user