{"action":"created","comment":{"url":"https://api.github.com/repos/iphydf/tox4j/pulls/comments/30431639","id":30431639,"diff_hunk":"@@ -0,0 +1,79 @@\n+package im.tox.tox4j.impl.tcp.server\n+\n+import java.io.{ IOException, InputStream, OutputStream }\n+import java.net.{ ServerSocket, Socket, SocketTimeoutException }\n+import scala.collection.mutable.{ AbstractBuffer, ArrayBuffer }\n+\n+import im.tox.tox4j.impl.tcp._\n+import im.tox.tox4j.impl.tcp.messages._\n+\n+class Server {\n+  private final val SOCKET_TIMEOUT = 1000\n+  private final val EXIT_WAIT_TIME = 500\n+\n+  private val server: ServerSocket = new ServerSocket(0)\n+  private val publicKey: PublicKey = PublicKey.empty\n+\n+  val connections: AbstractBuffer[Connection] = new ArrayBuffer[Connection]\n+\n+  private val thread: Thread = new Thread(new Runnable {\n+    def run: Unit = {\n+      server.setSoTimeout(SOCKET_TIMEOUT)\n+      while (!Thread.interrupted) {\n+        try {\n+          val client = server.accept()\n+          new Thread(new Runnable {\n+            def run: Unit = {\n+              handle(new NetworkConnection(client))\n+            }\n+          }).start\n+        } catch {\n+          case e: SocketTimeoutException =>\n+        }\n+      }\n+    }\n+  })\n+\n+  private def handle(network: NetworkConnection): Unit = {\n+    // Read public key from bootstrapping\n+    var connectionOption: Option[Connection] = None\n+    try {\n+      network.read match {\n+        case Some(NetworkMessage(source, target)) =>\n+          val connection = Connection(network, source)\n+          connectionOption = Some(connection)\n+          connections += connection\n+          // Resend package\n+          network.write(new NetworkMessage(target, source))\n+\n+          // Wait for packages\n+          var shouldExit = false\n+          while (!shouldExit) {\n+            network.read match {\n+              case Some(msg: ExitMessage) => shouldExit = true\n+              case _                      => throw new RuntimeException(\"Unknown message\")\n+            }\n+          }\n+      }\n+    } finally {\n+      connectionOption.foreach(connections -= _)\n+      network.close\n+    }\n+  }\n+\n+  def start: Unit = thread.start\n+\n+  def stop: Unit = {\n+    thread.interrupt\n+    //Try to close connections gracefully and wait some time\n+    for (con <- connections)\n+      con.network.write(new ExitMessage(publicKey, con.publicKey))\n+    Thread.sleep(EXIT_WAIT_TIME)\n+    // Force closing connections\n+    for (con <- connections)\n+      con.network.close\n+    connections.clear","path":"src/main/java/im/tox/tox4j/impl/tcp/server/Server.scala","position":75,"original_position":75,"commit_id":"527df31854f7c98a37403b9389dab57a2c6e1003","original_commit_id":"527df31854f7c98a37403b9389dab57a2c6e1003","user":{"login":"iphydf","id":10647936,"avatar_url":"https://avatars.githubusercontent.com/u/10647936?v=3","gravatar_id":"","url":"https://api.github.com/users/iphydf","html_url":"https://github.com/iphydf","followers_url":"https://api.github.com/users/iphydf/followers","following_url":"https://api.github.com/users/iphydf/following{/other_user}","gists_url":"https://api.github.com/users/iphydf/gists{/gist_id}","starred_url":"https://api.github.com/users/iphydf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iphydf/subscriptions","organizations_url":"https://api.github.com/users/iphydf/orgs","repos_url":"https://api.github.com/users/iphydf/repos","events_url":"https://api.github.com/users/iphydf/events{/privacy}","received_events_url":"https://api.github.com/users/iphydf/received_events","type":"User","site_admin":false},"body":"Side-effects.","created_at":"2015-05-15T18:10:21Z","updated_at":"2015-05-15T18:10:21Z","html_url":"https://github.com/iphydf/tox4j/pull/1#discussion_r30431639","pull_request_url":"https://api.github.com/repos/iphydf/tox4j/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/iphydf/tox4j/pulls/comments/30431639"},"html":{"href":"https://github.com/iphydf/tox4j/pull/1#discussion_r30431639"},"pull_request":{"href":"https://api.github.com/repos/iphydf/tox4j/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/iphydf/tox4j/pulls/1","id":35390519,"html_url":"https://github.com/iphydf/tox4j/pull/1","diff_url":"https://github.com/iphydf/tox4j/pull/1.diff","patch_url":"https://github.com/iphydf/tox4j/pull/1.patch","issue_url":"https://api.github.com/repos/iphydf/tox4j/issues/1","number":1,"state":"open","locked":false,"title":"ToxCore tcp implementation","user":{"login":"Flakebi","id":6499211,"avatar_url":"https://avatars.githubusercontent.com/u/6499211?v=3","gravatar_id":"","url":"https://api.github.com/users/Flakebi","html_url":"https://github.com/Flakebi","followers_url":"https://api.github.com/users/Flakebi/followers","following_url":"https://api.github.com/users/Flakebi/following{/other_user}","gists_url":"https://api.github.com/users/Flakebi/gists{/gist_id}","starred_url":"https://api.github.com/users/Flakebi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Flakebi/subscriptions","organizations_url":"https://api.github.com/users/Flakebi/orgs","repos_url":"https://api.github.com/users/Flakebi/repos","events_url":"https://api.github.com/users/Flakebi/events{/privacy}","received_events_url":"https://api.github.com/users/Flakebi/received_events","type":"User","site_admin":false},"body":"Hi,\r\n\r\nit would be nice if you could take a look at the current state.\r\nThere are still some `println`s and I'm not sure how the server should react if an exception is thrown.\r\nThe client `iteration` and callbacks are not yet implemented.\r\n\r\nA little working example:\r\n```\r\n    val server: Server = new Server\r\n    val client: ToxCore = new ToxCoreTcpMock\r\n    try {\r\n      server.start\r\n      client.bootstrap(\"localhost\", server.getPort, Array())\r\n      Thread.sleep(1000)\r\n    } finally {\r\n      client.close\r\n      server.stop\r\n    }\r\n```","created_at":"2015-05-14T03:06:53Z","updated_at":"2015-05-15T18:10:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e0502366a1f806b1ca63d13abafb3b867ad8bfc1","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/iphydf/tox4j/pulls/1/commits","review_comments_url":"https://api.github.com/repos/iphydf/tox4j/pulls/1/comments","review_comment_url":"https://api.github.com/repos/iphydf/tox4j/pulls/comments{/number}","comments_url":"https://api.github.com/repos/iphydf/tox4j/issues/1/comments","statuses_url":"https://api.github.com/repos/iphydf/tox4j/statuses/527df31854f7c98a37403b9389dab57a2c6e1003","head":{"label":"Flakebi:auto","ref":"auto","sha":"527df31854f7c98a37403b9389dab57a2c6e1003","user":{"login":"Flakebi","id":6499211,"avatar_url":"https://avatars.githubusercontent.com/u/6499211?v=3","gravatar_id":"","url":"https://api.github.com/users/Flakebi","html_url":"https://github.com/Flakebi","followers_url":"https://api.github.com/users/Flakebi/followers","following_url":"https://api.github.com/users/Flakebi/following{/other_user}","gists_url":"https://api.github.com/users/Flakebi/gists{/gist_id}","starred_url":"https://api.github.com/users/Flakebi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Flakebi/subscriptions","organizations_url":"https://api.github.com/users/Flakebi/orgs","repos_url":"https://api.github.com/users/Flakebi/repos","events_url":"https://api.github.com/users/Flakebi/events{/privacy}","received_events_url":"https://api.github.com/users/Flakebi/received_events","type":"User","site_admin":false},"repo":{"id":35155363,"name":"tox4j","full_name":"Flakebi/tox4j","owner":{"login":"Flakebi","id":6499211,"avatar_url":"https://avatars.githubusercontent.com/u/6499211?v=3","gravatar_id":"","url":"https://api.github.com/users/Flakebi","html_url":"https://github.com/Flakebi","followers_url":"https://api.github.com/users/Flakebi/followers","following_url":"https://api.github.com/users/Flakebi/following{/other_user}","gists_url":"https://api.github.com/users/Flakebi/gists{/gist_id}","starred_url":"https://api.github.com/users/Flakebi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Flakebi/subscriptions","organizations_url":"https://api.github.com/users/Flakebi/orgs","repos_url":"https://api.github.com/users/Flakebi/repos","events_url":"https://api.github.com/users/Flakebi/events{/privacy}","received_events_url":"https://api.github.com/users/Flakebi/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Flakebi/tox4j","description":"New and improved java wrapper for Tox","fork":true,"url":"https://api.github.com/repos/Flakebi/tox4j","forks_url":"https://api.github.com/repos/Flakebi/tox4j/forks","keys_url":"https://api.github.com/repos/Flakebi/tox4j/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Flakebi/tox4j/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Flakebi/tox4j/teams","hooks_url":"https://api.github.com/repos/Flakebi/tox4j/hooks","issue_events_url":"https://api.github.com/repos/Flakebi/tox4j/issues/events{/number}","events_url":"https://api.github.com/repos/Flakebi/tox4j/events","assignees_url":"https://api.github.com/repos/Flakebi/tox4j/assignees{/user}","branches_url":"https://api.github.com/repos/Flakebi/tox4j/branches{/branch}","tags_url":"https://api.github.com/repos/Flakebi/tox4j/tags","blobs_url":"https://api.github.com/repos/Flakebi/tox4j/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Flakebi/tox4j/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Flakebi/tox4j/git/refs{/sha}","trees_url":"https://api.github.com/repos/Flakebi/tox4j/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Flakebi/tox4j/statuses/{sha}","languages_url":"https://api.github.com/repos/Flakebi/tox4j/languages","stargazers_url":"https://api.github.com/repos/Flakebi/tox4j/stargazers","contributors_url":"https://api.github.com/repos/Flakebi/tox4j/contributors","subscribers_url":"https://api.github.com/repos/Flakebi/tox4j/subscribers","subscription_url":"https://api.github.com/repos/Flakebi/tox4j/subscription","commits_url":"https://api.github.com/repos/Flakebi/tox4j/commits{/sha}","git_commits_url":"https://api.github.com/repos/Flakebi/tox4j/git/commits{/sha}","comments_url":"https://api.github.com/repos/Flakebi/tox4j/comments{/number}","issue_comment_url":"https://api.github.com/repos/Flakebi/tox4j/issues/comments{/number}","contents_url":"https://api.github.com/repos/Flakebi/tox4j/contents/{+path}","compare_url":"https://api.github.com/repos/Flakebi/tox4j/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Flakebi/tox4j/merges","archive_url":"https://api.github.com/repos/Flakebi/tox4j/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Flakebi/tox4j/downloads","issues_url":"https://api.github.com/repos/Flakebi/tox4j/issues{/number}","pulls_url":"https://api.github.com/repos/Flakebi/tox4j/pulls{/number}","milestones_url":"https://api.github.com/repos/Flakebi/tox4j/milestones{/number}","notifications_url":"https://api.github.com/repos/Flakebi/tox4j/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Flakebi/tox4j/labels{/name}","releases_url":"https://api.github.com/repos/Flakebi/tox4j/releases{/id}","created_at":"2015-05-06T11:20:43Z","updated_at":"2015-05-08T19:19:15Z","pushed_at":"2015-05-14T22:10:17Z","git_url":"git://github.com/Flakebi/tox4j.git","ssh_url":"git@github.com:Flakebi/tox4j.git","clone_url":"https://github.com/Flakebi/tox4j.git","svn_url":"https://github.com/Flakebi/tox4j","homepage":null,"size":1506,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"auto"}},"base":{"label":"iphydf:auto","ref":"auto","sha":"255647c667eadd2b0188a8b6a4b71a944e3b900b","user":{"login":"iphydf","id":10647936,"avatar_url":"https://avatars.githubusercontent.com/u/10647936?v=3","gravatar_id":"","url":"https://api.github.com/users/iphydf","html_url":"https://github.com/iphydf","followers_url":"https://api.github.com/users/iphydf/followers","following_url":"https://api.github.com/users/iphydf/following{/other_user}","gists_url":"https://api.github.com/users/iphydf/gists{/gist_id}","starred_url":"https://api.github.com/users/iphydf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iphydf/subscriptions","organizations_url":"https://api.github.com/users/iphydf/orgs","repos_url":"https://api.github.com/users/iphydf/repos","events_url":"https://api.github.com/users/iphydf/events{/privacy}","received_events_url":"https://api.github.com/users/iphydf/received_events","type":"User","site_admin":false},"repo":{"id":32993709,"name":"tox4j","full_name":"iphydf/tox4j","owner":{"login":"iphydf","id":10647936,"avatar_url":"https://avatars.githubusercontent.com/u/10647936?v=3","gravatar_id":"","url":"https://api.github.com/users/iphydf","html_url":"https://github.com/iphydf","followers_url":"https://api.github.com/users/iphydf/followers","following_url":"https://api.github.com/users/iphydf/following{/other_user}","gists_url":"https://api.github.com/users/iphydf/gists{/gist_id}","starred_url":"https://api.github.com/users/iphydf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/iphydf/subscriptions","organizations_url":"https://api.github.com/users/iphydf/orgs","repos_url":"https://api.github.com/users/iphydf/repos","events_url":"https://api.github.com/users/iphydf/events{/privacy}","received_events_url":"https://api.github.com/users/iphydf/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/iphydf/tox4j","description":"New and improved java wrapper for Tox","fork":true,"url":"https://api.github.com/repos/iphydf/tox4j","forks_url":"https://api.github.com/repos/iphydf/tox4j/forks","keys_url":"https://api.github.com/repos/iphydf/tox4j/keys{/key_id}","collaborators_url":"https://api.github.com/repos/iphydf/tox4j/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/iphydf/tox4j/teams","hooks_url":"https://api.github.com/repos/iphydf/tox4j/hooks","issue_events_url":"https://api.github.com/repos/iphydf/tox4j/issues/events{/number}","events_url":"https://api.github.com/repos/iphydf/tox4j/events","assignees_url":"https://api.github.com/repos/iphydf/tox4j/assignees{/user}","branches_url":"https://api.github.com/repos/iphydf/tox4j/branches{/branch}","tags_url":"https://api.github.com/repos/iphydf/tox4j/tags","blobs_url":"https://api.github.com/repos/iphydf/tox4j/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/iphydf/tox4j/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/iphydf/tox4j/git/refs{/sha}","trees_url":"https://api.github.com/repos/iphydf/tox4j/git/trees{/sha}","statuses_url":"https://api.github.com/repos/iphydf/tox4j/statuses/{sha}","languages_url":"https://api.github.com/repos/iphydf/tox4j/languages","stargazers_url":"https://api.github.com/repos/iphydf/tox4j/stargazers","contributors_url":"https://api.github.com/repos/iphydf/tox4j/contributors","subscribers_url":"https://api.github.com/repos/iphydf/tox4j/subscribers","subscription_url":"https://api.github.com/repos/iphydf/tox4j/subscription","commits_url":"https://api.github.com/repos/iphydf/tox4j/commits{/sha}","git_commits_url":"https://api.github.com/repos/iphydf/tox4j/git/commits{/sha}","comments_url":"https://api.github.com/repos/iphydf/tox4j/comments{/number}","issue_comment_url":"https://api.github.com/repos/iphydf/tox4j/issues/comments{/number}","contents_url":"https://api.github.com/repos/iphydf/tox4j/contents/{+path}","compare_url":"https://api.github.com/repos/iphydf/tox4j/compare/{base}...{head}","merges_url":"https://api.github.com/repos/iphydf/tox4j/merges","archive_url":"https://api.github.com/repos/iphydf/tox4j/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/iphydf/tox4j/downloads","issues_url":"https://api.github.com/repos/iphydf/tox4j/issues{/number}","pulls_url":"https://api.github.com/repos/iphydf/tox4j/pulls{/number}","milestones_url":"https://api.github.com/repos/iphydf/tox4j/milestones{/number}","notifications_url":"https://api.github.com/repos/iphydf/tox4j/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/iphydf/tox4j/labels{/name}","releases_url":"https://api.github.com/repos/iphydf/tox4j/releases{/id}","created_at":"2015-03-27T15:26:33Z","updated_at":"2015-05-06T12:26:59Z","pushed_at":"2015-05-15T17:42:09Z","git_url":"git://github.com/iphydf/tox4j.git","ssh_url":"git@github.com:iphydf/tox4j.git","clone_url":"https://github.com/iphydf/tox4j.git","svn_url":"https://github.com/iphydf/tox4j","homepage":null,"size":1790,"stargazers_count":1,"watchers_count":1,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":2,"mirror_url":null,"open_issues_count":1,"forks":2,"open_issues":1,"watchers":1,"default_branch":"auto"}},"_links":{"self":{"href":"https://api.github.com/repos/iphydf/tox4j/pulls/1"},"html":{"href":"https://github.com/iphydf/tox4j/pull/1"},"issue":{"href":"https://api.github.com/repos/iphydf/tox4j/issues/1"},"comments":{"href":"https://api.github.com/repos/iphydf/tox4j/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/iphydf/tox4j/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/iphydf/tox4j/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/iphydf/tox4j/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/iphydf/tox4j/statuses/527df31854f7c98a37403b9389dab57a2c6e1003"}}}}