diff --git a/Assignment 4/src/ca/cosc3p91/a4/Main.java b/Assignment 4/src/ca/cosc3p91/a4/Main.java index 59c3aa1..1242464 100644 --- a/Assignment 4/src/ca/cosc3p91/a4/Main.java +++ b/Assignment 4/src/ca/cosc3p91/a4/Main.java @@ -1,6 +1,6 @@ package ca.cosc3p91.a4; -import ca.cosc3p91.a4.util.Client; +import ca.cosc3p91.a4.util.network.Client; import java.io.*; public class Main { diff --git a/Assignment 4/src/ca/cosc3p91/a4/userinterface/GameDisplay.java b/Assignment 4/src/ca/cosc3p91/a4/userinterface/GameDisplay.java index 5b6680b..7120eee 100644 --- a/Assignment 4/src/ca/cosc3p91/a4/userinterface/GameDisplay.java +++ b/Assignment 4/src/ca/cosc3p91/a4/userinterface/GameDisplay.java @@ -15,8 +15,8 @@ public class GameDisplay { private BufferedReader reader; private String input; - public GameDisplay(InputStream readFrom) { - reader = new BufferedReader(new InputStreamReader(readFrom)); + public GameDisplay() { + reader = new BufferedReader(new InputStreamReader(System.in)); } public String nextInput() throws IOException { diff --git a/Assignment 4/src/ca/cosc3p91/a4/util/PacketTable.java b/Assignment 4/src/ca/cosc3p91/a4/util/PacketTable.java deleted file mode 100644 index 471f49e..0000000 --- a/Assignment 4/src/ca/cosc3p91/a4/util/PacketTable.java +++ /dev/null @@ -1,16 +0,0 @@ -package ca.cosc3p91.a4.util; - -public class PacketTable { - - // packetID -> byte defined in this file - // clientID -> long - // messageID -> long - - // packetID, clientID (0 if connecting to server) - public static final byte CONNECT = 0x1; - // packetID, clientID - public static final byte DISCONNECT = 0x2; - // packetID, clientID, messageID - public static final byte ACK = 0x3; - -} diff --git a/Assignment 4/src/ca/cosc3p91/a4/util/Client.java b/Assignment 4/src/ca/cosc3p91/a4/util/network/Client.java similarity index 93% rename from Assignment 4/src/ca/cosc3p91/a4/util/Client.java rename to Assignment 4/src/ca/cosc3p91/a4/util/network/Client.java index 8c622bc..3b65efb 100644 --- a/Assignment 4/src/ca/cosc3p91/a4/util/Client.java +++ b/Assignment 4/src/ca/cosc3p91/a4/util/network/Client.java @@ -1,4 +1,4 @@ -package ca.cosc3p91.a4.util; +package ca.cosc3p91.a4.util.network; import ca.cosc3p91.a4.userinterface.GameDisplay; @@ -8,7 +8,7 @@ import java.net.DatagramSocket; import java.net.InetAddress; public class Client { - GameDisplay view = new GameDisplay(System.in); + GameDisplay view = new GameDisplay(); public Client(int port) throws IOException { DatagramSocket clientSocket = new DatagramSocket(); diff --git a/Assignment 4/src/ca/cosc3p91/a4/util/network/Message.java b/Assignment 4/src/ca/cosc3p91/a4/util/network/Message.java new file mode 100644 index 0000000..d5dc741 --- /dev/null +++ b/Assignment 4/src/ca/cosc3p91/a4/util/network/Message.java @@ -0,0 +1,57 @@ +package ca.cosc3p91.a4.util.network; + +import java.io.DataInputStream; +import java.io.DataOutputStream; + +public class Message { + + private final byte packetID; + private final long clientID, messageID; + + public Message(byte packetID, long clientID, long messageID){ + this.packetID = packetID; + this.clientID = clientID; + this.messageID = messageID; + } + + public byte getPacketID() { + return packetID; + } + + public long getClientID() { + return clientID; + } + + public long getMessageID() { + return messageID; + } + + public static class ReceivedMessage extends Message { + + private final DataInputStream reader; + + public ReceivedMessage(byte packetID, long clientID, long messageID, DataInputStream reader) { + super(packetID, clientID, messageID); + this.reader = reader; + } + + public DataInputStream getReader(){ + return reader; + } + } + + public static class SentMessage extends Message { + + private final DataOutputStream writer; + + public SentMessage(byte packetID, long clientID, long messageID, DataOutputStream writer) { + super(packetID, clientID, messageID); + this.writer = writer; + } + + public DataOutputStream getWriter(){ + return writer; + } + } + +} diff --git a/Assignment 4/src/ca/cosc3p91/a4/util/network/PacketTable.java b/Assignment 4/src/ca/cosc3p91/a4/util/network/PacketTable.java new file mode 100644 index 0000000..9a56875 --- /dev/null +++ b/Assignment 4/src/ca/cosc3p91/a4/util/network/PacketTable.java @@ -0,0 +1,20 @@ +package ca.cosc3p91.a4.util.network; + +public class PacketTable { + + // packetID -> byte defined in this file + // clientID -> long + // messageID -> long + + // messageHeader (packetID, clientID, messageID) + + // messageHeader, (clientID = 0 if connecting to server) + public static final byte CONNECT = 0x1; + // messageHeader + public static final byte DISCONNECT = 0x2; + // messageHeader + public static final byte ACK = 0x3; + // messageHeader, UTF8 String with length information (use DOS.writeUTF/DIS.readUTF) + public static final byte MESSAGE = 0x4; + +} diff --git a/Assignment 4/src/ca/cosc3p91/a4/util/Server.java b/Assignment 4/src/ca/cosc3p91/a4/util/network/Server.java similarity index 97% rename from Assignment 4/src/ca/cosc3p91/a4/util/Server.java rename to Assignment 4/src/ca/cosc3p91/a4/util/network/Server.java index e260403..ea4945b 100644 --- a/Assignment 4/src/ca/cosc3p91/a4/util/Server.java +++ b/Assignment 4/src/ca/cosc3p91/a4/util/network/Server.java @@ -1,6 +1,7 @@ -package ca.cosc3p91.a4.util; +package ca.cosc3p91.a4.util.network; import ca.cosc3p91.a4.game.GameEngine; +import ca.cosc3p91.a4.util.Time; import java.io.*; import java.net.*; @@ -139,6 +140,7 @@ public class Server implements Runnable { private final byte id; private final Time receiveTime; private final DataInputStream receive; + // ack should be on messages send to the client, which the client acks! private boolean ack = false; public ServerRequest(byte id, DataInputStream receive){