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

init project 2

parent 8a7227db
Branches
No related tags found
No related merge requests found
Showing
with 524 additions and 0 deletions
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="18" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
\ No newline at end of file
pom.xml 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.internetworking</groupId>
<artifactId>InternetworkingWS23</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.6.1</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>4.6.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.0</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package apps;
import cp.*;
import exceptions.*;
import phy.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
public class CPClient {
private static final String SERVER_NAME = "localhost";
public static void main(String[] args) {
// Each client needs to start on a unique UDP port provided by the user
if (args.length != 1) {
System.out.println("Provide an address identifier (int) from range [5000:65534]");
return;
}
var id = Integer.parseInt(args[0]);
if (id < 5000 || id > 65534) {
System.out.println("Invalid address identifier! Range [5000:65534]");
return;
}
// Set up the virtual link protocol
PhyProtocol phy = new PhyProtocol(id);
// Set up command protocol
CPProtocol cp = null;
try {
cp = new CPProtocol(InetAddress.getByName(SERVER_NAME), CPServer.SERVER_PORT, phy);
} catch (Exception e) {
e.printStackTrace();
}
// Read data from user to send to server
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
boolean eof = false;
while (!eof) {
try {
String sentence = null;
System.out.println("Command: ");
sentence = inFromUser.readLine();
// Currently only these two commands are supported by the specification
if(!(sentence.equals("status") || sentence.startsWith("print")))
continue;
cp.send(sentence.trim(), null);
System.out.println("Command sent to server ... wating for response");
String answer = cp.receive().getData();
System.out.println(answer);
} catch (IllegalCommandException e) {
System.out.println("Only these two commands are supported: status, print \"text\"");
} catch (IWProtocolException | IOException e) {
e.printStackTrace();
}
}
}
}
package apps;
import core.Msg;
import cp.CPProtocol;
import exceptions.IWProtocolException;
import phy.PhyProtocol;
import java.io.IOException;
public class CPServer {
protected static final int SERVER_PORT = 3027;
public static void main(String[] args) {
// Set up the virtual link protocol
PhyProtocol phy = new PhyProtocol(SERVER_PORT);
// Set up command protocol
CPProtocol cp = null;
try {
cp = new CPProtocol(phy);
} catch (Exception e) {
e.printStackTrace();
}
// Start server processing
boolean eof = false;
while (!eof) {
try {
Msg msg = cp.receive();
String sentence = msg.getData().trim();
System.out.println("Received message: " + sentence);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
\ No newline at end of file
package core;
/*
* Abstract configuration class
* nextLowerProtocol attribute is currently unused (ignore)
*/
public class Configuration {
protected Protocol nextLowerProtocol;
public Configuration(Protocol proto) {
this.nextLowerProtocol = proto;
}
public Protocol getNextLowerProtocol() {
return nextLowerProtocol;
}
private void setNextLowerProtocol(Protocol nextLowerProtocol) {
this.nextLowerProtocol = nextLowerProtocol;
}
}
package core;
import exceptions.IWProtocolException;
/*
* Msg base class (abstract)
*/
public abstract class Msg {
protected String data;
protected byte[] dataBytes;
protected Configuration config;
public byte[] getDataBytes() {
return this.dataBytes;
}
public int getLength() {
return this.dataBytes.length;
}
public String getData() {
return this.data;
}
public void setData(String data) {
this.data = data;
}
public Configuration getConfiguration() {
return this.config;
}
public void setConfiguration(Configuration c) {this.config = c;}
protected abstract void create(String sentence);
protected abstract Msg parse(String sentence) throws IWProtocolException;
public void printDataBytes() {
String msgString = new String(this.dataBytes);
System.out.println(msgString);
}
}
package core;
import java.io.IOException;
import exceptions.IWProtocolException;
/*
* Protocol base class (abstract)
*/
public abstract class Protocol {
public enum proto_id {
PHY, APP, SLP, CP
}
public abstract void send(String s, Configuration config) throws IOException, IWProtocolException;
public abstract Msg receive() throws IOException, IWProtocolException;
}
package cp;
import core.Msg;
import exceptions.IllegalMsgException;
class CPCookieRequestMsg extends CPMsg {
protected static final String CP_CREQ_HEADER = "creq";
/*
* Create cookie request message.
* The cp header is prepended in the super-class.
*/
@Override
protected void create(String data) {
// prepend reg header
data = CP_CREQ_HEADER;
// super class prepends slp header
super.create(data);
}
@Override
protected Msg parse(String sentence) throws IllegalMsgException {
if (!sentence.startsWith(CP_CREQ_HEADER)) {
throw new IllegalMsgException();
}
return this;
}
}
\ No newline at end of file
package cp;
import core.Msg;
import exceptions.IllegalMsgException;
class CPCookieResponseMsg extends CPMsg {
protected static final String CP_CRES_HEADER = "cres";
private boolean success;
protected CPCookieResponseMsg() {
}
protected CPCookieResponseMsg(boolean s) {
this.success = s;
}
protected boolean getSuccess() {return this.success;}
/*
* Create cookie request message.
* The cp header is prepended in the super-class.
*/
@Override
protected void create(String data) {
if (this.success) {
// prepend cres header
data = CP_CRES_HEADER + " ACK " + data;
} else {
data = CP_CRES_HEADER + " NAK " + data;
}
// super class prepends slp header
super.create(data);
}
protected Msg parse(String sentence) throws IllegalMsgException {
if (!sentence.startsWith(CP_CRES_HEADER)) {
throw new IllegalMsgException();
}
String[] parts = sentence.split("\\s+", 3);
if(parts[1].equals("ACK"))
this.success = true;
else
this.success = false;
this.data = parts[2];
return this;
}
}
package cp;
import core.Msg;
import exceptions.IWProtocolException;
import exceptions.IllegalMsgException;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
class CPMsg extends Msg {
protected static final String CP_HEADER = "cp";
@Override
protected void create(String sentence) {
this.data = sentence;
data = CP_HEADER + " " + sentence;
this.dataBytes = data.getBytes();
}
@Override
protected Msg parse(String sentence) throws IWProtocolException {
CPMsg parsedMsg;
if(!sentence.startsWith(CP_HEADER))
throw new IllegalMsgException();
String[] parts = sentence.split("\\s+", 2);
if(parts[1].startsWith(CPCookieRequestMsg.CP_CREQ_HEADER)) {
parsedMsg = new CPCookieRequestMsg();
} else if(parts[1].startsWith(CPCookieResponseMsg.CP_CRES_HEADER)) {
parsedMsg = new CPCookieResponseMsg();
} else
throw new IllegalMsgException();
parsedMsg = (CPMsg) parsedMsg.parse(parts[1]);
return parsedMsg;
}
}
package cp;
import core.*;
import exceptions.*;
import phy.*;
import java.io.IOException;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Random;
public class CPProtocol extends Protocol {
private static final int CP_TIMEOUT = 2000;
private static final int CP_HASHMAP_SIZE = 20;
private String cookie;
private int id;
private PhyConfiguration PhyConfig;
private PhyProtocol PhyProto;
boolean isClient;
HashMap<PhyConfiguration, Integer> cookieMap;
Random rnd;
// Constructor for clients
public CPProtocol(InetAddress rname, int rp, PhyProtocol phyP) throws UnknownHostException {
this.PhyConfig = new PhyConfiguration(rname, rp, proto_id.CP);
this.PhyProto = phyP;
this.isClient = true;
}
// Constructor for servers
public CPProtocol(PhyProtocol phyP) {
this.PhyProto = phyP;
this.isClient = false;
this.cookieMap = new HashMap<>();
this.rnd = new Random();
}
@Override
public void send(String s, Configuration config) throws IOException, IWProtocolException {
if (cookie == null) {
// Request a new cookie from server
// Either updates the cookie attribute or returns with an exception
requestCookie(0);
}
}
@Override
public Msg receive() throws IOException {
return null;
}
public void requestCookie(int count) throws IOException, IWProtocolException {
if(count >= 3)
throw new CookieRequestException();
CPCookieRequestMsg reqMsg = new CPCookieRequestMsg();
reqMsg.create(null);
this.PhyProto.send(new String (reqMsg.getDataBytes()), this.PhyConfig);
Msg resMsg = new CPMsg();
try {
Msg in = this.PhyProto.receive(CP_TIMEOUT);
if (((PhyConfiguration)in.getConfiguration()).getPid() != proto_id.CP)
throw new IllegalMsgException();
resMsg = ((CPMsg) resMsg).parse(in.getData());
} catch (SocketTimeoutException e) {
requestCookie(count+1);
}
if(resMsg instanceof CPCookieResponseMsg && !((CPCookieResponseMsg) resMsg).getSuccess()) {
throw new CookieRequestException();
}
this.cookie = resMsg.getData();
}
}
package exceptions;
public class BadChecksumException extends IWProtocolException {
/**
*
*/
private static final long serialVersionUID = -6481772554012497583L;
}
package exceptions;
public class CookieRequestException extends IWProtocolException {
}
package exceptions;
public abstract class IWProtocolException extends Exception {
/**
*
*/
private static final long serialVersionUID = -7002466204521265834L;
}
package exceptions;
import exceptions.IWProtocolException;
public class IllegalAddrException extends IWProtocolException {
/**
*
*/
private static final long serialVersionUID = -2112808760774454967L;
}
package exceptions;
public class IllegalCommandException extends IWProtocolException {
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment