Skip to content
Snippets Groups Projects
Commit 2ca031be authored by Martin Hustoles's avatar Martin Hustoles
Browse files

removed checksum check

parent eaa07522
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -6,22 +6,14 @@ import java.util.zip.CRC32; ...@@ -6,22 +6,14 @@ import java.util.zip.CRC32;
import java.util.zip.Checksum; import java.util.zip.Checksum;
public class CPCommandMessage extends CPMsg{ public class CPCommandMessage extends CPMsg{
private static int messageIdCount = 0; //tracks the IDS
private int currentMessageId; private int currentMessageId;
private static final int MAX_ID = 65535; //maximum message id according to the protocol specification.
protected static final String HEADER = "command"; protected static final String HEADER = "command";
public CPCommandMessage() {} public CPCommandMessage() {}
public CPCommandMessage create(String cookie, String command, String message){ public CPCommandMessage create(String cookie, String command, String message, int messageIdCount){
//maybe throw an exception if the ID's run out?
if(messageIdCount < MAX_ID){
currentMessageId = messageIdCount; this.currentMessageId = messageIdCount;
messageIdCount++;
}
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
str.append(HEADER).append(' '); str.append(HEADER).append(' ');
...@@ -45,8 +37,4 @@ public class CPCommandMessage extends CPMsg{ ...@@ -45,8 +37,4 @@ public class CPCommandMessage extends CPMsg{
public int getMessageId(){ public int getMessageId(){
return currentMessageId; return currentMessageId;
} }
public static int getMaxMessageId() {
return MAX_ID;
}
} }
...@@ -38,7 +38,7 @@ public class CPCommandResponseMessage extends CPMsg{ ...@@ -38,7 +38,7 @@ public class CPCommandResponseMessage extends CPMsg{
//throws NumberFormatException, if the id couldnt get parsed for whatever reason //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 //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(); throw new IllegalMsgException();
} }
}catch (NumberFormatException e){ }catch (NumberFormatException e){
...@@ -58,10 +58,7 @@ public class CPCommandResponseMessage extends CPMsg{ ...@@ -58,10 +58,7 @@ public class CPCommandResponseMessage extends CPMsg{
} }
/* im not proud of the following code, but it sure works :) */
//reconstructes message //reconstructes message
int fieldIndex = 5;
int sumLen = 0; int sumLen = 0;
//calculating the actual length of the message //calculating the actual length of the message
...@@ -79,25 +76,9 @@ public class CPCommandResponseMessage extends CPMsg{ ...@@ -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)){ //comparing the CRC checksum
throw new IllegalMsgException(); System.out.println("Checksum: " + fields[fields.length - 1]);
}*/
//sets the received message as the data, so the client can print it on the console //sets the received message as the data, so the client can print it on the console
this.data = reconstructedMessage.toString(); this.data = reconstructedMessage.toString();
......
...@@ -16,6 +16,8 @@ public class CPProtocol extends Protocol { ...@@ -16,6 +16,8 @@ public class CPProtocol extends Protocol {
private static final int CP_HASHMAP_SIZE = 20; private static final int CP_HASHMAP_SIZE = 20;
private String cookie; private String cookie;
private int id; 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 PhyConfiguration PhyConfig;
private PhyProtocol PhyProto; private PhyProtocol PhyProto;
boolean isClient; boolean isClient;
...@@ -47,9 +49,16 @@ public class CPProtocol extends Protocol { ...@@ -47,9 +49,16 @@ public class CPProtocol extends Protocol {
//1.2: 1 a //1.2: 1 a
if(cookie != null) { 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 //1.2: 1 b
CPCommandMessage cmdMsg = new CPCommandMessage(); 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 //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(); this.id = cmdMsg.getMessageId();
...@@ -83,17 +92,27 @@ public class CPProtocol extends Protocol { ...@@ -83,17 +92,27 @@ public class CPProtocol extends Protocol {
//Discard Message //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; receving = false;
} }
//compare the id from the received message and the earlier sent message
if(((CPCommandResponseMessage) in).getMessageId() != this.id){
return null;
}
return in; return in;
} }
public void cookieProcesing(PhyConfiguration config, String cookie){
}
public void requestCookie(int count) throws IOException, IWProtocolException { public void requestCookie(int count) throws IOException, IWProtocolException {
if(count >= 3) if(count >= 3)
...@@ -118,4 +137,8 @@ public class CPProtocol extends Protocol { ...@@ -118,4 +137,8 @@ public class CPProtocol extends Protocol {
} }
this.cookie = resMsg.getData(); this.cookie = resMsg.getData();
} }
public static int getMaxId(){
return MAX_ID;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment