Some refactoring and added Network check before download to see if it solves issue with DNS lookup freeze.

This commit is contained in:
Ziver Koc 2014-09-18 17:43:39 +02:00
parent 1eb006c1fe
commit 2941e5d3cc
4 changed files with 57 additions and 15 deletions

View file

@ -1,6 +1,7 @@
package com.ericsson.uecontrol.core.behaviour;
import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.core.util.UrlUtil;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable;
import org.apache.log4j.Logger;
@ -31,11 +32,7 @@ public class UeBehaviourFileDownload extends UeBehaviour {
log.debug("Downloading file: " + url);
byte[] data = new byte[BUFFER_SIZE];
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(3000);
connection.setUseCaches(false);
connection.connect();
URLConnection connection = UrlUtil.getURLConnection(url);
InputStream in = new BufferedInputStream(connection.getInputStream());
long progress = 0;

View file

@ -2,6 +2,7 @@ package com.ericsson.uecontrol.core.behaviour;
import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.core.util.ThroughputCalculator;
import com.ericsson.uecontrol.core.util.UrlUtil;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable;
import org.apache.log4j.Logger;
@ -39,10 +40,7 @@ public class UeBehaviourFtpUpload extends UeBehaviour {
log.debug("Uploading file: " + url);
byte[] data = new byte[BUFFER_SIZE];
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(3000);
connection.connect();
URLConnection connection = UrlUtil.getURLConnection(url);
OutputStream out = new BufferedOutputStream(connection.getOutputStream());
int total = 0;

View file

@ -1,6 +1,7 @@
package com.ericsson.uecontrol.core.behaviour;
import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.core.util.UrlUtil;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable;
import org.apache.log4j.Logger;
@ -71,16 +72,17 @@ public class UeBehaviourSurfing extends UeBehaviour {
long totalRead = 0;
for(int i=0; i<urlList.size(); i++) {
if(!UrlUtil.isNetworkAvailable()){
try { Thread.sleep(100); } catch (InterruptedException e) { }
continue;
}
InputStream in = null;
try {
URLContainer cont = urlList.get(i);
log.debug("Downloading: " + cont.url);
URLConnection connection = cont.url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(3000);
connection.setUseCaches(false);
connection.connect();
InputStream in = new BufferedInputStream(connection.getInputStream());
URLConnection connection = UrlUtil.getURLConnection(cont.url);
in = new BufferedInputStream(connection.getInputStream());
if (estimatedDataLength < 0)
estimatedDataLength = in.available();
@ -104,6 +106,8 @@ public class UeBehaviourSurfing extends UeBehaviour {
}catch(IOException e){
log.warn(null, e);
if(retException == null) retException = e;
}finally {
if(in != null) in.close();
}
}
@ -113,6 +117,7 @@ public class UeBehaviourSurfing extends UeBehaviour {
throw retException;
}
private static final Pattern[] PARSABLE_CONTENT_INCLUSION_PATTERNS = new Pattern[]{
// HTML
Pattern.compile("<iframe.* src=\"(.*?)\"", Pattern.CASE_INSENSITIVE),

View file

@ -0,0 +1,42 @@
package com.ericsson.uecontrol.core.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.ericsson.uecontrol.gui.MainActivity;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
/**
* Created by ezivkoc on 2014-09-18.
*/
public class UrlUtil {
public static URLConnection getURLConnection(URL url) throws IOException {
if(!isNetworkAvailable())
throw new IOException("No Data Network Available");
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(3000);
connection.setUseCaches(false);
connection.connect();
return connection;
}
public static boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager)
MainActivity.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
// if no network is available networkInfo will be null
// otherwise check if we are connected
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
}