From 95dce7bab2cc9738b7e4185302f3fe1a44fbc8dd Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Fri, 8 Feb 2019 15:32:09 +0100 Subject: Increase polling timeout; fix error handling --- .../org/openslx/dnbd3/status/poller/ServerPoller.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/openslx/dnbd3/status/poller/ServerPoller.java b/src/main/java/org/openslx/dnbd3/status/poller/ServerPoller.java index 97b5f5a..492e371 100644 --- a/src/main/java/org/openslx/dnbd3/status/poller/ServerPoller.java +++ b/src/main/java/org/openslx/dnbd3/status/poller/ServerPoller.java @@ -33,13 +33,13 @@ public class ServerPoller public Status update() { HttpURLConnection con = null; - InputStream is; + InputStream is = null; try { con = (HttpURLConnection)new URL( this.server ).openConnection(); con.setRequestMethod( "GET" ); - con.setConnectTimeout( 1000 ); - con.setReadTimeout( 2000 ); + con.setConnectTimeout( 2000 ); + con.setReadTimeout( 4000 ); if ( con.getResponseCode() != HttpURLConnection.HTTP_OK ) { return null; @@ -47,8 +47,12 @@ public class ServerPoller is = con.getInputStream(); } catch ( java.net.SocketTimeoutException e ) { + System.out.println( "Timeout " + this.server ); + NanoHTTPD.safeClose( is ); return null; } catch ( java.io.IOException e ) { + System.out.println( "IOException " + this.server ); + NanoHTTPD.safeClose( is ); return null; } // Now read data @@ -61,10 +65,17 @@ public class ServerPoller } NanoHTTPD.safeClose( isr ); NanoHTTPD.safeClose( is ); - NanoHTTPD.safeClose( con.getErrorStream() ); status.setAddress( address ); status.setTimestamp( System.currentTimeMillis() ); } catch ( Exception e ) { + try { + InputStream es = con.getErrorStream(); + while ( es.read( buffer ) > 0 ) { + // Nothingness + } + NanoHTTPD.safeClose( es ); + } catch ( Exception e2 ) { + } e.printStackTrace(); status = null; } -- cgit v1.2.3-55-g7522