{"action":"created","comment":{"url":"https://api.github.com/repos/openhab/openhab2-addons/pulls/comments/110342666","pull_request_review_id":31509214,"id":110342666,"diff_hunk":"@@ -37,89 +36,50 @@ public RFXComStreamReader(RFXComBaseConnector connector, InputStream in) {\n     }\n \n     @Override\n-    public void interrupt() {\n-        interrupted = true;\n-        super.interrupt();\n-        try {\n-            in.close();\n-        } catch (IOException ignore) {\n-            // quietly close\n-        }\n-    }\n-\n-    @Override\n     public void run() {\n-        final int dataBufferMaxLen = Byte.MAX_VALUE;\n-\n-        byte[] dataBuffer = new byte[dataBufferMaxLen];\n-\n-        int msgLen = 0;\n-        int index = 0;\n-        boolean startFound = false;\n-\n         logger.debug(\"Data listener started\");\n \n-        try {\n-\n-            byte[] tmpData = new byte[20];\n-            int len = -1;\n-\n-            while (!interrupted) {\n-\n-                if ((len = in.read(tmpData)) > 0) {\n-\n-                    byte[] logData = Arrays.copyOf(tmpData, len);\n-                    logger.trace(\"Received data (len={}): {}\", len, DatatypeConverter.printHexBinary(logData));\n-\n-                    for (int i = 0; i < len; i++) {\n-\n-                        if (index > dataBufferMaxLen) {\n-                            // too many bytes received, try to find new\n-                            // start\n-                            startFound = false;\n-                        }\n-\n-                        if (!startFound && tmpData[i] > 0) {\n-\n-                            startFound = true;\n-                            index = 0;\n-                            dataBuffer[index++] = tmpData[i];\n-                            msgLen = tmpData[i] + 1;\n-\n-                        } else if (startFound) {\n-\n-                            dataBuffer[index++] = tmpData[i];\n-\n-                            if (index == msgLen) {\n-                                sendMessage(dataBuffer, msgLen);\n-\n-                                // find new start\n-                                startFound = false;\n-                            }\n+        final int MAX_READ_TIMEOUTS = 4;\n+        byte[] buf = new byte[Byte.MAX_VALUE];\n+\n+        // The stream has (or SHOULD have) a read timeout set. Taking a\n+        // read timeout (read returns 0) between packets gives us a chance\n+        // to check if we've  been interrupted. Read interrupts during a\n+        // packet are ignored but if too many occur we take it as meaning\n+        // the RFXCOM has become missing presumed dead.\n+        while (!Thread.interrupted()) {\n+            try {\n+                // First byte tells us how long the packet is\n+                int bytesRead = in.read(buf, 0, 1);\n+                int packetLength = buf[0];\n+\n+                if (bytesRead > 0 && packetLength > 0) {\n+                    // Now read the rest of the packet\n+                    int bufferIndex = 1;\n+                    int readTimeoutCount = 1;\n+\n+                    while (bufferIndex <= packetLength) {\n+                        int bytesRemaining = packetLength - bufferIndex + 1;\n+                        bytesRead = in.read(buf, bufferIndex, bytesRemaining);\n+\n+                        if (bytesRead > 0) {\n+                            bufferIndex += bytesRead;\n+                        } else if (readTimeoutCount++ == MAX_READ_TIMEOUTS) {\n+                            throw new IOException(\"Timeout during packet read\");","path":"addons/binding/org.openhab.binding.rfxcom/src/main/java/org/openhab/binding/rfxcom/internal/connector/RFXComStreamReader.java","position":97,"original_position":97,"commit_id":"c23edf097ebc3e0fff06731694b4efecef9092f5","original_commit_id":"c23edf097ebc3e0fff06731694b4efecef9092f5","user":{"login":"martinvw","id":2625279,"avatar_url":"https://avatars0.githubusercontent.com/u/2625279?v=3","gravatar_id":"","url":"https://api.github.com/users/martinvw","html_url":"https://github.com/martinvw","followers_url":"https://api.github.com/users/martinvw/followers","following_url":"https://api.github.com/users/martinvw/following{/other_user}","gists_url":"https://api.github.com/users/martinvw/gists{/gist_id}","starred_url":"https://api.github.com/users/martinvw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/martinvw/subscriptions","organizations_url":"https://api.github.com/users/martinvw/orgs","repos_url":"https://api.github.com/users/martinvw/repos","events_url":"https://api.github.com/users/martinvw/events{/privacy}","received_events_url":"https://api.github.com/users/martinvw/received_events","type":"User","site_admin":false},"body":"> A continue would be fatal.\r\nCorrect, I missed the second nested loop. So what we actually need to do to avoid throwing and catching an exception in the same method is breaking the inner loop and continue-the outer loop. Java has ways for that but those are a little uncommon. \r\n_The most proper way is imho using more methods, I think we learned programming from different persons because I always like more methods and like them between 2 & 10 lines._\r\n\r\nThanks for the complete explanation of the error handling 👍 \r\n","created_at":"2017-04-07T08:42:24Z","updated_at":"2017-04-07T08:42:24Z","html_url":"https://github.com/openhab/openhab2-addons/pull/2101#discussion_r110342666","pull_request_url":"https://api.github.com/repos/openhab/openhab2-addons/pulls/2101","_links":{"self":{"href":"https://api.github.com/repos/openhab/openhab2-addons/pulls/comments/110342666"},"html":{"href":"https://github.com/openhab/openhab2-addons/pull/2101#discussion_r110342666"},"pull_request":{"href":"https://api.github.com/repos/openhab/openhab2-addons/pulls/2101"}}},"pull_request":{"url":"https://api.github.com/repos/openhab/openhab2-addons/pulls/2101","id":113691890,"html_url":"https://github.com/openhab/openhab2-addons/pull/2101","diff_url":"https://github.com/openhab/openhab2-addons/pull/2101.diff","patch_url":"https://github.com/openhab/openhab2-addons/pull/2101.patch","issue_url":"https://api.github.com/repos/openhab/openhab2-addons/issues/2101","number":2101,"state":"open","locked":false,"title":"[RFXCOM] Fixes to shutdown and packet reading thread","user":{"login":"mjagdis","id":856286,"avatar_url":"https://avatars0.githubusercontent.com/u/856286?v=3","gravatar_id":"","url":"https://api.github.com/users/mjagdis","html_url":"https://github.com/mjagdis","followers_url":"https://api.github.com/users/mjagdis/followers","following_url":"https://api.github.com/users/mjagdis/following{/other_user}","gists_url":"https://api.github.com/users/mjagdis/gists{/gist_id}","starred_url":"https://api.github.com/users/mjagdis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjagdis/subscriptions","organizations_url":"https://api.github.com/users/mjagdis/orgs","repos_url":"https://api.github.com/users/mjagdis/repos","events_url":"https://api.github.com/users/mjagdis/events{/privacy}","received_events_url":"https://api.github.com/users/mjagdis/received_events","type":"User","site_admin":false},"body":"We MUST set read timeouts on the input streams because the reads\r\nthemselves are not interruptible. We SHOULD wait for the read thread\r\nto handle the interrupt and exit before completing the shutdown.\r\n\r\nThe packet reading loop SHOULD NOT handle an interrupt and shutdown\r\nin the middle of a packet - only between packets. In the unlikely\r\n(but possible) event that the RFXCOM is unplugged mid-packet a\r\ntimeout is used while reading packet data and the connection\r\nabandoned if it is reached.\r\n\r\nCloses #2100\r\n\r\nSigned-off-by: Mike Jagdis <mjagdis@eris-associates.co.uk> (github: mjagdis)","created_at":"2017-03-31T17:01:22Z","updated_at":"2017-04-07T08:42:24Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e72feb14dd71136af80ce75eb734d57f139d0d43","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/openhab/openhab2-addons/pulls/2101/commits","review_comments_url":"https://api.github.com/repos/openhab/openhab2-addons/pulls/2101/comments","review_comment_url":"https://api.github.com/repos/openhab/openhab2-addons/pulls/comments{/number}","comments_url":"https://api.github.com/repos/openhab/openhab2-addons/issues/2101/comments","statuses_url":"https://api.github.com/repos/openhab/openhab2-addons/statuses/c23edf097ebc3e0fff06731694b4efecef9092f5","head":{"label":"mjagdis:2100-rfxcom-shutdown","ref":"2100-rfxcom-shutdown","sha":"c23edf097ebc3e0fff06731694b4efecef9092f5","user":{"login":"mjagdis","id":856286,"avatar_url":"https://avatars0.githubusercontent.com/u/856286?v=3","gravatar_id":"","url":"https://api.github.com/users/mjagdis","html_url":"https://github.com/mjagdis","followers_url":"https://api.github.com/users/mjagdis/followers","following_url":"https://api.github.com/users/mjagdis/following{/other_user}","gists_url":"https://api.github.com/users/mjagdis/gists{/gist_id}","starred_url":"https://api.github.com/users/mjagdis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjagdis/subscriptions","organizations_url":"https://api.github.com/users/mjagdis/orgs","repos_url":"https://api.github.com/users/mjagdis/repos","events_url":"https://api.github.com/users/mjagdis/events{/privacy}","received_events_url":"https://api.github.com/users/mjagdis/received_events","type":"User","site_admin":false},"repo":{"id":86806383,"name":"openhab2-addons","full_name":"mjagdis/openhab2-addons","owner":{"login":"mjagdis","id":856286,"avatar_url":"https://avatars0.githubusercontent.com/u/856286?v=3","gravatar_id":"","url":"https://api.github.com/users/mjagdis","html_url":"https://github.com/mjagdis","followers_url":"https://api.github.com/users/mjagdis/followers","following_url":"https://api.github.com/users/mjagdis/following{/other_user}","gists_url":"https://api.github.com/users/mjagdis/gists{/gist_id}","starred_url":"https://api.github.com/users/mjagdis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mjagdis/subscriptions","organizations_url":"https://api.github.com/users/mjagdis/orgs","repos_url":"https://api.github.com/users/mjagdis/repos","events_url":"https://api.github.com/users/mjagdis/events{/privacy}","received_events_url":"https://api.github.com/users/mjagdis/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/mjagdis/openhab2-addons","description":"Add-ons for openHAB 2.x","fork":true,"url":"https://api.github.com/repos/mjagdis/openhab2-addons","forks_url":"https://api.github.com/repos/mjagdis/openhab2-addons/forks","keys_url":"https://api.github.com/repos/mjagdis/openhab2-addons/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mjagdis/openhab2-addons/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mjagdis/openhab2-addons/teams","hooks_url":"https://api.github.com/repos/mjagdis/openhab2-addons/hooks","issue_events_url":"https://api.github.com/repos/mjagdis/openhab2-addons/issues/events{/number}","events_url":"https://api.github.com/repos/mjagdis/openhab2-addons/events","assignees_url":"https://api.github.com/repos/mjagdis/openhab2-addons/assignees{/user}","branches_url":"https://api.github.com/repos/mjagdis/openhab2-addons/branches{/branch}","tags_url":"https://api.github.com/repos/mjagdis/openhab2-addons/tags","blobs_url":"https://api.github.com/repos/mjagdis/openhab2-addons/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mjagdis/openhab2-addons/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mjagdis/openhab2-addons/git/refs{/sha}","trees_url":"https://api.github.com/repos/mjagdis/openhab2-addons/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mjagdis/openhab2-addons/statuses/{sha}","languages_url":"https://api.github.com/repos/mjagdis/openhab2-addons/languages","stargazers_url":"https://api.github.com/repos/mjagdis/openhab2-addons/stargazers","contributors_url":"https://api.github.com/repos/mjagdis/openhab2-addons/contributors","subscribers_url":"https://api.github.com/repos/mjagdis/openhab2-addons/subscribers","subscription_url":"https://api.github.com/repos/mjagdis/openhab2-addons/subscription","commits_url":"https://api.github.com/repos/mjagdis/openhab2-addons/commits{/sha}","git_commits_url":"https://api.github.com/repos/mjagdis/openhab2-addons/git/commits{/sha}","comments_url":"https://api.github.com/repos/mjagdis/openhab2-addons/comments{/number}","issue_comment_url":"https://api.github.com/repos/mjagdis/openhab2-addons/issues/comments{/number}","contents_url":"https://api.github.com/repos/mjagdis/openhab2-addons/contents/{+path}","compare_url":"https://api.github.com/repos/mjagdis/openhab2-addons/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mjagdis/openhab2-addons/merges","archive_url":"https://api.github.com/repos/mjagdis/openhab2-addons/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mjagdis/openhab2-addons/downloads","issues_url":"https://api.github.com/repos/mjagdis/openhab2-addons/issues{/number}","pulls_url":"https://api.github.com/repos/mjagdis/openhab2-addons/pulls{/number}","milestones_url":"https://api.github.com/repos/mjagdis/openhab2-addons/milestones{/number}","notifications_url":"https://api.github.com/repos/mjagdis/openhab2-addons/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mjagdis/openhab2-addons/labels{/name}","releases_url":"https://api.github.com/repos/mjagdis/openhab2-addons/releases{/id}","deployments_url":"https://api.github.com/repos/mjagdis/openhab2-addons/deployments","created_at":"2017-03-31T10:13:39Z","updated_at":"2017-03-31T10:13:46Z","pushed_at":"2017-04-05T23:08:34Z","git_url":"git://github.com/mjagdis/openhab2-addons.git","ssh_url":"git@github.com:mjagdis/openhab2-addons.git","clone_url":"https://github.com/mjagdis/openhab2-addons.git","svn_url":"https://github.com/mjagdis/openhab2-addons","homepage":"","size":76733,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"openhab:master","ref":"master","sha":"edcdb99f6869d753826936dc2ff4b49d37c6c48c","user":{"login":"openhab","id":1007353,"avatar_url":"https://avatars3.githubusercontent.com/u/1007353?v=3","gravatar_id":"","url":"https://api.github.com/users/openhab","html_url":"https://github.com/openhab","followers_url":"https://api.github.com/users/openhab/followers","following_url":"https://api.github.com/users/openhab/following{/other_user}","gists_url":"https://api.github.com/users/openhab/gists{/gist_id}","starred_url":"https://api.github.com/users/openhab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openhab/subscriptions","organizations_url":"https://api.github.com/users/openhab/orgs","repos_url":"https://api.github.com/users/openhab/repos","events_url":"https://api.github.com/users/openhab/events{/privacy}","received_events_url":"https://api.github.com/users/openhab/received_events","type":"Organization","site_admin":false},"repo":{"id":19753195,"name":"openhab2-addons","full_name":"openhab/openhab2-addons","owner":{"login":"openhab","id":1007353,"avatar_url":"https://avatars3.githubusercontent.com/u/1007353?v=3","gravatar_id":"","url":"https://api.github.com/users/openhab","html_url":"https://github.com/openhab","followers_url":"https://api.github.com/users/openhab/followers","following_url":"https://api.github.com/users/openhab/following{/other_user}","gists_url":"https://api.github.com/users/openhab/gists{/gist_id}","starred_url":"https://api.github.com/users/openhab/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/openhab/subscriptions","organizations_url":"https://api.github.com/users/openhab/orgs","repos_url":"https://api.github.com/users/openhab/repos","events_url":"https://api.github.com/users/openhab/events{/privacy}","received_events_url":"https://api.github.com/users/openhab/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/openhab/openhab2-addons","description":"Add-ons for openHAB 2.x","fork":false,"url":"https://api.github.com/repos/openhab/openhab2-addons","forks_url":"https://api.github.com/repos/openhab/openhab2-addons/forks","keys_url":"https://api.github.com/repos/openhab/openhab2-addons/keys{/key_id}","collaborators_url":"https://api.github.com/repos/openhab/openhab2-addons/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/openhab/openhab2-addons/teams","hooks_url":"https://api.github.com/repos/openhab/openhab2-addons/hooks","issue_events_url":"https://api.github.com/repos/openhab/openhab2-addons/issues/events{/number}","events_url":"https://api.github.com/repos/openhab/openhab2-addons/events","assignees_url":"https://api.github.com/repos/openhab/openhab2-addons/assignees{/user}","branches_url":"https://api.github.com/repos/openhab/openhab2-addons/branches{/branch}","tags_url":"https://api.github.com/repos/openhab/openhab2-addons/tags","blobs_url":"https://api.github.com/repos/openhab/openhab2-addons/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/openhab/openhab2-addons/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/openhab/openhab2-addons/git/refs{/sha}","trees_url":"https://api.github.com/repos/openhab/openhab2-addons/git/trees{/sha}","statuses_url":"https://api.github.com/repos/openhab/openhab2-addons/statuses/{sha}","languages_url":"https://api.github.com/repos/openhab/openhab2-addons/languages","stargazers_url":"https://api.github.com/repos/openhab/openhab2-addons/stargazers","contributors_url":"https://api.github.com/repos/openhab/openhab2-addons/contributors","subscribers_url":"https://api.github.com/repos/openhab/openhab2-addons/subscribers","subscription_url":"https://api.github.com/repos/openhab/openhab2-addons/subscription","commits_url":"https://api.github.com/repos/openhab/openhab2-addons/commits{/sha}","git_commits_url":"https://api.github.com/repos/openhab/openhab2-addons/git/commits{/sha}","comments_url":"https://api.github.com/repos/openhab/openhab2-addons/comments{/number}","issue_comment_url":"https://api.github.com/repos/openhab/openhab2-addons/issues/comments{/number}","contents_url":"https://api.github.com/repos/openhab/openhab2-addons/contents/{+path}","compare_url":"https://api.github.com/repos/openhab/openhab2-addons/compare/{base}...{head}","merges_url":"https://api.github.com/repos/openhab/openhab2-addons/merges","archive_url":"https://api.github.com/repos/openhab/openhab2-addons/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/openhab/openhab2-addons/downloads","issues_url":"https://api.github.com/repos/openhab/openhab2-addons/issues{/number}","pulls_url":"https://api.github.com/repos/openhab/openhab2-addons/pulls{/number}","milestones_url":"https://api.github.com/repos/openhab/openhab2-addons/milestones{/number}","notifications_url":"https://api.github.com/repos/openhab/openhab2-addons/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/openhab/openhab2-addons/labels{/name}","releases_url":"https://api.github.com/repos/openhab/openhab2-addons/releases{/id}","deployments_url":"https://api.github.com/repos/openhab/openhab2-addons/deployments","created_at":"2014-05-13T19:41:58Z","updated_at":"2017-04-07T06:25:53Z","pushed_at":"2017-04-06T21:18:28Z","git_url":"git://github.com/openhab/openhab2-addons.git","ssh_url":"git@github.com:openhab/openhab2-addons.git","clone_url":"https://github.com/openhab/openhab2-addons.git","svn_url":"https://github.com/openhab/openhab2-addons","homepage":"","size":76736,"stargazers_count":555,"watchers_count":555,"language":"Java","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":922,"mirror_url":null,"open_issues_count":310,"forks":922,"open_issues":310,"watchers":555,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/openhab/openhab2-addons/pulls/2101"},"html":{"href":"https://github.com/openhab/openhab2-addons/pull/2101"},"issue":{"href":"https://api.github.com/repos/openhab/openhab2-addons/issues/2101"},"comments":{"href":"https://api.github.com/repos/openhab/openhab2-addons/issues/2101/comments"},"review_comments":{"href":"https://api.github.com/repos/openhab/openhab2-addons/pulls/2101/comments"},"review_comment":{"href":"https://api.github.com/repos/openhab/openhab2-addons/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/openhab/openhab2-addons/pulls/2101/commits"},"statuses":{"href":"https://api.github.com/repos/openhab/openhab2-addons/statuses/c23edf097ebc3e0fff06731694b4efecef9092f5"}}}}
{"actor":{"display_login":"martinvw"}}