diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b81b018ad684f3a35fee301741b2734c8f4..0000000000000000000000000000000000000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index d2c8febd59ef62ba4bca79c36bb69231091d7b47..0000000000000000000000000000000000000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,13 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="CompilerConfiguration"> - <annotationProcessing> - <profile name="Maven default annotation processors profile" enabled="true"> - <sourceOutputDir name="target/generated-sources/annotations" /> - <sourceTestOutputDir name="target/generated-test-sources/test-annotations" /> - <outputRelativeToContentRoot value="true" /> - <module name="Internetworking_Protocol" /> - </profile> - </annotationProcessing> - </component> -</project> \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index 6e25cb4152e51e7034791a972315ea629e56e423..0000000000000000000000000000000000000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,7 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="Encoding"> - <file url="file://$PROJECT_DIR$/Internetworking_Protocol/src/main/java" charset="UTF-8" /> - <file url="file://$PROJECT_DIR$/Internetworking_Protocol/src/main/resources" charset="UTF-8" /> - </component> -</project> \ No newline at end of file diff --git a/.idea/internetworking_ws23.iml b/.idea/internetworking_ws23.iml deleted file mode 100644 index d6ebd4805981b8400db3e3291c74a743fef9a824..0000000000000000000000000000000000000000 --- a/.idea/internetworking_ws23.iml +++ /dev/null @@ -1,9 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<module type="JAVA_MODULE" version="4"> - <component name="NewModuleRootManager" inherit-compiler-output="true"> - <exclude-output /> - <content url="file://$MODULE_DIR$" /> - <orderEntry type="inheritedJdk" /> - <orderEntry type="sourceFolder" forTests="false" /> - </component> -</module> \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml deleted file mode 100644 index 712ab9d985c20018a0c97b93d2148ac1ffe588a5..0000000000000000000000000000000000000000 --- a/.idea/jarRepositories.xml +++ /dev/null @@ -1,20 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="RemoteRepositoriesConfiguration"> - <remote-repository> - <option name="id" value="central" /> - <option name="name" value="Central Repository" /> - <option name="url" value="https://repo.maven.apache.org/maven2" /> - </remote-repository> - <remote-repository> - <option name="id" value="central" /> - <option name="name" value="Maven Central repository" /> - <option name="url" value="https://repo1.maven.org/maven2" /> - </remote-repository> - <remote-repository> - <option name="id" value="jboss.community" /> - <option name="name" value="JBoss Community repository" /> - <option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" /> - </remote-repository> - </component> -</project> \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index b2ffe19e9dce218a9152463531669580b540ebea..0000000000000000000000000000000000000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,14 +0,0 @@ -<?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$/Internetworking_Protocol/pom.xml" /> - </list> - </option> - </component> - <component name="ProjectRootManager" version="2" languageLevel="JDK_20" default="true" project-jdk-name="openjdk-20" project-jdk-type="JavaSDK"> - <output url="file://$PROJECT_DIR$/out" /> - </component> -</project> \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index d36097df2131a3940bfae4f1bce9afa16668e131..0000000000000000000000000000000000000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="ProjectModuleManager"> - <modules> - <module fileurl="file://$PROJECT_DIR$/.idea/internetworking_ws23.iml" filepath="$PROJECT_DIR$/.idea/internetworking_ws23.iml" /> - </modules> - </component> -</project> \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1ddfbbc029bcab630581847471d7f238ec53..0000000000000000000000000000000000000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ -<?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 diff --git a/Internetworking_Protocol/.gitignore b/Internetworking_Protocol/.gitignore deleted file mode 100644 index 5ff6309b7199129c1afe4f4ec1906e640bec48c6..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/.gitignore +++ /dev/null @@ -1,38 +0,0 @@ -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 diff --git a/Internetworking_Protocol/.idea/.gitignore b/Internetworking_Protocol/.idea/.gitignore deleted file mode 100644 index 13566b81b018ad684f3a35fee301741b2734c8f4..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/Internetworking_Protocol/.idea/encodings.xml b/Internetworking_Protocol/.idea/encodings.xml deleted file mode 100644 index aa00ffab7828f4818589659c804ec2cfd99baed3..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/.idea/encodings.xml +++ /dev/null @@ -1,7 +0,0 @@ -<?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 diff --git a/Internetworking_Protocol/.idea/misc.xml b/Internetworking_Protocol/.idea/misc.xml deleted file mode 100644 index 1cecbe56e0fd50dc356dc41d831fcab6106c24fb..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/.idea/misc.xml +++ /dev/null @@ -1,14 +0,0 @@ -<?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_20" default="true" project-jdk-name="18" project-jdk-type="JavaSDK"> - <output url="file://$PROJECT_DIR$/out" /> - </component> -</project> \ No newline at end of file diff --git a/Internetworking_Protocol/pom.xml b/Internetworking_Protocol/pom.xml deleted file mode 100644 index 42d639519d754044c48c064cc5260ce368282e20..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/pom.xml +++ /dev/null @@ -1,38 +0,0 @@ -<?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.childmolester</groupId> - <artifactId>Internetworking_Protocol</artifactId> - <version>1.0-SNAPSHOT</version> - - <properties> - <maven.compiler.source>20</maven.compiler.source> - <maven.compiler.target>20</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 diff --git a/Internetworking_Protocol/src/main/java/apps/CPClient.java b/Internetworking_Protocol/src/main/java/apps/CPClient.java deleted file mode 100644 index 6a1d55c5482bda8a72e082123648b9e86db9160c..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/apps/CPClient.java +++ /dev/null @@ -1,62 +0,0 @@ -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(); - } - } - } -} diff --git a/Internetworking_Protocol/src/main/java/apps/CPServer.java b/Internetworking_Protocol/src/main/java/apps/CPServer.java deleted file mode 100644 index 071a8eddca62f9ce581a813fd05d20532db66703..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/apps/CPServer.java +++ /dev/null @@ -1,37 +0,0 @@ -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 diff --git a/Internetworking_Protocol/src/main/java/core/Configuration.java b/Internetworking_Protocol/src/main/java/core/Configuration.java deleted file mode 100644 index 59ee4581d22bf200299ddd2e32063dbdbe6cbfb3..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/core/Configuration.java +++ /dev/null @@ -1,21 +0,0 @@ -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; - } -} diff --git a/Internetworking_Protocol/src/main/java/core/Msg.java b/Internetworking_Protocol/src/main/java/core/Msg.java deleted file mode 100644 index bf3be7213d97e3569b414bdf325274513100320d..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/core/Msg.java +++ /dev/null @@ -1,42 +0,0 @@ -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); - } -} diff --git a/Internetworking_Protocol/src/main/java/core/Protocol.java b/Internetworking_Protocol/src/main/java/core/Protocol.java deleted file mode 100644 index 54be4ee55718cd3d5250a191fa54b45d8f715c13..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/core/Protocol.java +++ /dev/null @@ -1,18 +0,0 @@ -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; - -} diff --git a/Internetworking_Protocol/src/main/java/cp/CPCookieRequestMsg.java b/Internetworking_Protocol/src/main/java/cp/CPCookieRequestMsg.java deleted file mode 100644 index 79a9e5236cd48b9e691f115755f4900ec98a1652..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/cp/CPCookieRequestMsg.java +++ /dev/null @@ -1,28 +0,0 @@ -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 diff --git a/Internetworking_Protocol/src/main/java/cp/CPCookieResponseMsg.java b/Internetworking_Protocol/src/main/java/cp/CPCookieResponseMsg.java deleted file mode 100644 index faeed5f46f10f3e4dc0e762f48dfabf16df8383c..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/cp/CPCookieResponseMsg.java +++ /dev/null @@ -1,50 +0,0 @@ -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; - } - -} diff --git a/Internetworking_Protocol/src/main/java/cp/CPMsg.java b/Internetworking_Protocol/src/main/java/cp/CPMsg.java deleted file mode 100644 index 150c8e7519ebc89de10608402817658bd7b679f9..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/cp/CPMsg.java +++ /dev/null @@ -1,34 +0,0 @@ -package cp; - -import core.Msg; -import exceptions.IWProtocolException; -import exceptions.IllegalMsgException; - -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; - } - -} diff --git a/Internetworking_Protocol/src/main/java/cp/CPProtocol.java b/Internetworking_Protocol/src/main/java/cp/CPProtocol.java deleted file mode 100644 index 66e0f5e9622c0aa169e36a899b2adcc53d71ae02..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/cp/CPProtocol.java +++ /dev/null @@ -1,76 +0,0 @@ -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(); - } -} diff --git a/Internetworking_Protocol/src/main/java/examples/MonolithicEchoPhyClient.java b/Internetworking_Protocol/src/main/java/examples/MonolithicEchoPhyClient.java deleted file mode 100644 index 98c905d192458d9ac53db440a7f39a657f3f7b0a..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/examples/MonolithicEchoPhyClient.java +++ /dev/null @@ -1,45 +0,0 @@ -package examples; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetAddress; - -public class MonolithicEchoPhyClient { - - public static void main(String[] args) { - BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); - - try (DatagramSocket datagramSocket = new DatagramSocket()) { - // Read String from keyboard - System.out.print("Your message for the server: "); - String sentence = inFromUser.readLine(); - - // Add protocol header - sentence = "phy 3 " + sentence; - - // Send packet - byte[] buffer = sentence.getBytes(); - DatagramPacket packet = new DatagramPacket( - buffer, buffer.length, InetAddress.getByName("localhost"), 12000); - datagramSocket.send(packet); - - // Receive a packet - byte[] receiveData = new byte[2048]; - DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); - datagramSocket.receive(receivePacket); - String message = new String(receivePacket.getData()); - - // Strip protocol header - message = message.substring("phy 3 ".length()).trim(); - System.out.println("Received message: " + message); - - // SocketException and UnknownHostException are subclasses of IOException - } catch (IOException e) { - e.printStackTrace(); - } - } - -} diff --git a/Internetworking_Protocol/src/main/java/examples/MonolithicEchoPhyServer.java b/Internetworking_Protocol/src/main/java/examples/MonolithicEchoPhyServer.java deleted file mode 100644 index d1d72f897cb590791828ceea32ef01be1ee8b565..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/examples/MonolithicEchoPhyServer.java +++ /dev/null @@ -1,39 +0,0 @@ -package examples; - -import java.net.DatagramPacket; -import java.net.DatagramSocket; - -public class MonolithicEchoPhyServer { - public static void main(String[] args) { - try (DatagramSocket datagramSocket = new DatagramSocket(12000)) { - DatagramPacket receivePacket; - while (true) { - // Receive packet - byte[] receiveData = new byte[2048]; - receivePacket = new DatagramPacket(receiveData, receiveData.length); - datagramSocket.receive(receivePacket); - - String message = new String(receivePacket.getData()); - - // Strip protocol header - String sentence = message.substring("phy 3 ".length()).trim(); - System.out.println("Received message: " + sentence); - - // Process string - sentence = sentence.toUpperCase(); - - // Add protocol header - sentence = "phy 3 " + sentence; - byte[] buffer = sentence.getBytes(); - - // Send packet - DatagramPacket packet = new DatagramPacket( - buffer, buffer.length, receivePacket.getAddress(), receivePacket.getPort()); - datagramSocket.send(packet); - - } - } catch (Exception e) { - e.printStackTrace(); - } - } -} diff --git a/Internetworking_Protocol/src/main/java/examples/PhyAllInOne.java b/Internetworking_Protocol/src/main/java/examples/PhyAllInOne.java deleted file mode 100644 index a8f0f3f2c6c5159d8f080d504210b1e6641e8c85..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/examples/PhyAllInOne.java +++ /dev/null @@ -1,87 +0,0 @@ -package examples; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.InetAddress; - -import core.Msg; -import core.Protocol; -import exceptions.IWProtocolException; -import phy.PhyProtocol; -import phy.PhyConfiguration; - -public class PhyAllInOne implements Runnable { - private static final String SERVERNAME = "localhost"; - PhyProtocol phy; - - public PhyAllInOne(PhyProtocol p) { - this.phy = p; - } - - public static void main(String[] args) { - if (args.length != 1) { - System.out.println("Provide an UDP port number"); - return; - } - int phyPortNumber = Integer.parseInt(args[0]); - - // Set up the virtual link protocol - PhyProtocol phy = new PhyProtocol(phyPortNumber); - - // Start the receiver side of the app - PhyAllInOne recv = new PhyAllInOne(phy); - Thread recvT = new Thread(recv); - recvT.start(); - - // Read data from user to send to client - BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); - System.out.println("Message Destination: "); - String sentence; - try { - sentence = inFromUser.readLine(); - sentence = sentence.trim(); - } catch (IOException | NullPointerException e1) { - System.out.println("Incorrect switch port! Aborting ..."); - return; - } - int dst = Integer.parseInt(sentence); - - boolean eof = false; - while (!eof) { - try { - System.out.println("Message: "); - sentence = null; - sentence = inFromUser.readLine(); - if (sentence.equalsIgnoreCase("eof")) - eof = true; - PhyConfiguration config = new PhyConfiguration(InetAddress.getByName(SERVERNAME), dst, Protocol.proto_id.APP); - phy.send(sentence.trim(), config); - } catch (IWProtocolException | IOException | NullPointerException e) { - e.printStackTrace(); - } - } - - } - - // Receiver thread - @Override - public void run() { - boolean eof = false; - - while (!eof) { - try { - Msg msg = this.phy.receive(); - String sentence = msg.getData().trim(); - if (sentence.equalsIgnoreCase("eof")) - eof = true; - else - System.out.println("Received message: " + sentence); - } catch (IOException e) { - e.printStackTrace(); - } - } - - } - -} diff --git a/Internetworking_Protocol/src/main/java/examples/PhyPingClient.java b/Internetworking_Protocol/src/main/java/examples/PhyPingClient.java deleted file mode 100644 index 187a3e2e11450980bac42e78cd89b2000ea2d2f6..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/examples/PhyPingClient.java +++ /dev/null @@ -1,42 +0,0 @@ -package examples; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.UnknownHostException; - -import core.Protocol; -import exceptions.IWProtocolException; -import phy.PhyConfiguration; -import phy.PhyProtocol; - -/* - * Client using the phy protocol - */ -public class PhyPingClient { - - private static final String SERVERNAME = "localhost"; - public static final int CLIENTPORT = 4444; - - public static void main(String[] args) { - // Create a new phy protocol instance with defined UDP port - PhyProtocol proto = new PhyProtocol(CLIENTPORT); - - PhyConfiguration config; - try { - // create a new phy configuration object - // getByName might throw exception - config = new PhyConfiguration(InetAddress.getByName(SERVERNAME), PhyPingServer.SERVERPORT, Protocol.proto_id.APP); - } catch (UnknownHostException e1) { - e1.printStackTrace(); - return; - } - - try { - proto.ping(config); - } catch (IWProtocolException | IOException e) { - e.printStackTrace(); - } - - } - -} diff --git a/Internetworking_Protocol/src/main/java/examples/PhyPingServer.java b/Internetworking_Protocol/src/main/java/examples/PhyPingServer.java deleted file mode 100644 index b2bda6a23b2704f0eb1112d5d873f71ec2e5545d..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/examples/PhyPingServer.java +++ /dev/null @@ -1,31 +0,0 @@ -package examples; - -import java.io.IOException; - -import core.Msg; -import exceptions.IWProtocolException; -import phy.PhyPingMsg; -import phy.PhyProtocol; - -public class PhyPingServer { - protected static final int SERVERPORT = 4455; - - public static void main(String[] args) throws IWProtocolException { - // create phy protocol instance - PhyProtocol phy = new PhyProtocol(SERVERPORT); - int count=0; - - while (count < 2) { - try { - // read the message received via the phy protocol - Msg msg = phy.receive(); - if(msg instanceof PhyPingMsg) { - count = ((PhyPingMsg) msg).getCount(); - System.out.println("Received ping message: " + count); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - } -} diff --git a/Internetworking_Protocol/src/main/java/examples/UDPAllInOne.java b/Internetworking_Protocol/src/main/java/examples/UDPAllInOne.java deleted file mode 100644 index a45e3125c819f68449f6d5245a772bd64c5a0651..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/examples/UDPAllInOne.java +++ /dev/null @@ -1,32 +0,0 @@ -package examples; - -import java.net.DatagramSocket; - -public class UDPAllInOne { - - public static void main(String[] args) throws Exception { - if (args.length != 1) { - System.out.println("Provide an UDP port number"); - return; - } - int port = Integer.parseInt(args[0]); - - // Create socket on the port provided - DatagramSocket recvSocket = new DatagramSocket(port); - - // Start the receiver side of the app; share socket with this thread - // Create an object to run as a new activity - UDPServerRunnable recv = new UDPServerRunnable(recvSocket); - // Create a new thread object in which to embed the object - Thread recvT = new Thread(recv); - // Start the activity -> this will invoke the run-method of the Runnable object - recvT.start(); - System.out.println("Receiver started on port: " + port); - - // Create sender thread -> the sender will create its own socket for sending - UDPClientRunnable send = new UDPClientRunnable(); - Thread sendT = new Thread(send); - sendT.start(); - } - -} diff --git a/Internetworking_Protocol/src/main/java/examples/UDPClientRunnable.java b/Internetworking_Protocol/src/main/java/examples/UDPClientRunnable.java deleted file mode 100644 index 733549c592753f8231a8290cb8bf2f36dbb1bec3..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/examples/UDPClientRunnable.java +++ /dev/null @@ -1,46 +0,0 @@ -package examples; - -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetAddress; -import java.net.SocketException; - -public class UDPClientRunnable implements Runnable { - private final DatagramSocket clientSocket; - - public UDPClientRunnable() throws SocketException { - this.clientSocket = new DatagramSocket(); - } - - @Override - public void run() { - // Read from keyboard - java.io.BufferedReader inFromUser = - new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); - String portString; - String sentence; - int port; - while (true) { - try { - System.out.println("Recipient: "); - portString = inFromUser.readLine(); - port = Integer.parseInt(portString.trim()); - System.out.println("Message: "); - sentence = inFromUser.readLine(); - - // Convert String to byte array for sending - byte[] sendData; - sendData = sentence.getBytes(); - System.out.println("Send to " + port + ": " + sentence); - - // Create datagram object - DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, - InetAddress.getByName("localhost"), port); - clientSocket.send(sendPacket); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - -} diff --git a/Internetworking_Protocol/src/main/java/examples/UDPEchoClient.java b/Internetworking_Protocol/src/main/java/examples/UDPEchoClient.java deleted file mode 100644 index d91b950a311f8cdbdd66ebac0f2f8aa6713b8832..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/examples/UDPEchoClient.java +++ /dev/null @@ -1,51 +0,0 @@ -package examples; - -import java.io.IOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetAddress; - -public class UDPEchoClient { - private final static String host = "localhost"; - - public static void main(String[] args) throws Exception { - // convert URI to IP address -> exception if DNS lookup fails - InetAddress IPAddress = InetAddress.getByName(host); - // Create socket object for sending and receiving data - DatagramSocket clientSocket = new DatagramSocket(); - - // Read from keyboard - java.io.BufferedReader inFromUser = - new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); - System.out.println("Message to server: "); - String sentence = ""; - try { - sentence = inFromUser.readLine(); - } catch (IOException e) { - e.printStackTrace(); - } - - // Convert String to byte array for sending - byte[] sendData; - sendData = sentence.getBytes(); - System.out.println("Send to server: " + sentence); - // Create datagram object - DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, UDPEchoServer.ECHOSERVERPORT); - try { - clientSocket.send(sendPacket); - // object that will contain the incoming message - byte[] receiveData = new byte[1024]; - DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); - // server process blocks on receive method until a message comes in - clientSocket.receive(receivePacket); - // convert byte array to String object for easier manipulation - String reply = new String(receivePacket.getData()).trim(); - System.out.println("From Server: " + reply); - } catch (IOException e) { - e.printStackTrace(); - } finally { - clientSocket.close(); - } - } - -} diff --git a/Internetworking_Protocol/src/main/java/examples/UDPEchoServer.java b/Internetworking_Protocol/src/main/java/examples/UDPEchoServer.java deleted file mode 100644 index 6e308c2e10e49ef558e3f0d3dc159ba8d9d54bb9..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/examples/UDPEchoServer.java +++ /dev/null @@ -1,44 +0,0 @@ -package examples; - -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.InetAddress; - -public class UDPEchoServer { - public final static int ECHOSERVERPORT = 9876; - - public static void main(String[] args) throws Exception { - // Open socket on UDP port 9876 - try (DatagramSocket serverSocket = new DatagramSocket(ECHOSERVERPORT)) { - String sentence = ""; - - // The server should run indefinitely - while (!sentence.equals("eof")) { - // Socket expects byte arrays for sending and receiving data - byte[] receiveData = new byte[1024]; - byte[] sendData; - - // object that will contain the incoming message - DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); - // server process blocks on receive method until a message comes in - serverSocket.receive(receivePacket); - - // convert byte array to String object for easier manipulation - sentence = new String(receivePacket.getData()).trim(); - System.out.println("RECEIVED: " + sentence); - - // Get sender's meta data from the object (IP address and UDP port number) - InetAddress IPAddress = receivePacket.getAddress(); - int port = receivePacket.getPort(); - - // Manipulate message and convert back to byte array - String capitalizedSentence = sentence.toUpperCase(); - sendData = capitalizedSentence.getBytes(); - - // create object for sending modified message back to sender - DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); - serverSocket.send(sendPacket); - } - } - } -} diff --git a/Internetworking_Protocol/src/main/java/examples/UDPServerRunnable.java b/Internetworking_Protocol/src/main/java/examples/UDPServerRunnable.java deleted file mode 100644 index d97ccda9774e89e8d8f3909b6ae9183221e5a3cf..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/examples/UDPServerRunnable.java +++ /dev/null @@ -1,38 +0,0 @@ -package examples; - -import java.net.DatagramPacket; -import java.net.DatagramSocket; - -public class UDPServerRunnable implements Runnable { - private final DatagramSocket serverSocket; - - public UDPServerRunnable(DatagramSocket sock) { - this.serverSocket = sock; - } - - @Override - public void run() { - // The server should run indefinitely - while (true) { - // Socket expects byte arrays for sending and receiving data - byte[] receiveData = new byte[1024]; - - // object that will contain the incoming message - DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); - - try { - // server process blocks on receive method until a message comes in - this.serverSocket.receive(receivePacket); - - // convert byte array to String object for easier manipulation - String sentence = new String(receivePacket.getData()).trim(); - System.out.println("RECEIVED: " + sentence); - - } catch (Exception e) { - System.out.println("An error occurred ... aborting"); - serverSocket.close(); - return; - } - } - } -} diff --git a/Internetworking_Protocol/src/main/java/exceptions/BadChecksumException.java b/Internetworking_Protocol/src/main/java/exceptions/BadChecksumException.java deleted file mode 100644 index 397c97b87fd7939aff2f329889f42c6a86df3811..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/exceptions/BadChecksumException.java +++ /dev/null @@ -1,10 +0,0 @@ -package exceptions; - -public class BadChecksumException extends IWProtocolException { - - /** - * - */ - private static final long serialVersionUID = -6481772554012497583L; - -} diff --git a/Internetworking_Protocol/src/main/java/exceptions/CookieRequestException.java b/Internetworking_Protocol/src/main/java/exceptions/CookieRequestException.java deleted file mode 100644 index a2c57679f515b8b1d4dcdbaf5e21c9ba6d7b9183..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/exceptions/CookieRequestException.java +++ /dev/null @@ -1,4 +0,0 @@ -package exceptions; - -public class CookieRequestException extends IWProtocolException { -} diff --git a/Internetworking_Protocol/src/main/java/exceptions/IWProtocolException.java b/Internetworking_Protocol/src/main/java/exceptions/IWProtocolException.java deleted file mode 100644 index a1e895c799f7cc2ff5c20b45c154f0d3d14cb29e..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/exceptions/IWProtocolException.java +++ /dev/null @@ -1,10 +0,0 @@ -package exceptions; - -public abstract class IWProtocolException extends Exception { - - /** - * - */ - private static final long serialVersionUID = -7002466204521265834L; - -} diff --git a/Internetworking_Protocol/src/main/java/exceptions/IllegalAddrException.java b/Internetworking_Protocol/src/main/java/exceptions/IllegalAddrException.java deleted file mode 100644 index 7354600b55c065c9deecf2d9d9763ce6813e8abf..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/exceptions/IllegalAddrException.java +++ /dev/null @@ -1,10 +0,0 @@ -package exceptions; - -public class IllegalAddrException extends IWProtocolException { - - /** - * - */ - private static final long serialVersionUID = -2112808760774454967L; - -} diff --git a/Internetworking_Protocol/src/main/java/exceptions/IllegalCommandException.java b/Internetworking_Protocol/src/main/java/exceptions/IllegalCommandException.java deleted file mode 100644 index d1882d59fed81504104efe583c9dc130a353d4aa..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/exceptions/IllegalCommandException.java +++ /dev/null @@ -1,4 +0,0 @@ -package exceptions; - -public class IllegalCommandException extends IWProtocolException { -} diff --git a/Internetworking_Protocol/src/main/java/exceptions/IllegalMsgException.java b/Internetworking_Protocol/src/main/java/exceptions/IllegalMsgException.java deleted file mode 100644 index 9c5a1f5c913e874a5828a34416621f6fae0e3f85..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/exceptions/IllegalMsgException.java +++ /dev/null @@ -1,10 +0,0 @@ -package exceptions; - -public class IllegalMsgException extends IWProtocolException { - - /** - * - */ - private static final long serialVersionUID = 2003522098885060854L; - -} diff --git a/Internetworking_Protocol/src/main/java/exceptions/NoNextStateException.java b/Internetworking_Protocol/src/main/java/exceptions/NoNextStateException.java deleted file mode 100644 index ca10c0ccf42f4a11673cd3c4d3568146653fde98..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/exceptions/NoNextStateException.java +++ /dev/null @@ -1,10 +0,0 @@ -package exceptions; - -public class NoNextStateException extends IWProtocolException { - - /** - * - */ - private static final long serialVersionUID = 9032871496863489383L; - -} diff --git a/Internetworking_Protocol/src/main/java/exceptions/RegistrationFailedException.java b/Internetworking_Protocol/src/main/java/exceptions/RegistrationFailedException.java deleted file mode 100644 index 960f98500a138f9396acf4d6fba002b34671a9e5..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/exceptions/RegistrationFailedException.java +++ /dev/null @@ -1,10 +0,0 @@ -package exceptions; - -public class RegistrationFailedException extends IWProtocolException { - - /** - * - */ - private static final long serialVersionUID = -3028142631363949563L; - -} diff --git a/Internetworking_Protocol/src/main/java/phy/PhyConfiguration.java b/Internetworking_Protocol/src/main/java/phy/PhyConfiguration.java deleted file mode 100644 index e323121e59f39816f9893f823e7c876ecbb8fe0b..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/phy/PhyConfiguration.java +++ /dev/null @@ -1,31 +0,0 @@ -package phy; - -import java.net.InetAddress; -import java.net.UnknownHostException; - -import core.Configuration; -import core.Protocol; - -public class PhyConfiguration extends Configuration{ - protected int remotePort; - protected InetAddress remoteIPAddress; - protected Protocol.proto_id pid; - protected boolean isClient; - - public PhyConfiguration(InetAddress rip, int rp, Protocol.proto_id pid) throws UnknownHostException { - super(null); - this.remotePort = rp; - this.remoteIPAddress = rip; - this.pid = pid; - this.isClient = true; - } - - public int getRemotePort() { - return this.remotePort; - } - - public InetAddress getRemoteIPAddress () { - return this.remoteIPAddress; - } - public Protocol.proto_id getPid() {return this.pid;} -} diff --git a/Internetworking_Protocol/src/main/java/phy/PhyMsg.java b/Internetworking_Protocol/src/main/java/phy/PhyMsg.java deleted file mode 100644 index 22dbeb60a6502d7615ce077a305d064afa3a5680..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/phy/PhyMsg.java +++ /dev/null @@ -1,82 +0,0 @@ -package phy; - -import core.Msg; -import core.Protocol; -import exceptions.IllegalMsgException; - -/* - * The only message class for the phy layer - * This layer prepends a "phy" header when sending a message - * This layer removes the "phy" header when receiving a message - */ -public class PhyMsg extends Msg { - protected static final String PHY_HEADER = "phy"; - protected Protocol.proto_id pid; - - protected PhyMsg() {} - protected PhyMsg(PhyConfiguration config) { - super(); - this.config = config; - } - - protected Protocol.proto_id getPid() {return this.pid;} - - /* - * Prepend header for sending - */ - @Override - protected void create(String data) { - this.data = data; - int id; - PhyConfiguration conf = (PhyConfiguration) this.config; - id = switch (conf.getPid()) { - case PHY -> 1; - case APP -> 3; - case SLP -> 5; - case CP -> 7; - }; - data = PHY_HEADER + " " + id + " " +data; - this.dataBytes = data.getBytes(); - } - - /* - * Does the message start with the correct header - * -> if not illegal message - * Remove header and populate data attribute - */ - @Override - protected Msg parse(String sentence) throws IllegalMsgException { - this.dataBytes = sentence.getBytes(); - if (!sentence.startsWith(PHY_HEADER)) { - System.out.println("Illeagal data header: " + sentence); - throw new IllegalMsgException(); - } - String[] parts = sentence.split("\\s+", 3); - PhyMsg pdu; - // Parse the protocol id - int id; - try { - id = Integer.parseInt(parts[1]); - } catch (NumberFormatException e) { - throw new IllegalMsgException(); - } - //Check protocol id - switch (id) { - case 1 -> pid = Protocol.proto_id.PHY; - case 3 -> pid = Protocol.proto_id.APP; - case 5 -> pid = Protocol.proto_id.SLP; - case 7 -> pid = Protocol.proto_id.CP; - default -> throw new IllegalMsgException(); - } - // If the second token is "1", call the PhyPingMsg parser - if(pid == Protocol.proto_id.PHY && parts[2].startsWith(PhyPingMsg.PHY_PING_HEADER)) { - pdu = new PhyPingMsg((PhyConfiguration) this.config); - pdu.parse(parts[2]); - } else { - this.data = parts[2]; - pdu = this; - } - return pdu; - } - -} diff --git a/Internetworking_Protocol/src/main/java/phy/PhyPingMsg.java b/Internetworking_Protocol/src/main/java/phy/PhyPingMsg.java deleted file mode 100644 index 51790fddd4287983686d792b1ad0a483271b9b11..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/phy/PhyPingMsg.java +++ /dev/null @@ -1,51 +0,0 @@ -package phy; - -import core.Msg; -import exceptions.IllegalMsgException; - -public class PhyPingMsg extends PhyMsg { - protected static final String PHY_PING_HEADER = "ping "; - private int count; - - protected PhyPingMsg(PhyConfiguration config) { - super(config); - } - - public int getCount() { - return count; - } - - /* - * Create ping msg - */ - @Override - protected void create(String data) { - this.data = data; - String sentence = PHY_PING_HEADER + data; - super.create(sentence); - } - - /* - * Does the message start with the correct header - * -> if not illegal message - * Remove header and populate data attribute - * -> try converting data to type Integer - */ - @Override - protected Msg parse(String sentence) throws IllegalMsgException { - this.dataBytes = sentence.getBytes(); - if (!sentence.startsWith(PHY_PING_HEADER)) { - System.out.println("Illeagal ping header: " + sentence); - throw new IllegalMsgException(); - } - this.data = sentence.substring(PHY_PING_HEADER.length()); - try { - this.count = Integer.parseInt(this.data.trim()); - } catch (NumberFormatException e) { - throw new IllegalMsgException(); - } - - return this; - } - -} diff --git a/Internetworking_Protocol/src/main/java/phy/PhyProtocol.java b/Internetworking_Protocol/src/main/java/phy/PhyProtocol.java deleted file mode 100644 index 9618f06ee10ba804fc5baaf6408b54621a4eedd0..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/main/java/phy/PhyProtocol.java +++ /dev/null @@ -1,102 +0,0 @@ -package phy; - -import java.io.IOException; -import java.net.DatagramPacket; -import java.net.DatagramSocket; -import java.net.SocketException; - -import core.*; -import exceptions.*; - -public class PhyProtocol extends Protocol { - protected DatagramSocket socket; - - /* - * Create a new PhyProtocol instance connected to UDP port provided - */ - public PhyProtocol(int port) { - try { - this.socket = new DatagramSocket(port); - } catch (SocketException e) { - e.printStackTrace(); - } - } - - /* - * Create msg object and send - */ - @Override - public void send(String s, Configuration config) throws IOException, IWProtocolException { - // Create empty PhyMsg object - PhyMsg m = new PhyMsg((PhyConfiguration) config); - //Populate PhyMsg object with data - m.create(s); - // Call actual send method - this.send(m); - } - - public void send(PhyMsg m) throws IOException, IWProtocolException { - // Create UDP packet - DatagramPacket sendPacket = new DatagramPacket(m.getDataBytes(), m.getLength(), - ((PhyConfiguration) m.getConfiguration()).remoteIPAddress, - ((PhyConfiguration) m.getConfiguration()).remotePort); - // send UDP packet - socket.send(sendPacket); - } - - /* - * receive incoming message from socket and parse -> call blocks on socket until message is received - * return Msg object to caller - */ - @Override - public Msg receive() throws IOException { - // read from UDP socket - // data and meta-data contained in receivedPacket object - byte[] receiveData = new byte[1024]; - DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); - socket.receive(receivePacket); - - // create msg object for parsing - PhyMsg in = new PhyMsg(); - // get data from packet data - String sentence = new String(receivePacket.getData()).trim(); - try { - // parse data to check if message is compliant with protocol specification - in = (PhyMsg) in.parse(sentence); - } catch (IllegalMsgException e) { - e.printStackTrace(); - } - // create a config object from packet meta-data - PhyConfiguration config = new PhyConfiguration(receivePacket.getAddress(), receivePacket.getPort(), in.getPid()); - in.setConfiguration(config); - - // if message was parsed correctly object is returned to caller - return in; - } - - /* - * wrapper method to basic receive method -> call blocks on socket until message is received or - * timeout expires and exception is raised - */ - public Msg receive(int timeout) throws IOException { - socket.setSoTimeout(timeout); - Msg in; - in = receive(); - - socket.setSoTimeout(0); - return in; - } - - // Send three ping messages to another system - public void ping(Configuration config) throws IOException, IWProtocolException { - for(int i=0; i<3;i++) { - // Create empty PhyPingMsg object - PhyPingMsg m = new PhyPingMsg((PhyConfiguration) config); - //Populate PhyPingMsg object with data - m.create(Integer.toString(i)); - // Call actual send method - this.send(m); - } - } - -} diff --git a/Internetworking_Protocol/src/test/java/CPClientCookieRequestTest.java b/Internetworking_Protocol/src/test/java/CPClientCookieRequestTest.java deleted file mode 100644 index dbe591dd32299aa5cdb2384156e5ce0d72deb60e..0000000000000000000000000000000000000000 --- a/Internetworking_Protocol/src/test/java/CPClientCookieRequestTest.java +++ /dev/null @@ -1,149 +0,0 @@ -package phy; - -import core.Protocol; -import cp.CPProtocol; -import exceptions.CookieRequestException; -import exceptions.IWProtocolException; -import exceptions.IllegalMsgException; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.SocketTimeoutException; -import java.net.UnknownHostException; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.*; - -@ExtendWith(MockitoExtension.class) -public class CPClientCookieRequestTest { - String serverName = "localhost"; - int serverPort = 3027; - - @Mock - PhyProtocol phyProtocolMock; - PhyMsg testMsg; - CPProtocol cProtocol; - - @BeforeEach - void setup() throws UnknownHostException { - // Create additionally needed objects for every test - PhyConfiguration phyConfig; - try { - phyConfig = new PhyConfiguration(InetAddress.getByName(serverName), serverPort, Protocol.proto_id.CP); - testMsg = new PhyMsg(phyConfig); - } catch (UnknownHostException e) { - fail(); - } - // Set up the object-under-test - cProtocol = new CPProtocol(InetAddress.getByName(serverName), serverPort, phyProtocolMock); - } - - @Test - void testCookieRequestSuccessful() throws IWProtocolException, IOException { - // Fill the message object that is going to be returned to the object-under-test - // with the message needed for this test case - testMsg = (PhyMsg)testMsg.parse("phy 7 cp cres ACK 12345"); - - // Implement behavior of the mocked object - when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); - - // Run the test - assertDoesNotThrow(()->cProtocol.requestCookie(0)); - - // verify a specified behavior - verify(phyProtocolMock, times(1)).receive(2000); - verify(phyProtocolMock).send(eq("cp creq"), any(PhyConfiguration.class)); - } - - @Test - void testNoCookie() throws IOException, IWProtocolException { - - // Fill the message object that is going to be returned to the object-under-test - // with the message needed for this test case - testMsg = (PhyMsg) testMsg.parse("phy 7 cp cres NAK no resources"); - - // Implement behavior of the mocked object - when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); - - // Run the test - assertThrows(CookieRequestException.class, - ()->cProtocol.requestCookie(0)); - verify(phyProtocolMock, times(1)).receive(2000); - - } - - @Test - void testIllegalPhyMsg() throws IOException, IWProtocolException { - - // Fill the message object that is going to be returned to the object-under-test - // with the message needed for this test case (also create corresponding configuration object) - PhyConfiguration phyConfig = new PhyConfiguration(InetAddress.getByName(serverName), serverPort, Protocol.proto_id.SLP); - testMsg = new PhyMsg(phyConfig); - testMsg = (PhyMsg) testMsg.parse("phy 5 cp cres ACK 12345"); - - // Implement behavior of the mocked object - when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); - - // Run the test - assertThrows(IllegalMsgException.class, - ()->cProtocol.requestCookie(0)); - verify(phyProtocolMock, times(1)).receive(2000); - } - - @Test - void testMaleformedCPMsg() throws IOException, IWProtocolException { - - // Fill the message object that is going to be returned to the object-under-test - // with the message needed for this test case (also create corresponding configuration object) - PhyConfiguration phyConfig = new PhyConfiguration(InetAddress.getByName(serverName), serverPort, Protocol.proto_id.SLP); - testMsg = new PhyMsg(phyConfig); - testMsg = (PhyMsg) testMsg.parse("phy 7 cp cresACK 12345"); - - // Implement behavior of the mocked object - when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); - - // Run the test - assertThrows(IllegalMsgException.class, - ()->cProtocol.requestCookie(0)); - verify(phyProtocolMock, times(1)).receive(2000); - } - - @Test - void testIncompleteCPMsg() throws IOException, IWProtocolException { - - // Fill the message object that is going to be returned to the object-under-test - // with the message needed for this test case (also create corresponding configuration object) - PhyConfiguration phyConfig = new PhyConfiguration(InetAddress.getByName(serverName), serverPort, Protocol.proto_id.SLP); - testMsg = new PhyMsg(phyConfig); - testMsg = (PhyMsg) testMsg.parse("phy 5 cp cres ACK"); - - // Implement behavior of the mocked object - when(phyProtocolMock.receive(anyInt())).thenReturn(testMsg); - - // Run the test - assertThrows(IllegalMsgException.class, - ()->cProtocol.requestCookie(0)); - verify(phyProtocolMock, times(1)).receive(2000); - } - - @Test - void testMessageLoss() throws IOException, IWProtocolException { - - // Implement behavior of the mocked object - when(phyProtocolMock.receive(anyInt())).thenThrow(new SocketTimeoutException()); - - // Run the test - assertThrows(CookieRequestException.class, - ()->cProtocol.requestCookie(0)); - verify(phyProtocolMock, times(3)).receive(2000); - } - -}