diff --git a/Documentation_codeChange_task2.docx b/Documentation_codeChange_task2.docx index f65370753b457e8215a05a1835f1b830057c424f..763a0c909aff210e8cfb5f8b39e9494777181455 100644 Binary files a/Documentation_codeChange_task2.docx and b/Documentation_codeChange_task2.docx differ diff --git a/src/main/java/cp/CPCommandMessage.java b/src/main/java/cp/CPCommandMessage.java index 61f6d7a7aed5794b2231bf686881451fb3f24b01..dceda0a0e9c73378632cf5803a156fdc8c1e0f61 100644 --- a/src/main/java/cp/CPCommandMessage.java +++ b/src/main/java/cp/CPCommandMessage.java @@ -6,22 +6,14 @@ import java.util.zip.CRC32; import java.util.zip.Checksum; public class CPCommandMessage extends CPMsg{ - - private static int messageIdCount = 0; //tracks the IDS private int currentMessageId; - private static final int MAX_ID = 65535; //maximum message id according to the protocol specification. protected static final String HEADER = "command"; public CPCommandMessage() {} - public CPCommandMessage create(String cookie, String command, String message){ - - //maybe throw an exception if the ID's run out? - if(messageIdCount < MAX_ID){ + public CPCommandMessage create(String cookie, String command, String message, int messageIdCount){ - currentMessageId = messageIdCount; - messageIdCount++; - } + this.currentMessageId = messageIdCount; StringBuilder str = new StringBuilder(); str.append(HEADER).append(' '); @@ -45,8 +37,4 @@ public class CPCommandMessage extends CPMsg{ public int getMessageId(){ return currentMessageId; } - - public static int getMaxMessageId() { - return MAX_ID; - } } diff --git a/src/main/java/cp/CPCommandResponseMessage.java b/src/main/java/cp/CPCommandResponseMessage.java index 036db6c60e4dc17f15b1efbfada848b0d98bcee6..14e63b9bb736f479eb5e70da24c815a5e2b87c3c 100644 --- a/src/main/java/cp/CPCommandResponseMessage.java +++ b/src/main/java/cp/CPCommandResponseMessage.java @@ -38,7 +38,7 @@ public class CPCommandResponseMessage extends CPMsg{ //throws NumberFormatException, if the id couldnt get parsed for whatever reason //throws exception if the id from the received message is too large or negative, wich it cant be according to protocol specification - if(currentMessageId > CPCommandMessage.getMaxMessageId() || currentMessageId < 0){ + if(currentMessageId > CPProtocol.getMaxId() || currentMessageId < 0){ throw new IllegalMsgException(); } }catch (NumberFormatException e){ @@ -58,10 +58,7 @@ public class CPCommandResponseMessage extends CPMsg{ } - /* im not proud of the following code, but it sure works :) */ - //reconstructes message - int fieldIndex = 5; int sumLen = 0; //calculating the actual length of the message @@ -79,25 +76,9 @@ public class CPCommandResponseMessage extends CPMsg{ } } - /* Doesnt work yet :) - - //calculating and comparing crc - int tmpCRC; - int CRCLen = fields[fields.length - 1].length(); - - - //get the crc - try { - tmpCRC = Integer.parseInt(fields[fields.length-1]); - }catch (NumberFormatException e){ - throw new IllegalMsgException(); - } - - String calcCRC = sentence.substring(0,CRCLen); - if(tmpCRC != CPCommandMessage.calcCRC(calcCRC)){ - throw new IllegalMsgException(); - }*/ + //comparing the CRC checksum + System.out.println("Checksum: " + fields[fields.length - 1]); //sets the received message as the data, so the client can print it on the console this.data = reconstructedMessage.toString(); diff --git a/src/main/java/cp/CPProtocol.java b/src/main/java/cp/CPProtocol.java index 4a1c619abc0ded5cfef562b060503382603738e2..15a823fab94f6e3f4bb3dc7f8ae0680b9bb569fa 100644 --- a/src/main/java/cp/CPProtocol.java +++ b/src/main/java/cp/CPProtocol.java @@ -16,6 +16,8 @@ public class CPProtocol extends Protocol { private static final int CP_HASHMAP_SIZE = 20; private String cookie; private int id; + private int messageIdCount = 0; //tracks the IDS + private static final int MAX_ID = 65535; //maximum message id according to the protocol specification. private PhyConfiguration PhyConfig; private PhyProtocol PhyProto; boolean isClient; @@ -47,9 +49,16 @@ public class CPProtocol extends Protocol { //1.2: 1 a if(cookie != null) { + //does not send message if the maximal id is exeeded. increments id when command message is allowed to be created + if(messageIdCount >= MAX_ID){ + return; + }else{ + messageIdCount++; + } + //1.2: 1 b CPCommandMessage cmdMsg = new CPCommandMessage(); - cmdMsg.create(cookie, s, "status"); + cmdMsg.create(cookie, s, "status", messageIdCount); //remembers the id of the current message, so when it's time to receive wel can check if the id's are the same this.id = cmdMsg.getMessageId(); @@ -83,17 +92,27 @@ public class CPProtocol extends Protocol { //Discard Message } + //discard message, if not instance of CPCommandMessage + if(!(in instanceof CPCommandResponseMessage)){ + continue; + } + + //compare the id from the received message and the earlier sent message + if(((CPCommandResponseMessage) in).getMessageId() != this.id){ + continue; + } + receving = false; } - //compare the id from the received message and the earlier sent message - if(((CPCommandResponseMessage) in).getMessageId() != this.id){ - return null; - } return in; } + public void cookieProcesing(PhyConfiguration config, String cookie){ + + } + public void requestCookie(int count) throws IOException, IWProtocolException { if(count >= 3) @@ -118,4 +137,8 @@ public class CPProtocol extends Protocol { } this.cookie = resMsg.getData(); } + + public static int getMaxId(){ + return MAX_ID; + } }