Added new logic behaviours (Iterator, Stop)

[artf473055]
This commit is contained in:
Ziver Koc 2015-01-19 17:52:09 +01:00
parent b07ac93e35
commit fef55a5dd4
9 changed files with 203 additions and 56 deletions

View file

@ -11,33 +11,36 @@ import java.io.Serializable;
*/
public abstract class UeBehaviour implements Serializable{
private static final Logger log = Logger.getLogger(UeBehaviour.class);
/** A sleep period for visual queue, mainly for behaviours that instantly finnish **/
public static final int VISUAL_SLEEP_PERIOD = 200;
private transient boolean running;
private transient long execTime = -1;
private transient BehaviourExecutionListener execListener;
private transient UeControlExecutor executor;
public synchronized void preRun() {
running = true;
}
public synchronized void preRun() { }
/**
* Starts to run the behaviour, this method will block until the execution is done
*/
public void run(){
public synchronized void run(UeControlExecutor exec){
execTime = -1;
long startTime = System.currentTimeMillis();
this.executor = exec;
if(execListener != null)
execListener.executionStarted();
setProgress(0);
long startTime = System.currentTimeMillis();
try {
setProgress(0);
execute();
} catch(Exception e){
if(execListener != null) execListener.exception(e);
log.warn(null, e);
}
running = false;
execTime = System.currentTimeMillis() - startTime;
executor = null;
if(execListener != null)
execListener.executionStopped(execTime);
}
@ -46,7 +49,7 @@ public abstract class UeBehaviour implements Serializable{
* Will stop the currently running behaviour
*/
public void terminate(){
running = false;
executor = null;
}
public void reset(){
@ -62,14 +65,18 @@ public abstract class UeBehaviour implements Serializable{
}
public boolean isRunning(){
return running;
return executor != null;
}
/**
* @return if currently running behaviour should be terminated
*/
protected boolean stopExecution(){
return !running;
return !isRunning();
}
protected UeControlExecutor getExecutor(){
return executor;
}

View file

@ -36,6 +36,7 @@ public class UeControlExecutor implements Runnable{
private Handler handler;
private ThroughputUpdateRunnable throughputUpdateRunnable;
private ThroughputListener throughputListener;
private ExecutionListener execListener;
public UeControlExecutor(){
@ -90,7 +91,7 @@ public class UeControlExecutor implements Runnable{
try{ Thread.sleep(20); }catch(Exception e){log.error(null,e);}
}
private void terminateNonBlock(){
public void terminateNonBlock(){
terminate = true;
handler.removeCallbacks(throughputUpdateRunnable);
if(currentlyActive != null)
@ -105,14 +106,14 @@ public class UeControlExecutor implements Runnable{
try {
thread.wait();
while(isRunning()) // Just to make sure it has stopped
Thread.sleep(10);
Thread.sleep(100);
}catch(InterruptedException e) {log.error(null,e);}
}
}
}
public synchronized void reset(){
terminate();
terminateNonBlock();
currentlyActive = null;
for(int i=0; i<behaviours.size(); i++){
behaviours.get(i).reset();
@ -127,6 +128,8 @@ public class UeControlExecutor implements Runnable{
public void run(){
log.info("Starting execution");
if(execListener != null)
execListener.executionStarted();
if(csvPath != null)
csvLogger = new CSVWriter(csvPath);
while(!terminate) {
@ -144,11 +147,15 @@ public class UeControlExecutor implements Runnable{
currentlyActive.preRun();
thread.notifyAll();
}
// Run behaviour
log.info("Running behaviour: " + currentlyActive.getName());
currentlyActive.run();
if(!terminate) {
// Run behaviour
UeBehaviour execBehaviour = currentlyActive;
log.info("Running behaviour: " + currentlyActive.getName());
execBehaviour.run(this);
// Before deciding on the next behaviour check if currentlyActive
// has been changed by the previous behaviour
if(!terminate && execBehaviour == currentlyActive) {
int index = behaviours.indexOf(currentlyActive) + 1;
if (index < behaviours.size())
currentlyActive = behaviours.get(index);
@ -161,9 +168,16 @@ public class UeControlExecutor implements Runnable{
csvLogger = null;
}
log.info("Execution completed");
if(execListener != null)
execListener.executionStopped();
synchronized (thread){thread.notifyAll();}
}
public void setNextBehaviour(int i) {
if(0 <= i && i < behaviours.size())
currentlyActive = behaviours.get(i);
}
/**
* Set the log path or null to disable logging
*/
@ -173,6 +187,9 @@ public class UeControlExecutor implements Runnable{
public void setThroughputListener(ThroughputListener listener){
throughputListener = listener;
}
public void setExecutionListener(ExecutionListener listener){
execListener = listener;
}
public void setDeviceBasedThroughput(boolean enable){
throughputUpdateRunnable.deviceBasedThroughput = enable;
}
@ -188,11 +205,16 @@ public class UeControlExecutor implements Runnable{
}
public static interface ThroughputListener{
public void throughputUpdate(double downThroughput, double upThroughput);
public static interface ExecutionListener {
public void executionStarted();
public void executionStopped();
}
public static interface ThroughputListener{
public void throughputUpdate(double downThroughput, double upThroughput);
}
private class ThroughputUpdateRunnable implements Runnable{
protected boolean deviceBasedThroughput;

View file

@ -0,0 +1,49 @@
package com.ericsson.uecontrol.core.logic;
import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.core.util.Configurator.Configurable;
import org.apache.log4j.Logger;
/**
* Created by ezivkoc on 2015-01-19.
*/
public class UeBehaviourIterator extends UeBehaviour {
private static final Logger log = Logger.getLogger(UeBehaviourIterator.class);
@Configurable(order=1, value="Iterations")
private int iterations = 5;
private transient int iterationCount = 0;
@Override
protected void execute() throws Exception {
iterationCount++;
if(iterationCount < iterations){
log.debug("Iteration: "+ iterationCount);
super.getExecutor().setNextBehaviour(0);
Thread.sleep(VISUAL_SLEEP_PERIOD); // Sleep as a visual queue
}
else{
log.debug("Iteration done, skipping");
}
}
@Override
public void reset() {
iterationCount = 0;
super.reset();
}
@Override
public String getName() {
return "Iterator";
}
@Override
public String toString() {
return "Will iterate " +iterations+ " times";
}
}

View file

@ -0,0 +1,34 @@
package com.ericsson.uecontrol.core.logic;
import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.core.util.Configurator.Configurable;
import org.apache.log4j.Logger;
/**
* Created by ezivkoc on 2015-01-19.
*/
public class UeBehaviourStop extends UeBehaviour {
private static final Logger log = Logger.getLogger(UeBehaviourStop.class);
@Override
protected void execute() throws Exception {
log.debug("Terminating execution");
super.getExecutor().setNextBehaviour(0);
log.debug("Resetting executor");
super.getExecutor().reset();
Thread.sleep(VISUAL_SLEEP_PERIOD); // Sleep as a visual queue
}
@Override
public String getName() {
return "Stop";
}
@Override
public String toString() {
return "Will stop execution";
}
}

View file

@ -29,7 +29,7 @@ public class Configurator {
}
public static enum ConfigType{
STRING, INT
STRING, INT, BOOLEAN
}
@ -96,6 +96,7 @@ public class Configurator {
if (f.getType() == String.class) type = ConfigType.STRING;
else if(f.getType() == int.class) type = ConfigType.INT;
else if(f.getType() == boolean.class)type = ConfigType.BOOLEAN;
}
@ -114,6 +115,8 @@ public class Configurator {
value = v; break;
case INT:
value = Integer.parseInt(v); break;
case BOOLEAN:
value = Boolean.parseBoolean(v); break;
}
}

View file

@ -17,6 +17,7 @@ import android.widget.Toast;
import com.ericsson.uecontrol.R;
import com.ericsson.uecontrol.core.UeControlExecutor;
import com.ericsson.uecontrol.core.UeControlExecutor.ExecutionListener;
import com.ericsson.uecontrol.core.behaviour.UeBehaviourSleep;
import com.ericsson.uecontrol.core.behaviour.UeBehaviourSurfing;
import com.ericsson.uecontrol.gui.fragments.BehaviourListFragment;
@ -34,7 +35,7 @@ import de.mindpipe.android.logging.log4j.LogConfigurator;
public class MainActivity extends FragmentActivity implements OnSharedPreferenceChangeListener,
OnFileSelectionListener {
OnFileSelectionListener, ExecutionListener {
private static final Logger log = Logger.getLogger(MainActivity.class);
public static final String DEFAULT_LOG_PATH = "/sdcard/uecontrol/";
public static final String BEHAVIOUR_SAVE_FILE = "behaviour_list.json";
@ -103,6 +104,7 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
executor.setThroughputListener(statusFragment.getThroughputListener());
executor.setExecutionListener(this);
updateExecutionState();
}
@ -241,25 +243,42 @@ public class MainActivity extends FragmentActivity implements OnSharedPreference
}
}
private void updateExecutionState(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(action_execute != null) {
if (executor.isRunning()) {
if (executor.isRunning())
executionStarted();
else
executionStopped();
}
}
public void executionStarted(){
if(action_execute == null)
return;
this.runOnUiThread(new Runnable() {
@Override
public void run() {
action_execute.setTitle(R.string.action_stop);
ExecNotification.create();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
if(action_mark != null && prefs.getBoolean("logging", false))
action_mark.setEnabled(true);
if(prefs.getBoolean("screen_on", false))
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
else {
});
}
public void executionStopped(){
if(action_execute == null)
return;
this.runOnUiThread(new Runnable() {
@Override
public void run() {
action_execute.setTitle(R.string.action_run);
ExecNotification.dismiss();
action_mark.setEnabled(false);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
});
}

View file

@ -8,6 +8,7 @@ import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.ContextThemeWrapper;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
@ -16,6 +17,7 @@ import com.ericsson.uecontrol.R;
import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.gui.MainActivity;
import com.ericsson.uecontrol.core.util.Configurator;
import com.ericsson.uecontrol.core.util.Configurator.ConfigType;
import com.ericsson.uecontrol.core.util.Configurator.ConfigurationParam;
import java.util.HashMap;
@ -24,12 +26,12 @@ import java.util.HashMap;
* Created by ezivkoc on 2014-07-24.
*/
public class ConfigureDialog extends DialogFragment {
private HashMap<String, EditText> inputs;
private HashMap<String, Object> inputs;
private Configurator confer;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
inputs = new HashMap<String, EditText>();
inputs = new HashMap<String, Object>();
// Fix for CopyPaste bar not visible
Context context = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light);
@ -62,20 +64,33 @@ public class ConfigureDialog extends DialogFragment {
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
layout.addView(label);
if(confParam.getType() == ConfigType.STRING || confParam.getType() == ConfigType.INT) {
EditText input = new EditText(context);
input.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
// Set previous value
if (savedInstanceState != null && savedInstanceState.containsKey(confParam.getName()))
input.setText(savedInstanceState.getString(confParam.getName()), TextView.BufferType.EDITABLE);
else
input.setText(confParam.getString(), TextView.BufferType.EDITABLE);
EditText input = new EditText(context);
input.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
// Set previous value
if(savedInstanceState != null && savedInstanceState.containsKey(confParam.getName()))
input.setText(savedInstanceState.getString(confParam.getName()), TextView.BufferType.EDITABLE);
else
input.setText(confParam.getString(), TextView.BufferType.EDITABLE);
// store inputs for later use
inputs.put(confParam.getName(), input);
layout.addView(input);
// store inputs for later use
inputs.put(confParam.getName(), input);
layout.addView(input);
}
else if(confParam.getType() == ConfigType.BOOLEAN) {
CheckBox input = new CheckBox(context);
// Set previous value
if (savedInstanceState != null && savedInstanceState.containsKey(confParam.getName()))
input.setChecked(Boolean.parseBoolean(savedInstanceState.getString(confParam.getName())));
else
input.setChecked(Boolean.parseBoolean(confParam.getString()));
// store inputs for later use
inputs.put(confParam.getName(), input);
layout.addView(input);
}
root.addView(layout);
}
@ -83,8 +98,11 @@ public class ConfigureDialog extends DialogFragment {
builder.setPositiveButton(R.string.action_save, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
for(ConfigurationParam confParam : confer.getConfiguration()){
confParam.setString(inputs.get(confParam.getName()).getText().toString());
for (ConfigurationParam confParam : confer.getConfiguration()) {
if(inputs.get(confParam.getName()) instanceof EditText)
confParam.setString(((EditText)inputs.get(confParam.getName())).getText().toString());
else if(inputs.get(confParam.getName()) instanceof CheckBox)
confParam.setString(""+ ((CheckBox)inputs.get(confParam.getName())).isChecked());
}
confer.setConfiguration();
}
@ -103,7 +121,10 @@ public class ConfigureDialog extends DialogFragment {
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
for(String key : inputs.keySet()){
outState.putString(key, inputs.get(key).getText().toString());
if(inputs.get(key) instanceof EditText)
outState.putString(key, ((EditText)inputs.get(key)).getText().toString());
else if(inputs.get(key) instanceof CheckBox)
outState.putString(key, ""+ ((CheckBox)inputs.get(key)).isChecked());
}
}
}

View file

@ -10,6 +10,9 @@
<item>com.ericsson.uecontrol.core.behaviour.UeBehaviourSpeechCall</item>
<item>com.ericsson.uecontrol.core.behaviour.UeBehaviourSurfing</item>
<item>com.ericsson.uecontrol.core.behaviour.UeBehaviourVideoStreaming</item>
<item>com.ericsson.uecontrol.core.logic.UeBehaviourIterator</item>
<item>com.ericsson.uecontrol.core.logic.UeBehaviourStop</item>
</string-array>
<string-array name="throughput_types">