From 0e684ee3dcbd2f9a9d7084f441af116c6ddd19c5 Mon Sep 17 00:00:00 2001
From: ik1g19 <ik1g19@soton.ac.uk>
Date: Mon, 5 Apr 2021 01:10:41 +0100
Subject: [PATCH] rearrange

---
 distributed-systems-cw.iml                 | 11 +++++
 ftp/Controller.java                        | 19 +++++++++
 nioExampleFiles/AbstractTest.java          | 41 ++++++++++++++++++
 nioExampleFiles/Constants.java             | 15 +++++++
 nioExampleFiles/FileCopyTest.java          | 33 +++++++++++++++
 nioExampleFiles/FileReader.java            | 37 +++++++++++++++++
 nioExampleFiles/FileReceiver.java          | 48 ++++++++++++++++++++++
 nioExampleFiles/FileSender.java            | 32 +++++++++++++++
 nioExampleFiles/FileWriter.java            | 44 ++++++++++++++++++++
 nioExampleFiles/README.md                  |  3 ++
 nioExampleFiles/distributed-systems-cw.iml | 21 ++++++++++
 11 files changed, 304 insertions(+)
 create mode 100644 distributed-systems-cw.iml
 create mode 100644 ftp/Controller.java
 create mode 100644 nioExampleFiles/AbstractTest.java
 create mode 100644 nioExampleFiles/Constants.java
 create mode 100644 nioExampleFiles/FileCopyTest.java
 create mode 100644 nioExampleFiles/FileReader.java
 create mode 100644 nioExampleFiles/FileReceiver.java
 create mode 100644 nioExampleFiles/FileSender.java
 create mode 100644 nioExampleFiles/FileWriter.java
 create mode 100644 nioExampleFiles/README.md
 create mode 100644 nioExampleFiles/distributed-systems-cw.iml

diff --git a/distributed-systems-cw.iml b/distributed-systems-cw.iml
new file mode 100644
index 0000000..b107a2d
--- /dev/null
+++ b/distributed-systems-cw.iml
@@ -0,0 +1,11 @@
+<?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$">
+      <sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>
\ No newline at end of file
diff --git a/ftp/Controller.java b/ftp/Controller.java
new file mode 100644
index 0000000..d53915f
--- /dev/null
+++ b/ftp/Controller.java
@@ -0,0 +1,19 @@
+package ftp;
+
+public class Controller {
+    int cport;
+    int r;
+    int timeout;
+    int rbPeriod;
+
+    public Controller(int cport, int r, int timeout, int rbPeriod) {
+        this.cport = cport;
+        this.r = r;
+        this.timeout = timeout;
+        this.rbPeriod = rbPeriod;
+    }
+
+    public static void main(String args[]) {
+        Controller ctrl = new Controller(Integer.parseInt(args[0]),Integer.parseInt(args[1]),Integer.parseInt(args[2]),Integer.parseInt(args[3]));
+    }
+}
\ No newline at end of file
diff --git a/nioExampleFiles/AbstractTest.java b/nioExampleFiles/AbstractTest.java
new file mode 100644
index 0000000..14383e7
--- /dev/null
+++ b/nioExampleFiles/AbstractTest.java
@@ -0,0 +1,41 @@
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+
+public abstract class AbstractTest {
+
+    protected static final String SRC = "/README.md";
+    protected static final String TARGET = "/tmp/output";
+    protected static final long AWAIT_TEST_COMPLETE = 20000l;
+
+    protected File srcFile;
+    protected File targetFile;
+
+    @BeforeClass
+    public static void init() {
+        Thread.currentThread().getContextClassLoader().setDefaultAssertionStatus(true);
+    }
+
+    @AfterClass
+    public static void destroy() {
+        Thread.currentThread().getContextClassLoader().setDefaultAssertionStatus(false);
+    }
+
+    @Before
+    public void setUp() throws IOException {
+        this.srcFile = new File(SRC);
+        this.targetFile = new File(TARGET + "/" + this.srcFile.getName());
+
+        Files.deleteIfExists(this.targetFile.toPath());
+    }
+
+    protected final void compare() {
+        assertEquals("file did not copy completely", this.srcFile.length(), this.targetFile.length());
+    }
+}
diff --git a/nioExampleFiles/Constants.java b/nioExampleFiles/Constants.java
new file mode 100644
index 0000000..269cb7b
--- /dev/null
+++ b/nioExampleFiles/Constants.java
@@ -0,0 +1,15 @@
+public final class Constants {
+
+    public static final String INSTANTIATION_NOT_ALLOWED = "Instantiation not allowed";
+    public static final long TRANSFER_MAX_SIZE = (1024 * 1024);
+    public static final int BUFFER_SIZE = 2048;
+
+    public static final String END_MESSAGE_MARKER = ":END";
+    public static final String MESSAGE_DELIMITTER = "#";
+
+    public static final String CONFIRMATION = "OK";
+
+    private Constants() {
+        throw new IllegalStateException(INSTANTIATION_NOT_ALLOWED);
+    }
+}
diff --git a/nioExampleFiles/FileCopyTest.java b/nioExampleFiles/FileCopyTest.java
new file mode 100644
index 0000000..a399738
--- /dev/null
+++ b/nioExampleFiles/FileCopyTest.java
@@ -0,0 +1,33 @@
+import java.io.IOException;
+import java.util.concurrent.CountDownLatch;
+
+import org.junit.Test;
+
+public class FileCopyTest extends AbstractTest {
+
+    private static final int PORT = 9999;
+
+    @Test
+    public void copyLargeFile() throws IOException, InterruptedException {
+        final CountDownLatch latch = new CountDownLatch(1);
+        final FileReceiver receiver = new FileReceiver(PORT, new FileWriter(TARGET + "/" + super.srcFile.getName()), super.srcFile.length());
+
+        new Thread() {
+            public void run() {
+                try {
+                    receiver.receive();
+                } catch (IOException e) {
+
+                } finally {
+                    latch.countDown();
+                }
+            }
+        }.start();
+
+        final FileReader reader = new FileReader(new FileSender(PORT), SRC);
+        reader.read();
+
+        latch.await();
+        compare();
+    }   
+}
diff --git a/nioExampleFiles/FileReader.java b/nioExampleFiles/FileReader.java
new file mode 100644
index 0000000..e66f754
--- /dev/null
+++ b/nioExampleFiles/FileReader.java
@@ -0,0 +1,37 @@
+import java.io.IOException;
+import java.nio.channels.FileChannel;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.util.Objects;
+
+final class FileReader {
+ 
+    private final FileChannel channel;
+    private final FileSender sender;
+ 
+    FileReader(final FileSender sender, final String path) throws IOException {
+        if (Objects.isNull(sender) || path == "") {
+            throw new IllegalArgumentException("sender and path required");
+        }
+ 
+        this.sender = sender;
+        this.channel = FileChannel.open(Paths.get(path), StandardOpenOption.READ);
+    }
+ 
+    void read() throws IOException {
+        try {
+            transfer();
+        } finally {
+            close();
+        }
+    }
+ 
+    void close() throws IOException {
+        this.sender.close();
+        this.channel.close();
+    }
+ 
+    private void transfer() throws IOException {
+        this.sender.transfer(this.channel, 0l, this.channel.size());
+    }
+}
\ No newline at end of file
diff --git a/nioExampleFiles/FileReceiver.java b/nioExampleFiles/FileReceiver.java
new file mode 100644
index 0000000..eaf3e26
--- /dev/null
+++ b/nioExampleFiles/FileReceiver.java
@@ -0,0 +1,48 @@
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
+import java.util.Objects;
+
+final class FileReceiver {
+ 
+    private final int port;
+    private final FileWriter fileWriter;
+    private final long size;
+ 
+    FileReceiver(final int port, final FileWriter fileWriter, final long size) {
+        this.port = port;
+        this.fileWriter = fileWriter;
+        this.size = size;
+    }
+ 
+    void receive() throws IOException {
+        SocketChannel channel = null;
+ 
+        try (final ServerSocketChannel serverSocketChannel = ServerSocketChannel.open()) {
+            init(serverSocketChannel);
+ 
+            channel = serverSocketChannel.accept();
+ 
+            doTransfer(channel);
+        } finally {
+            if (!Objects.isNull(channel)) {
+                channel.close();
+            }
+ 
+            this.fileWriter.close();
+        }
+    }
+ 
+    private void doTransfer(final SocketChannel channel) throws IOException {
+        assert !Objects.isNull(channel);
+ 
+        this.fileWriter.transfer(channel, this.size);
+    }
+ 
+    private void init(final ServerSocketChannel serverSocketChannel) throws IOException {
+        assert !Objects.isNull(serverSocketChannel);
+ 
+        serverSocketChannel.bind(new InetSocketAddress(this.port));
+    }
+}
\ No newline at end of file
diff --git a/nioExampleFiles/FileSender.java b/nioExampleFiles/FileSender.java
new file mode 100644
index 0000000..f0c3a0b
--- /dev/null
+++ b/nioExampleFiles/FileSender.java
@@ -0,0 +1,32 @@
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.channels.FileChannel;
+import java.nio.channels.SocketChannel;
+import java.util.Objects;
+
+final class FileSender {
+ 
+    private final InetSocketAddress hostAddress;
+    private SocketChannel client;
+ 
+    FileSender(final int port) throws IOException {
+        this.hostAddress = new InetSocketAddress(port);
+        this.client = SocketChannel.open(this.hostAddress);
+    }
+ 
+    void transfer(final FileChannel channel, long position, long size) throws IOException {
+        assert !Objects.isNull(channel);
+ 
+        while (position < size) {
+            position += channel.transferTo(position, Constants.TRANSFER_MAX_SIZE, this.client);
+        }
+    }
+     
+    SocketChannel getChannel() {
+        return this.client;
+    }
+ 
+    void close() throws IOException {
+        this.client.close();
+    }
+}
\ No newline at end of file
diff --git a/nioExampleFiles/FileWriter.java b/nioExampleFiles/FileWriter.java
new file mode 100644
index 0000000..e0be9b8
--- /dev/null
+++ b/nioExampleFiles/FileWriter.java
@@ -0,0 +1,44 @@
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.channels.SocketChannel;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.util.Objects;
+
+final class FileWriter {
+ 
+    private final FileChannel channel;
+ 
+    FileWriter(final String path) throws IOException {
+        if (path == "") {
+            throw new IllegalArgumentException("path required");
+        }
+ 
+        this.channel = FileChannel.open(Paths.get(path), StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
+    }
+ 
+    void transfer(final SocketChannel channel, final long bytes) throws IOException {
+        assert !Objects.isNull(channel);
+ 
+        long position = 0l;
+        while (position < bytes) {
+            position += this.channel.transferFrom(channel, position, Constants.TRANSFER_MAX_SIZE);
+        }
+    }
+     
+    int write(final ByteBuffer buffer, long position) throws IOException {
+        assert !Objects.isNull(buffer);
+ 
+        int bytesWritten = 0;        
+        while(buffer.hasRemaining()) {
+            bytesWritten += this.channel.write(buffer, position + bytesWritten);            
+        }
+         
+        return bytesWritten;
+    }
+ 
+    void close() throws IOException {
+        this.channel.close();
+    }
+}
\ No newline at end of file
diff --git a/nioExampleFiles/README.md b/nioExampleFiles/README.md
new file mode 100644
index 0000000..75843ce
--- /dev/null
+++ b/nioExampleFiles/README.md
@@ -0,0 +1,3 @@
+# Distributed Systems Coursework
+
+a distributed file system, maybe some day it will work
diff --git a/nioExampleFiles/distributed-systems-cw.iml b/nioExampleFiles/distributed-systems-cw.iml
new file mode 100644
index 0000000..c6df852
--- /dev/null
+++ b/nioExampleFiles/distributed-systems-cw.iml
@@ -0,0 +1,21 @@
+<?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$">
+      <sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+    <orderEntry type="module-library">
+      <library name="JUnit4">
+        <CLASSES>
+          <root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
+          <root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES />
+      </library>
+    </orderEntry>
+  </component>
+</module>
\ No newline at end of file
-- 
GitLab