Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
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>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>

<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

<groupId>ru.spbau.mit.kravchenkoyura</groupId>
<artifactId>hw</artifactId>
Expand Down
159 changes: 159 additions & 0 deletions src/main/java/ru/spbau/mit/hw4/ClientUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package ru.spbau.mit.hw4;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
import javax.swing.text.JTextComponent;

public class ClientUI extends JFrame {
TorrentClient client;
Box eastBox;
Box centerBox;

ClientUI() {
super("main");
try {
client = new TorrentClient();
} catch (IOException e) {
return;
}
try {
client.read(new DataInputStream(new FileInputStream("client.info")));
} catch (IOException e) {
}

setBounds(100, 100, 700, 700);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

eastBox = Box.createVerticalBox();
getContentPane().add(eastBox, BorderLayout.EAST);

centerBox = Box.createVerticalBox();
getContentPane().add(centerBox, BorderLayout.CENTER);

Box northBox = Box.createHorizontalBox();
getContentPane().add(northBox, BorderLayout.NORTH);

JButton button = new JButton("upload");
button.addActionListener((e) -> upload());
northBox.add(button);

button = new JButton("refresh list");
button.addActionListener((e) -> evallist());
northBox.add(button);

button = new JButton("exit");
button.addActionListener((e) -> {
try {
client.write(new DataOutputStream(new FileOutputStream("client.info", false)));
} catch (IOException e1) {
}
System.exit(0);
});
northBox.add(button);

evallist();

setVisible(true);
}

private void download(PartableFile file) {
System.out.print(client.files.size());

HashMap<Integer, ArrayList<Sid>> sids = null;
try {
sids = client.getSids(file.getId());
} catch (IOException e) {
return;
}

JFrame frame = new JFrame("chose folder");
JFileChooser directoryChooser = new JFileChooser();
directoryChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
directoryChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
frame.setVisible(true);
frame.add(directoryChooser);
if (directoryChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
try {
file.createFile(directoryChooser.getSelectedFile().getPath());
RandomAccessFile rAFile = new RandomAccessFile(file.getFile().getPath(), "rw");
rAFile.setLength(file.getSize());
}catch (FileNotFoundException e) {
return;
} catch (IOException e) {
return;
}
}
else {
return;
}
frame.setVisible(false);

JProgressBar pb = new JProgressBar(0, (int) Math.ceil(file.getSize() / (1.0 * TorrentClient.size)));
centerBox.add(new JTextField(file.toString()));
centerBox.add(pb);
centerBox.revalidate();

client.files.add(file);

ArrayList<Thread> threads = new ArrayList<>();
for (Map.Entry<Integer, ArrayList<Sid>> part : sids.entrySet()) {
Thread thread = new Thread(() -> {
if (client.downloadPart(part.getKey(), part.getValue(), file)) {
synchronized (pb) {
pb.setValue(pb.getValue() + 1);
}
}
});
thread.start();
threads.add(thread);
}
for (Thread thread : threads) {
try {
thread.join();
} catch (InterruptedException e) {
}
}
}


private void evallist() {
Iterable<PartableFile> files = null;
try {
files = client.list();
} catch (IOException e) {
return;
}
eastBox.removeAll();
for (PartableFile f : files) {
eastBox.add(new JTextField(f.toString()));
JButton button = new JButton("download");
button.addActionListener(e -> download(f));
eastBox.add(button);
}
eastBox.revalidate();
}

private void upload() {
JFrame frame = new JFrame("chose file");
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
frame.setVisible(true);
int result = fileChooser.showOpenDialog(frame);
frame.add(fileChooser);
if (result == JFileChooser.APPROVE_OPTION) {
try {
client.upload(new PartableFile(fileChooser.getSelectedFile()));
} catch (IOException e) {
}
System.out.println("Selected file: " + fileChooser.getSelectedFile().getAbsolutePath());
}
frame.setVisible(false);
}

public static void main(String[] args) throws IOException {
ClientUI ui = new ClientUI();
}
}
92 changes: 92 additions & 0 deletions src/main/java/ru/spbau/mit/hw4/PartableFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package ru.spbau.mit.hw4;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

class PartableFile {
private int id;
private File file = null;
private String name = null;
private long size;
ArrayList<Integer> parts = new ArrayList<>();
PartableFile(File file) {
this.file = file;
name = file.getName();
size = file.length();
for (int i = 0; i * (TorrentClient.size) < file.length(); ++i) {
parts.add(i);
}
}
PartableFile(int id, String name, long size) {
this.id = id;
this.name = name;
this.size = size;
}

PartableFile(DataInputStream in) throws IOException {
id = in.readInt();
if (in.readBoolean()) {
file = new File(in.readUTF());
name = file.getName();
size = file.length();
}
else {
name = in.readUTF();
size = in.readLong();
}
int count = in.readInt();
for (int i = 0; i < count; ++i) {
parts.add(in.readInt());
}
}

void write(DataOutputStream out) throws IOException {
out.writeInt(id);
if (file != null) {
out.writeBoolean(true);
out.writeUTF(file.getPath());
}
else {
out.writeBoolean(false);
out.writeUTF(name);
out.writeLong(size);
}
out.writeInt(parts.size());
for (Integer i : parts) {
out.writeInt(i);
}
}

void setId(int id) {
this.id = id;
}
String getName() {
return name;
}
long getSize() {
return size;
}
File getFile() {
return file;
}
void createFile(String path) throws IOException {
file = new File(path + "//" + name);
file.getParentFile().mkdirs();
file.createNewFile();
}
int getId() {
return id;
}
ArrayList<Integer> getParts() {
return parts;
}
void addPart(int number) {
parts.add(number);
}
public String toString() {
return String.valueOf(id) + " " + getName() + " " + String.valueOf(getSize());
}
}
23 changes: 23 additions & 0 deletions src/main/java/ru/spbau/mit/hw4/Sid.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.spbau.mit.hw4;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

class Sid {
private byte[] ip;
private int port;
Sid(byte[] ip, int port) {
this.ip = ip;
this.port = port;
}
byte[] ip() {
return ip;
}
int getPort() {
return port;
}
Socket connect() throws IOException {
return new Socket(InetAddress.getByAddress(ip), port);
}
}
77 changes: 77 additions & 0 deletions src/main/java/ru/spbau/mit/hw4/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ru.spbau.mit.hw4;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.junit.BeforeClass;

import static org.junit.Assert.assertTrue;

public class Test {

@BeforeClass
public static void setUp() throws Exception {
TorrentServer server = new TorrentServer();
Thread thread = new Thread(server);
thread.setDaemon(true);
thread.start();
}

@org.junit.Test
public void onePartTest() throws IOException {
TorrentClient client1 = new TorrentClient();
TorrentClient client2 = new TorrentClient();
File in = new File("src/main/resources/from/file1.txt");
File out = new File("src/main/resources/to/file1.txt");
int id = client1.upload(new PartableFile(in));
try {
Thread.sleep(TorrentClient.time * 2);
} catch (InterruptedException e) {
}
client2.download(id, "src/main/resources/to");
try {
Thread.sleep(TorrentClient.time * 2);
} catch (InterruptedException e) {
}
assertTrue(FileUtils.contentEquals(in, out));
out.delete();
}
@org.junit.Test
public void twoPartsTest() throws IOException {
TorrentClient client1 = new TorrentClient();
TorrentClient client2 = new TorrentClient();
TorrentClient client3 = new TorrentClient();
File in = new File("src/main/resources/from/file2.txt");
File middle = new File("src/main/resources/middle/file2.txt");
File out = new File("src/main/resources/to/file2.txt");
int id = client1.upload(new PartableFile(in));
try {
Thread.sleep(TorrentClient.time * 2);
} catch (InterruptedException e) {
}
client2.download(id, "src/main/resources/middle");
try {
Thread.sleep(TorrentClient.time * 2);
} catch (InterruptedException e) {
}
client1.files.get(0).parts.clear();
client1.files.get(0).parts.add(0);
client2.files.get(0).parts.clear();
client2.files.get(0).parts.add(1);
try {
Thread.sleep(TorrentClient.time * 2);
} catch (InterruptedException e) {
}
client3.download(id, "src/main/resources/to");
try {
Thread.sleep(TorrentClient.time * 2);
} catch (InterruptedException e) {
}
assertTrue(FileUtils.contentEquals(in, out));
assertTrue(FileUtils.contentEquals(in, middle));
assertTrue(FileUtils.contentEquals(middle, out));
middle.delete();
out.delete();
}
}
Loading