{"action":"created","comment":{"url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/pulls/comments/60624601","id":60624601,"diff_hunk":"@@ -0,0 +1,554 @@\n+import java.io.IOException;\n+import java.io.RandomAccessFile;\n+import java.net.*;\n+import java.nio.file.Files;\n+import java.nio.file.Path;\n+import java.nio.file.Paths;\n+import java.util.HashSet;\n+import java.util.Set;\n+import java.util.concurrent.Executors;\n+import java.util.concurrent.ScheduledExecutorService;\n+import java.util.concurrent.TimeUnit;\n+import java.util.concurrent.locks.Lock;\n+import java.util.concurrent.locks.ReentrantLock;\n+\n+/**\n+ * Created by n_buga on 11.04.16.\n+ */\n+public class Client implements AutoCloseable{\n+    private ClientFileData clientFileData;\n+    private TorrentClient torrentClient;\n+    private TorrentServer torrentServer;\n+\n+    public Client(String ipTorrent) {\n+        clientFileData = new ClientFileData();\n+        clientFileData.updateDataFromFile();\n+        torrentClient = new TorrentClient(ipTorrent);\n+    }\n+\n+    public void start() {\n+        torrentServer = new TorrentServer();\n+        torrentServer.start();\n+        torrentClient.start();\n+    }\n+\n+    public void close() {\n+        clientFileData.saveDataToFile();\n+        torrentClient.close();\n+        torrentServer.close();\n+    }\n+\n+    private static void outFormat() {\n+        System.out.printf(\"You can use the next formats: \\n\"\n+                + \"list <tracker-address> = get the list of available files from server\\n\"\n+                + \"get <tracker-address> <file-id> = mark the file to load in the future\\n\"\n+                + \"newfile <tracker-address> <path> = add available file to server\\n\"\n+                + \"run <tracker-address> = load all files, that we want to load\\n\");\n+    }\n+\n+    public static void main(String[] args) {\n+        final int minAllowedCountArgs = 3;\n+        final boolean isExtraArgs = (args.length > minAllowedCountArgs);\n+        final int argForExtraData = 3;\n+        if (args.length < minAllowedCountArgs) {\n+            System.out.println(\"Wrong format\");\n+            outFormat();\n+            return;\n+        }\n+        String command = args[1];\n+        String address = args[2];\n+        Client client = new Client(address);\n+        switch (command) {\n+            case \"list\":\n+                Set<Client.TorrentClient.FileInfo> answer = client.getList();\n+                System.out.printf(\"The count of files: %d\\nFiles are:\\n\", answer.size());\n+                for (Client.TorrentClient.FileInfo file: answer) {\n+                    System.out.printf(\"Name = %s, size = %d, id = %d\\n\", file.getName(),\n+                            file.getSize(), file.getID());\n+                }\n+                break;\n+            case \"get\":\n+                if (!isExtraArgs) {\n+                    System.out.println(\"Wrong format\");\n+                    outFormat();\n+                    return;\n+                }\n+                String fileStringID = args[argForExtraData];\n+                int fileID;\n+                try {\n+                    fileID = Integer.parseInt(fileStringID);\n+                } catch (NumberFormatException e) {\n+                    System.out.println(\"ID isn't a number\");\n+                    outFormat();\n+                    return;\n+                }\n+                client.markAsWantToLoad(fileID);\n+                break;\n+            case \"newfile\":\n+                if (!isExtraArgs) {\n+                    System.out.println(\"Wrong format\");\n+                    outFormat();\n+                    return;\n+                }\n+                String path = args[argForExtraData];\n+                client.upload(Paths.get(path));\n+                break;\n+            case \"run\":\n+                client.run();\n+                break;\n+            case \"help\":\n+                outFormat();\n+            default:\n+                System.out.println(\"Wrong format\");\n+                outFormat();\n+        }\n+    }\n+\n+    public void resetData() {\n+        clientFileData.resetData();\n+    }\n+\n+    public int getServerPort() {\n+        return torrentServer.getPort();\n+    }\n+\n+    public String getServerIP() {\n+        return torrentClient.getIP();\n+    }\n+\n+    private enum State {NOT_STARTED, END, MISSED_CONNECTION, RUNNING};\n+\n+    public class TorrentClient implements AutoCloseable {\n+        private final int TIME_OUT_OF_WAITING_CONNECTION = 200;\n+        private Connection trackerConnection;\n+        private ScheduledExecutorService scheduledExecutorService;\n+        private final int serverPort = 8081;","path":"src/main/java/Client.java","position":125,"original_position":125,"commit_id":"dc86d9d0bc1c78559cf8cefdbb0f87bb578b03a4","original_commit_id":"dc86d9d0bc1c78559cf8cefdbb0f87bb578b03a4","user":{"login":"dzharkov","id":863207,"avatar_url":"https://avatars.githubusercontent.com/u/863207?v=3","gravatar_id":"","url":"https://api.github.com/users/dzharkov","html_url":"https://github.com/dzharkov","followers_url":"https://api.github.com/users/dzharkov/followers","following_url":"https://api.github.com/users/dzharkov/following{/other_user}","gists_url":"https://api.github.com/users/dzharkov/gists{/gist_id}","starred_url":"https://api.github.com/users/dzharkov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dzharkov/subscriptions","organizations_url":"https://api.github.com/users/dzharkov/orgs","repos_url":"https://api.github.com/users/dzharkov/repos","events_url":"https://api.github.com/users/dzharkov/events{/privacy}","received_events_url":"https://api.github.com/users/dzharkov/received_events","type":"User","site_admin":false},"body":"может быть static final","created_at":"2016-04-21T17:43:41Z","updated_at":"2016-04-21T17:43:41Z","html_url":"https://github.com/N-buga/spbau-term4-java-torrent/pull/1#discussion_r60624601","pull_request_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/pulls/comments/60624601"},"html":{"href":"https://github.com/N-buga/spbau-term4-java-torrent/pull/1#discussion_r60624601"},"pull_request":{"href":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/pulls/1","id":66893289,"html_url":"https://github.com/N-buga/spbau-term4-java-torrent/pull/1","diff_url":"https://github.com/N-buga/spbau-term4-java-torrent/pull/1.diff","patch_url":"https://github.com/N-buga/spbau-term4-java-torrent/pull/1.patch","issue_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/issues/1","number":1,"state":"open","locked":false,"title":"the right version","user":{"login":"N-buga","id":10629368,"avatar_url":"https://avatars.githubusercontent.com/u/10629368?v=3","gravatar_id":"","url":"https://api.github.com/users/N-buga","html_url":"https://github.com/N-buga","followers_url":"https://api.github.com/users/N-buga/followers","following_url":"https://api.github.com/users/N-buga/following{/other_user}","gists_url":"https://api.github.com/users/N-buga/gists{/gist_id}","starred_url":"https://api.github.com/users/N-buga/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/N-buga/subscriptions","organizations_url":"https://api.github.com/users/N-buga/orgs","repos_url":"https://api.github.com/users/N-buga/repos","events_url":"https://api.github.com/users/N-buga/events{/privacy}","received_events_url":"https://api.github.com/users/N-buga/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-04-18T17:45:27Z","updated_at":"2016-04-21T17:43:41Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e212806a8ff6f42e0475cded4836dfdf74fd9ca6","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/pulls/1/commits","review_comments_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/pulls/1/comments","review_comment_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/pulls/comments{/number}","comments_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/issues/1/comments","statuses_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/statuses/dc86d9d0bc1c78559cf8cefdbb0f87bb578b03a4","head":{"label":"N-buga:console-torrent","ref":"console-torrent","sha":"dc86d9d0bc1c78559cf8cefdbb0f87bb578b03a4","user":{"login":"N-buga","id":10629368,"avatar_url":"https://avatars.githubusercontent.com/u/10629368?v=3","gravatar_id":"","url":"https://api.github.com/users/N-buga","html_url":"https://github.com/N-buga","followers_url":"https://api.github.com/users/N-buga/followers","following_url":"https://api.github.com/users/N-buga/following{/other_user}","gists_url":"https://api.github.com/users/N-buga/gists{/gist_id}","starred_url":"https://api.github.com/users/N-buga/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/N-buga/subscriptions","organizations_url":"https://api.github.com/users/N-buga/orgs","repos_url":"https://api.github.com/users/N-buga/repos","events_url":"https://api.github.com/users/N-buga/events{/privacy}","received_events_url":"https://api.github.com/users/N-buga/received_events","type":"User","site_admin":false},"repo":{"id":56524471,"name":"spbau-term4-java-torrent","full_name":"N-buga/spbau-term4-java-torrent","owner":{"login":"N-buga","id":10629368,"avatar_url":"https://avatars.githubusercontent.com/u/10629368?v=3","gravatar_id":"","url":"https://api.github.com/users/N-buga","html_url":"https://github.com/N-buga","followers_url":"https://api.github.com/users/N-buga/followers","following_url":"https://api.github.com/users/N-buga/following{/other_user}","gists_url":"https://api.github.com/users/N-buga/gists{/gist_id}","starred_url":"https://api.github.com/users/N-buga/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/N-buga/subscriptions","organizations_url":"https://api.github.com/users/N-buga/orgs","repos_url":"https://api.github.com/users/N-buga/repos","events_url":"https://api.github.com/users/N-buga/events{/privacy}","received_events_url":"https://api.github.com/users/N-buga/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/N-buga/spbau-term4-java-torrent","description":"","fork":false,"url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent","forks_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/forks","keys_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/keys{/key_id}","collaborators_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/teams","hooks_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/hooks","issue_events_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/issues/events{/number}","events_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/events","assignees_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/assignees{/user}","branches_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/branches{/branch}","tags_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/tags","blobs_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/git/refs{/sha}","trees_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/git/trees{/sha}","statuses_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/statuses/{sha}","languages_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/languages","stargazers_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/stargazers","contributors_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/contributors","subscribers_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/subscribers","subscription_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/subscription","commits_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/commits{/sha}","git_commits_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/git/commits{/sha}","comments_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/comments{/number}","issue_comment_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/issues/comments{/number}","contents_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/contents/{+path}","compare_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/compare/{base}...{head}","merges_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/merges","archive_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/downloads","issues_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/issues{/number}","pulls_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/pulls{/number}","milestones_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/milestones{/number}","notifications_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/labels{/name}","releases_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/releases{/id}","deployments_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/deployments","created_at":"2016-04-18T16:37:14Z","updated_at":"2016-04-18T16:37:14Z","pushed_at":"2016-04-21T11:59:33Z","git_url":"git://github.com/N-buga/spbau-term4-java-torrent.git","ssh_url":"git@github.com:N-buga/spbau-term4-java-torrent.git","clone_url":"https://github.com/N-buga/spbau-term4-java-torrent.git","svn_url":"https://github.com/N-buga/spbau-term4-java-torrent","homepage":null,"size":59,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"N-buga:master","ref":"master","sha":"1a14de455e588592de77308b99c77c4e1bb1104a","user":{"login":"N-buga","id":10629368,"avatar_url":"https://avatars.githubusercontent.com/u/10629368?v=3","gravatar_id":"","url":"https://api.github.com/users/N-buga","html_url":"https://github.com/N-buga","followers_url":"https://api.github.com/users/N-buga/followers","following_url":"https://api.github.com/users/N-buga/following{/other_user}","gists_url":"https://api.github.com/users/N-buga/gists{/gist_id}","starred_url":"https://api.github.com/users/N-buga/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/N-buga/subscriptions","organizations_url":"https://api.github.com/users/N-buga/orgs","repos_url":"https://api.github.com/users/N-buga/repos","events_url":"https://api.github.com/users/N-buga/events{/privacy}","received_events_url":"https://api.github.com/users/N-buga/received_events","type":"User","site_admin":false},"repo":{"id":56524471,"name":"spbau-term4-java-torrent","full_name":"N-buga/spbau-term4-java-torrent","owner":{"login":"N-buga","id":10629368,"avatar_url":"https://avatars.githubusercontent.com/u/10629368?v=3","gravatar_id":"","url":"https://api.github.com/users/N-buga","html_url":"https://github.com/N-buga","followers_url":"https://api.github.com/users/N-buga/followers","following_url":"https://api.github.com/users/N-buga/following{/other_user}","gists_url":"https://api.github.com/users/N-buga/gists{/gist_id}","starred_url":"https://api.github.com/users/N-buga/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/N-buga/subscriptions","organizations_url":"https://api.github.com/users/N-buga/orgs","repos_url":"https://api.github.com/users/N-buga/repos","events_url":"https://api.github.com/users/N-buga/events{/privacy}","received_events_url":"https://api.github.com/users/N-buga/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/N-buga/spbau-term4-java-torrent","description":"","fork":false,"url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent","forks_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/forks","keys_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/keys{/key_id}","collaborators_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/teams","hooks_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/hooks","issue_events_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/issues/events{/number}","events_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/events","assignees_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/assignees{/user}","branches_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/branches{/branch}","tags_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/tags","blobs_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/git/refs{/sha}","trees_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/git/trees{/sha}","statuses_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/statuses/{sha}","languages_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/languages","stargazers_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/stargazers","contributors_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/contributors","subscribers_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/subscribers","subscription_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/subscription","commits_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/commits{/sha}","git_commits_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/git/commits{/sha}","comments_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/comments{/number}","issue_comment_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/issues/comments{/number}","contents_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/contents/{+path}","compare_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/compare/{base}...{head}","merges_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/merges","archive_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/downloads","issues_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/issues{/number}","pulls_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/pulls{/number}","milestones_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/milestones{/number}","notifications_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/labels{/name}","releases_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/releases{/id}","deployments_url":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/deployments","created_at":"2016-04-18T16:37:14Z","updated_at":"2016-04-18T16:37:14Z","pushed_at":"2016-04-21T11:59:33Z","git_url":"git://github.com/N-buga/spbau-term4-java-torrent.git","ssh_url":"git@github.com:N-buga/spbau-term4-java-torrent.git","clone_url":"https://github.com/N-buga/spbau-term4-java-torrent.git","svn_url":"https://github.com/N-buga/spbau-term4-java-torrent","homepage":null,"size":59,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/pulls/1"},"html":{"href":"https://github.com/N-buga/spbau-term4-java-torrent/pull/1"},"issue":{"href":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/issues/1"},"comments":{"href":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/N-buga/spbau-term4-java-torrent/statuses/dc86d9d0bc1c78559cf8cefdbb0f87bb578b03a4"}}}}