Skip to content
Snippets Groups Projects
Commit 0e684ee3 authored by ik1g19's avatar ik1g19
Browse files

rearrange

parent c7e04386
No related branches found
No related tags found
No related merge requests found
<?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
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
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());
}
}
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);
}
}
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();
}
}
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
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
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
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
# Distributed Systems Coursework
a distributed file system, maybe some day it will work
<?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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment