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

implemented task 2 completly

parent 0235f806
No related branches found
No related tags found
No related merge requests found
No preview for this file type
File added
File deleted
File added
......@@ -7,6 +7,7 @@ import exceptions.IllegalMsgException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.zip.CRC32;
public class CPCommandResponseMessage extends CPMsg{
......@@ -77,7 +78,20 @@ public class CPCommandResponseMessage extends CPMsg{
}
//comparing the CRC checksum
// Compare the CRC checksum
String receivedChecksumStr = fields[fields.length - 1];
long receivedChecksum;
try {
receivedChecksum = Long.parseLong(receivedChecksumStr, 16);
} catch (NumberFormatException e) {
throw new IllegalMsgException();
}
long calculatedChecksum = this.calcCRC(reconstructedMessage.toString());
if (calculatedChecksum != receivedChecksum) {
throw new IllegalMsgException();
}
//sets the received message as the data, so the client can print it on the console
......
......@@ -2,18 +2,24 @@ package cp;
public class CPCookie {
private int cookie;
private long createdAt;
CPCookie(long createdAt, int cookie){
this.createdAt = createdAt;
this.cookie = cookie;
//error Messages for Cookie Response Message
public static final String cookieErrorLimit = "COOKIE_ERR_LIMIT_REACHED";
public static final String cookieErrorOldCookie = "COOKIE_ERR_OLD_COOKIE_VALID";
public static final String cookieSuccess = "COOKIE_ERR_LIMIT_REACHED";
private int cookieValue; //"random" Number
private long createdAt; //time of Creation as "Unix time"
private static final long MAX_LIFETIME_SECONDS = 600; //600 Seconds, according to protocol specification
CPCookie(){
this.createdAt = System.currentTimeMillis();
this.cookieValue = 1;
}
public int getCookie() {
return cookie;
public int getCookieValue() {
return cookieValue;
}
public long getCreatedAt() {
return createdAt;
public boolean isStillValid(){
return System.currentTimeMillis() - createdAt <= 600; //returns true, if the cookie has lived 600 Seconds or less
}
}
......@@ -21,7 +21,7 @@ public class CPProtocol extends Protocol {
private PhyConfiguration PhyConfig;
private PhyProtocol PhyProto;
boolean isClient;
HashMap<PhyConfiguration, Integer> cookieMap;
HashMap<Integer, CPCookie> cookieMap;
Random rnd;
// Constructor for clients
......@@ -39,6 +39,10 @@ public class CPProtocol extends Protocol {
}
@Override
public void send(String s, Configuration config) throws IOException, IWProtocolException {
this.send(s, "", config);
}
public void send(String s, String message, Configuration config) throws IOException, IWProtocolException {
if (cookie == null) {
// Request a new cookie from server
......@@ -59,7 +63,7 @@ public class CPProtocol extends Protocol {
//1.2: 1 b
CPCommandMessage cmdMsg = new CPCommandMessage();
cmdMsg.create(cookie, s, "", messageIdCount);
cmdMsg.create(cookie, s, message, 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();
......@@ -109,14 +113,31 @@ public class CPProtocol extends Protocol {
return in;
}
public void cookieProcesing(PhyConfiguration config, String cookie){
public void cookieProcesing(PhyConfiguration config, CPCookie cookie) throws IWProtocolException, IOException {
if(cookieMap.size() >= CP_HASHMAP_SIZE){
System.out.println("Too many cookies.");
new CPServerCookieResponseMsg(false,"Cookie limit reached", cookie).sendResponse(this.PhyProto, config);
return;
}
CPCookie recievedCookie = cookieMap.get(config.hashCode()); //is null when there is no cookie currently saved
if(recievedCookie != null){ //if the cookie still exists
if(recievedCookie.isStillValid()){
//tells the client that an old cookie has not expired
new CPServerCookieResponseMsg(false,"Old Cookie is still valid", cookie).sendResponse(this.PhyProto, config);
}else{
cookieMap.remove(config.hashCode()); //removes the Cookie, because it has expired
}
return;
}
cookieMap.put(config, config.hashCode(cookie));
cookieMap.put(config.hashCode(), cookie);
new CPServerCookieResponseMsg(true,"Cookie is valid", cookie).sendResponse(this.PhyProto, config);
}
......
package cp;
import exceptions.IWProtocolException;
import phy.PhyConfiguration;
import phy.PhyProtocol;
import java.io.IOException;
public class CPServerCookieResponseMsg extends CPCookieResponseMsg{
CPServerCookieResponseMsg(boolean success, String responseMessage, CPCookie cookie){
super(success);
if(success){
super.create(String.valueOf(cookie.getCookieValue()));
}else {
super.create(responseMessage);
}
}
public void sendResponse(PhyProtocol phy, PhyConfiguration config) throws IWProtocolException, IOException {
phy.send(super.data, config);
}
}
......@@ -31,23 +31,17 @@ public class PhyConfiguration extends Configuration{
public Protocol.proto_id getPid() {return this.pid;}
public boolean equals(PhyConfiguration otherConfig ){
return false;
}
public int hashCode(String cookie){
return System.identityHashCode(cookie);
}
//generated using Alt+Insert
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PhyConfiguration that)) return false;
return remotePort == that.remotePort && isClient == that.isClient && Objects.equals(remoteIPAddress, that.remoteIPAddress) && pid == that.pid;
if(o instanceof PhyConfiguration phy1){ //no "null check" needed, because instanceof already covers that
return phy1.hashCode() == this.hashCode(); //if the hash code is the same, the objects are the same
}
return false;
}
//generated using Alt+Insert
@Override
public int hashCode() {
return Objects.hash(remotePort, remoteIPAddress, pid, isClient);
......
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