-
Notifications
You must be signed in to change notification settings - Fork 0
Java06. ДЗ 05, Егоров Антон #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pvktk
wants to merge
25
commits into
master
Choose a base branch
from
torrent
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
31f0278
Начал писать сервер на асинхронных каналах
pvktk 3d6ba2c
Написана часть кода для клиента.
pvktk 8c04ed7
Написан скачиватель отдельного файла.
pvktk 7ec70ba
Убран ReadWriteLock в клиенте. Мапы сделаны concurrent.
pvktk 44ce551
Весь код написан.
pvktk 690260c
Настроен maven-assembly-plugin
pvktk 7f12c90
В main запускается один главный поток, для облегчения тестирования.
pvktk 592d090
Добавлен тест для сервера.
pvktk f26b291
Добавлена инструкция по сборке.
pvktk 782cd47
Исправлена бага при чтении из канала в буфер.
pvktk 2cb97f2
Исправлены баги сервера.
pvktk b127922
Добавлен тест реакции клиента на базовые команды.
pvktk 620fedc
Добавлен тест на скачивание с другого пира.
pvktk 64214be
Добавлен тест скачивания с 2 пиров.
pvktk dad3dca
Исправлена бага при повторном добавлении файла.
pvktk b942e81
Переделал на RandomAccessFile.
pvktk e477f21
Добавлено отображение размера файла в частях в выводе команды list.
pvktk 60fd96f
Унифицирован код команд status и list.
pvktk d1f5f5b
обновлен readme
pvktk 5b154f9
Подправлены сообщения о включении/выключении.
pvktk 483b337
Добавлена обработка ввода чего-то вместо числа.
pvktk ce667b0
Сделано ограничение на количество одновременно скачивающихся кусков ф…
pvktk 85f1c97
Исправил замечания.
pvktk 92a22e1
Сделал double check вместо synchronized в getOrCreateFile в FilesHolder.
pvktk 7dd71bb
Убраны закрытия DataStream привязанных к сокету.
pvktk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,13 @@ | ||
| # java_homeworks | ||
| ### Torrent | ||
|
|
||
| Для сборки выполнить | ||
|
|
||
| ``` | ||
| mvn install | ||
|
|
||
| ``` | ||
|
|
||
| В папке target появятся client.jar и server.jar . | ||
| Клиенту при запуске можно передать имя хоста. По умолчанию localhost. | ||
|
|
||
| У клиента есть команда `help`, показывающая список команд. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| <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>1</groupId> | ||
| <artifactId>torrent</artifactId> | ||
| <version>0.0.1-SNAPSHOT</version> | ||
| <!-- <packaging>jar</packaging> --> | ||
|
|
||
| <name>torrent</name> | ||
| <url>http://maven.apache.org</url> | ||
|
|
||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <configuration> | ||
| <source>1.9</source> | ||
| <target>1.9</target> | ||
| <encoding>UTF-8</encoding> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-assembly-plugin</artifactId> | ||
| <executions> | ||
| <execution> | ||
| <id>torrent_server</id> | ||
| <phase>package</phase> | ||
| <goals> | ||
| <goal>single</goal> | ||
| </goals> | ||
| <configuration> | ||
| <archive> | ||
| <manifest> | ||
| <mainClass> | ||
| torrent.server.Main | ||
| </mainClass> | ||
| </manifest> | ||
| </archive> | ||
| <descriptorRefs> | ||
| <descriptorRef>jar-with-dependencies</descriptorRef> | ||
| </descriptorRefs> | ||
| <finalName>server</finalName> | ||
| <appendAssemblyId>false</appendAssemblyId> | ||
| </configuration> | ||
| </execution> | ||
| <execution> | ||
| <id>torrent_client</id> | ||
| <phase>package</phase> | ||
| <goals> | ||
| <goal>single</goal> | ||
| </goals> | ||
| <configuration> | ||
| <archive> | ||
| <manifest> | ||
| <mainClass> | ||
| torrent.client.Main | ||
| </mainClass> | ||
| </manifest> | ||
| </archive> | ||
| <descriptorRefs> | ||
| <descriptorRef>jar-with-dependencies</descriptorRef> | ||
| </descriptorRefs> | ||
| <finalName>client</finalName> | ||
| <appendAssemblyId>false</appendAssemblyId> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.12</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> | ||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.core</groupId> | ||
| <artifactId>jackson-databind</artifactId> | ||
| <version>2.9.7</version> | ||
| </dependency> | ||
|
|
||
| <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io --> | ||
| <dependency> | ||
| <groupId>org.apache.commons</groupId> | ||
| <artifactId>commons-io</artifactId> | ||
| <version>1.3.2</version> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
14 changes: 14 additions & 0 deletions
14
torrent/src/main/java/torrent/client/FileProblemException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package torrent.client; | ||
|
|
||
| public class FileProblemException extends Exception { | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| public FileProblemException(String string) { | ||
| super(string); | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package torrent.client; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.SocketAddress; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Future; | ||
|
|
||
| import torrent.client.FilesHolder.FileStatus; | ||
|
|
||
| public class FilesDownloader { | ||
|
|
||
| private FilesHolder filesHolder; | ||
| private SocketAddress toServer; | ||
|
|
||
| private ExecutorService pool = Executors.newCachedThreadPool(); | ||
|
|
||
| private Map<Integer, Future<?>> fileDownloadsFutures = new HashMap<>(); | ||
|
|
||
| public FilesDownloader(FilesHolder stm, SocketAddress toServer) { | ||
| this.filesHolder = stm; | ||
| this.toServer = toServer; | ||
| stm.fileStatus.forEach((id, status) -> { | ||
| if (status == FileStatus.Downloading ) { | ||
| try { | ||
| startFileDownload(id); | ||
| } catch (IOException e) { | ||
| System.err.println("Failed. to start download at startup"); | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public boolean startFileDownload(int fileId) throws IOException { | ||
| if (fileDownloadsFutures.containsKey(fileId)) { | ||
| return false; | ||
| } | ||
|
|
||
| filesHolder.fileStatus.put(fileId, FileStatus.Downloading); | ||
|
|
||
| SingleFileDownloader downloader = new SingleFileDownloader(toServer, filesHolder, fileId, this); | ||
| fileDownloadsFutures.put(fileId, pool.submit(downloader)); | ||
| return true; | ||
| } | ||
|
|
||
| public void stopFileDownload(int fileId) { | ||
| if (!filesHolder.fileStatus.containsKey(fileId)) { | ||
| throw new IllegalStateException("This file wasn't been downloading"); | ||
| } | ||
|
|
||
| if (filesHolder.fileStatus.get(fileId) != FileStatus.Downloading) { | ||
| return; | ||
| } | ||
|
|
||
| fileDownloadsFutures.get(fileId).cancel(true); | ||
| fileDownloadsFutures.remove(fileId); | ||
|
|
||
| filesHolder.fileStatus.put(fileId, FileStatus.Paused); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
зачем? оно ж не сериализуется
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это подсказка IDE была.