Decoupled throughput updates from behaviours

Optimized imports
This commit is contained in:
Ziver Koc 2014-09-16 15:56:32 +02:00
parent 9323c099ce
commit e00d1fcb62
17 changed files with 83 additions and 127 deletions

View file

@ -14,7 +14,6 @@ public abstract class UeBehaviour implements Serializable{
private transient boolean running;
private transient BehaviourExecutionListener execListener;
private transient DataHandledListener datahandledListener;
public synchronized void preRun() {
@ -58,23 +57,6 @@ public abstract class UeBehaviour implements Serializable{
protected void setProgress(float progress){
if(execListener != null)
execListener.progressChanged(progress);
// TODO: This is a dirty way of updating the throughput, should be changed
if(datahandledListener != null)
datahandledListener.handledIncomingData(0);
}
public void setDataHandledListener(DataHandledListener listener) {
datahandledListener = listener;
}
protected void setHandledIncomingData(long size){
if(datahandledListener != null)
datahandledListener.handledIncomingData(size);
}
protected void setHandledOutgoingData(long size){
if(datahandledListener != null)
datahandledListener.handledOutgoingData(size);
}
/**
@ -105,8 +87,4 @@ public abstract class UeBehaviour implements Serializable{
public void exception(Exception e);
}
public static interface DataHandledListener {
public void handledIncomingData(long size);
public void handledOutgoingData(long size);
}
}

View file

@ -1,6 +1,7 @@
package com.ericsson.uecontrol.core;
import android.net.TrafficStats;
import android.os.Handler;
import com.ericsson.uecontrol.core.util.AbstractElementAdapter;
import com.ericsson.uecontrol.core.util.ThroughputCalculator;
@ -11,15 +12,9 @@ import com.google.gson.reflect.TypeToken;
import org.apache.log4j.Logger;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Writer;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
@ -27,7 +22,7 @@ import java.util.List;
/**
* Created by ezivkoc on 2014-07-15.
*/
public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListener {
public class UeControlExecutor implements Runnable{
private static final Logger log = Logger.getLogger(UeControlExecutor.class);
private ArrayList<UeBehaviour> behaviours;
@ -36,19 +31,15 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
private Thread thread;
private Gson gson;
private long previousRxBytes = TrafficStats.UNSUPPORTED;
private long previousTxBytes = TrafficStats.UNSUPPORTED;
private boolean deviceBasedThroughput;
private ThroughputCalculator downloadSpeed;
private ThroughputCalculator uploadSpeed;
private Handler handler;
private ThroughputUpdateRunnable throughputUpdateRunnable;
private ThroughputListener throughputListener;
public UeControlExecutor(){
behaviours = new ArrayList<UeBehaviour>();
downloadSpeed = new ThroughputCalculator();
uploadSpeed = new ThroughputCalculator();
deviceBasedThroughput = false;
handler = new Handler();
throughputUpdateRunnable = new ThroughputUpdateRunnable();
gson = new GsonBuilder()
.registerTypeAdapter(UeBehaviour.class, new AbstractElementAdapter<UeBehaviour>())
@ -57,7 +48,6 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
public void addBehaviour(UeBehaviour b){
behaviours.add(b);
b.setDataHandledListener(this);
}
public void read(String file) throws Exception {
@ -78,8 +68,13 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
}
public synchronized void execute(){
if(!isRunning()) {
// Start throughput reader scheduler
handler.postDelayed(throughputUpdateRunnable, 100);
// Start Execution
terminate = false;
thread = new Thread(this);
thread.start();
@ -92,6 +87,7 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
}
private void terminateNonBlock(){
handler.removeCallbacks(throughputUpdateRunnable);
terminate = true;
if(currentlyActive != null)
currentlyActive.terminate();
@ -116,6 +112,8 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
currentlyActive = null;
}
public boolean isRunning(){
return thread != null && thread.isAlive();
}
@ -154,53 +152,12 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
}
@Override
public void handledIncomingData(long size) {
if (!setRealDataUsage()){
// TrafficStat unsupported so try to use estimation instead
downloadSpeed.setHandledData(size);
}
if (throughputListener != null && downloadSpeed.isUpdated())
throughputListener.throughputUpdate(downloadSpeed.getBitThroughput(), uploadSpeed.getBitThroughput());
}
@Override
public void handledOutgoingData(long size) {
if (!setRealDataUsage()) {
// TrafficStat unsupported so try to us estimation instead
uploadSpeed.setHandledData(size);
}
if (throughputListener != null && uploadSpeed.isUpdated())
throughputListener.throughputUpdate(downloadSpeed.getBitThroughput(), uploadSpeed.getBitThroughput());
}
protected boolean setRealDataUsage(){
boolean ret = false;
long currentRxBytes = 0;
long currentTxBytes = 0;
if(deviceBasedThroughput) {
currentRxBytes = TrafficStats.getTotalRxBytes();
currentTxBytes = TrafficStats.getTotalTxBytes();
}
else{
currentRxBytes = TrafficStats.getUidRxBytes(MainActivity.getUID());
currentTxBytes = TrafficStats.getUidTxBytes(MainActivity.getUID());
}
if (currentRxBytes != TrafficStats.UNSUPPORTED && currentTxBytes != TrafficStats.UNSUPPORTED
&& previousRxBytes != TrafficStats.UNSUPPORTED && previousTxBytes != TrafficStats.UNSUPPORTED){
downloadSpeed.setHandledData(currentRxBytes-previousRxBytes);
uploadSpeed.setHandledData(currentTxBytes-previousTxBytes);
ret = true;
}
previousRxBytes = currentRxBytes;
previousTxBytes = currentTxBytes;
return ret;
}
public void setThroughputListener(ThroughputListener listener){
throughputListener = listener;
}
public void setDeviceBasedThroughput(boolean enable){
deviceBasedThroughput = enable;
throughputUpdateRunnable.deviceBasedThroughput = enable;
}
public List<UeBehaviour> getBehaviourList() {
@ -214,4 +171,57 @@ public class UeControlExecutor implements Runnable, UeBehaviour.DataHandledListe
public static interface ThroughputListener{
public void throughputUpdate(double downThroughput, double upThroughput);
}
private class ThroughputUpdateRunnable implements Runnable{
protected boolean deviceBasedThroughput;
private long previousRxBytes = TrafficStats.UNSUPPORTED;
private long previousTxBytes = TrafficStats.UNSUPPORTED;
private ThroughputCalculator downloadSpeed;
private ThroughputCalculator uploadSpeed;
public ThroughputUpdateRunnable(){
downloadSpeed = new ThroughputCalculator();
uploadSpeed = new ThroughputCalculator();
deviceBasedThroughput = false;
}
public void run(){
if (!setRealDataUsage()) {
uploadSpeed.setHandledData(0);
downloadSpeed.setHandledData(0);
}
if (throughputListener != null && uploadSpeed.isUpdated())
throughputListener.throughputUpdate(downloadSpeed.getBitThroughput(), uploadSpeed.getBitThroughput());
// Rescedule this handler
handler.postDelayed(this, 100);
}
protected boolean setRealDataUsage(){
boolean ret = false;
long currentRxBytes = 0;
long currentTxBytes = 0;
if(deviceBasedThroughput) {
currentRxBytes = TrafficStats.getTotalRxBytes();
currentTxBytes = TrafficStats.getTotalTxBytes();
}
else{
currentRxBytes = TrafficStats.getUidRxBytes(MainActivity.getUID());
currentTxBytes = TrafficStats.getUidTxBytes(MainActivity.getUID());
}
if (currentRxBytes != TrafficStats.UNSUPPORTED && currentTxBytes != TrafficStats.UNSUPPORTED
&& previousRxBytes != TrafficStats.UNSUPPORTED && previousTxBytes != TrafficStats.UNSUPPORTED){
downloadSpeed.setHandledData(currentRxBytes - previousRxBytes);
uploadSpeed.setHandledData(currentTxBytes-previousTxBytes);
ret = true;
}
previousRxBytes = currentRxBytes;
previousTxBytes = currentTxBytes;
return ret;
}
}
}

View file

@ -41,7 +41,6 @@ public class UeBehaviourFileDownload extends UeBehaviour {
while((read = in.read(data)) != -1 && !stopExecution()){
progress += read;
super.setProgress((float)progress/estimatedDataLength);
super.setHandledIncomingData(read);
}
estimatedDataLength = progress;

View file

@ -1,15 +1,9 @@
package com.ericsson.uecontrol.core.behaviour;
import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
/**
* This behaviour simulates downloading a file from FTP
*

View file

@ -7,7 +7,6 @@ import com.ericsson.uecontrol.gui.util.Configurator.Configurable;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
@ -49,7 +48,6 @@ public class UeBehaviourFtpUpload extends UeBehaviour {
out.write(data, 0, writeLength);
total += writeLength;
super.setHandledOutgoingData(writeLength);
super.setProgress((float)total/size);
}
out.close();

View file

@ -6,12 +6,9 @@ import android.telephony.TelephonyManager;
import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.core.util.CallUtil;
import com.ericsson.uecontrol.gui.MainActivity;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable;
import org.apache.log4j.Logger;
import java.lang.reflect.InvocationTargetException;
/**
* This behaviour simulates an idle period for the device.
*

View file

@ -7,7 +7,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.widget.Toast;
import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.gui.MainActivity;

View file

@ -1,8 +1,6 @@
package com.ericsson.uecontrol.core.behaviour;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.TelephonyManager;
import com.ericsson.uecontrol.core.UeBehaviour;
@ -12,9 +10,6 @@ import com.ericsson.uecontrol.gui.util.Configurator.Configurable;
import org.apache.log4j.Logger;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* This behaviour simulates an idle period for the device.
*

View file

@ -1,7 +1,5 @@
package com.ericsson.uecontrol.core.behaviour;
import android.util.Log;
import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable;
@ -14,9 +12,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -93,7 +89,6 @@ public class UeBehaviourSurfing extends UeBehaviour {
return;
totalRead += read;
super.setProgress((float) totalRead / estimatedDataLength);
super.setHandledIncomingData(read);
if (cont.parsable)
content.append(new String(data, 0, read));
if (content.length() > MAX_BUFFER_SIZE)

View file

@ -9,7 +9,6 @@ import org.apache.log4j.Logger;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.net.MalformedURLException;
@ -51,7 +50,7 @@ public class UeBehaviourVideoStreaming extends UeBehaviour implements MediaPlaye
Thread.sleep(UeBehaviourSleep.SLEEP_PERIOD);
while (mp.isPlaying() && playbackException == null) {
super.setProgress((float) mp.getCurrentPosition() / mp.getDuration());
super.setHandledIncomingData(0); // Unable to retrieve the amount of incoming data
if (super.stopExecution()) break;
Thread.sleep(UeBehaviourSleep.SLEEP_PERIOD);
}

View file

@ -1,6 +1,14 @@
package com.ericsson.uecontrol.core.util;
import com.google.gson.*;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
public class AbstractElementAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T> {

View file

@ -10,7 +10,6 @@ import com.ericsson.uecontrol.BuildConfig;
import org.apache.log4j.Logger;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

View file

@ -14,13 +14,11 @@ import com.ericsson.uecontrol.core.UeControlExecutor;
import com.ericsson.uecontrol.gui.fragments.ConfigureDialog;
import com.ericsson.uecontrol.gui.fragments.SelectBehaviourDialog;
import com.ericsson.uecontrol.gui.util.BehaviourListAdapter;
import com.ericsson.uecontrol.gui.util.Configurator;
import com.ericsson.uecontrol.gui.util.DynamicListView;
import org.apache.log4j.Logger;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class EditActivity extends ListActivity implements AdapterView.OnItemClickListener {

View file

@ -1,11 +1,6 @@
package com.ericsson.uecontrol.gui.fragments;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
@ -33,6 +28,11 @@ import com.ericsson.uecontrol.R;
import org.apache.log4j.Logger;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/** Allow user to select destination directory and to enter filename.
*

View file

@ -11,9 +11,6 @@ import com.ericsson.uecontrol.core.UeBehaviour;
import org.apache.log4j.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SelectBehaviourDialog extends DialogFragment {
private static final Logger log = Logger.getLogger(SelectBehaviourDialog.class);
public static String[] behaviourClasses;

View file

@ -1,16 +1,9 @@
package com.ericsson.uecontrol.gui.fragments;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.TelephonyManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

View file

@ -6,9 +6,7 @@ import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.preference.PreferenceManager;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
@ -21,7 +19,6 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.List;
/**
* Created by ezivkoc on 2014-07-30.