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

init commit

parents
No related branches found
No related tags found
No related merge requests found
Showing
with 461 additions and 0 deletions
# 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="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
<?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
<?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
<?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
<?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
<?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
<?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
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_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
<?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
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;
}
}
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