Added local file upload for FTP behaviour. [artf472311]

Fixed config dialog parameter order and new line for longer labels
This commit is contained in:
Ziver Koc 2015-01-15 15:52:05 +01:00
parent d9c924009d
commit e18c09b39d
4 changed files with 77 additions and 37 deletions

View file

@ -16,13 +16,13 @@ public class UeBehaviourFtpDownload extends UeBehaviourFtp {
private static final Logger log = Logger.getLogger(UeBehaviourFtpDownload.class); private static final Logger log = Logger.getLogger(UeBehaviourFtpDownload.class);
private static final int BUFFER_SIZE = 512; private static final int BUFFER_SIZE = 512;
@Configurable("Host") @Configurable(order=1, value="Host")
private String host = "ftp.otenet.gr"; private String host = "ftp.otenet.gr";
@Configurable("File Path") @Configurable(order=2, value="File Path")
private String filePath = "/test1Mb.db"; private String filePath = "/test1Mb.db";
@Configurable("Username") @Configurable(order=3, value="Username")
private String username = "speedtest"; private String username = "speedtest";
@Configurable("Password") @Configurable(order=4, value="Password")
private String password = "speedtest"; private String password = "speedtest";
private transient long estimatedDataLength = -1; private transient long estimatedDataLength = -1;

View file

@ -1,20 +1,13 @@
package com.ericsson.uecontrol.core.behaviour; 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.ThroughputCalculator;
import com.ericsson.uecontrol.core.util.UrlUtil;
import com.ericsson.uecontrol.gui.util.Configurator.Configurable; import com.ericsson.uecontrol.gui.util.Configurator.Configurable;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import java.io.BufferedOutputStream; import java.io.*;
import java.io.IOException; import java.util.regex.Pattern;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
/** /**
* This behaviour simulates downloading a file from FTP * This behaviour simulates downloading a file from FTP
@ -22,37 +15,35 @@ import java.net.URLConnection;
* Created by ezivkoc on 2014-07-15. * Created by ezivkoc on 2014-07-15.
*/ */
public class UeBehaviourFtpUpload extends UeBehaviourFtp { public class UeBehaviourFtpUpload extends UeBehaviourFtp {
private static final int BUFFER_SIZE = 512;
private static final Logger log = Logger.getLogger(UeBehaviourFtpUpload.class); private static final Logger log = Logger.getLogger(UeBehaviourFtpUpload.class);
private static final int BUFFER_SIZE = 512;
@Configurable("Host")
@Configurable(order=1, value="Host")
private String host; private String host;
@Configurable("File Path") @Configurable(order=2, value="Remote Path")
private String filePath; private String remotePath;
@Configurable("Username") @Configurable(order=3, value="Username")
private String username; private String username;
@Configurable("Password") @Configurable(order=4, value="Password")
private String password; private String password;
@Configurable("File Size(Bytes)") @Configurable(order=5, value="Random data Size(Bytes) or Local File Path")
private int size; private String sizeOrFile;
@Override @Override
protected void executeFtp(FTPClient ftp) throws IOException { protected void executeFtp(FTPClient ftp) throws IOException {
log.debug("Uploading to FTP file: " + getFtpUrl(filePath)); log.debug("Uploading to FTP file: " + getFtpUrl(remotePath));
OutputStream outStream = ftp.storeFileStream(filePath); OutputStream outStream = ftp.storeFileStream(remotePath);
if (outStream != null) { if (outStream != null) {
OutputStream out = new BufferedOutputStream(outStream); OutputStream out = new BufferedOutputStream(outStream);
// Upload data if(sizeOrFile != null && Pattern.matches("\\d*", sizeOrFile)){
int total = 0; int size = Integer.parseInt(sizeOrFile);
byte[] data = new byte[BUFFER_SIZE]; uploadRandomData(out, size);
while (total < size && !stopExecution()) { }
int writeLength = (total + data.length < size ? data.length : size - total); else{
out.write(data, 0, writeLength); uploadLocalFile(out, new File(sizeOrFile));
total += writeLength;
super.setProgress((float) total / size);
} }
out.close(); out.close();
@ -64,7 +55,38 @@ public class UeBehaviourFtpUpload extends UeBehaviourFtp {
} }
} }
private void uploadRandomData(OutputStream out, int size) throws IOException {
// Upload data
log.debug("Uploading random data with size "+size);
int total = 0;
byte[] data = new byte[BUFFER_SIZE];
while (total < size && !stopExecution()) {
int writeLength = (total + data.length < size ? data.length : size - total);
out.write(data, 0, writeLength);
total += writeLength;
super.setProgress((float) total / size);
}
}
private void uploadLocalFile(OutputStream out, File file) throws IOException {
log.debug("Uploading local file "+file.getAbsolutePath());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
try {
// Upload file
int writeLength = 0;
int total = 0;
byte[] data = new byte[BUFFER_SIZE];
while ((writeLength = in.read(data)) > 0 && !stopExecution()) {
out.write(data, 0, writeLength);
total += writeLength;
super.setProgress((float) total / file.length());
}
} finally {
in.close();
}
}
@Override @Override
public String getHost() { public String getHost() {
@ -87,7 +109,11 @@ public class UeBehaviourFtpUpload extends UeBehaviourFtp {
@Override @Override
public String toString() { public String toString() {
return "Will upload a "+ ThroughputCalculator.getByteString(size) +" file."; if(sizeOrFile != null && Pattern.matches("\\d*", sizeOrFile)){
int size = Integer.parseInt(sizeOrFile);
return "Will upload "+ ThroughputCalculator.getByteString(size) +" of random data.";
}
return "Will upload local file "+ sizeOrFile;
} }
} }

View file

@ -50,7 +50,8 @@ public class ConfigureDialog extends DialogFragment {
confer = new Configurator(behaviour); confer = new Configurator(behaviour);
for(ConfigurationParam confParam : confer.getConfiguration()){ for(ConfigurationParam confParam : confer.getConfiguration()){
LinearLayout layout = new LinearLayout(context); LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.HORIZONTAL); layout.setOrientation(
(confParam.getNiceName().length() < 25 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL));
layout.setLayoutParams(new LinearLayout.LayoutParams( layout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT)); LinearLayout.LayoutParams.MATCH_PARENT));

View file

@ -9,6 +9,7 @@ import java.lang.annotation.Target;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
/** /**
* Created by ezivkoc on 2014-07-24. * Created by ezivkoc on 2014-07-24.
@ -22,6 +23,7 @@ public class Configurator {
@Target({ElementType.FIELD}) // This annotation can only be applied to class fields. @Target({ElementType.FIELD}) // This annotation can only be applied to class fields.
public static @interface Configurable{ public static @interface Configurable{
String value(); String value();
int order() default Integer.MAX_VALUE;
} }
public static enum ConfigType{ public static enum ConfigType{
@ -47,7 +49,8 @@ public class Configurator {
Field[] all = c.getDeclaredFields(); Field[] all = c.getDeclaredFields();
for(Field f : all){ for(Field f : all){
if(f.isAnnotationPresent(Configurable.class) && !Modifier.isStatic(f.getModifiers())) { if(f.isAnnotationPresent(Configurable.class) &&
!Modifier.isStatic(f.getModifiers()) && !Modifier.isTransient(f.getModifiers())) {
try { try {
conf.add(new ConfigurationParam(f)); conf.add(new ConfigurationParam(f));
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
@ -56,7 +59,9 @@ public class Configurator {
} }
} }
return conf.toArray(new ConfigurationParam[conf.size()]); ConfigurationParam[] list = conf.toArray(new ConfigurationParam[conf.size()]);
Arrays.sort(list);
return list;
} }
public void setConfiguration(){ public void setConfiguration(){
@ -70,12 +75,13 @@ public class Configurator {
} }
public class ConfigurationParam{ public class ConfigurationParam implements Comparable<ConfigurationParam>{
protected Field field; protected Field field;
protected String name; protected String name;
protected String niceName; protected String niceName;
protected ConfigType type; protected ConfigType type;
protected Object value; protected Object value;
protected int order;
protected ConfigurationParam(Field f) throws IllegalAccessException { protected ConfigurationParam(Field f) throws IllegalAccessException {
@ -84,6 +90,7 @@ public class Configurator {
name = field.getName(); name = field.getName();
niceName = field.getAnnotation(Configurable.class).value(); niceName = field.getAnnotation(Configurable.class).value();
value = field.get(obj); value = field.get(obj);
order = field.getAnnotation(Configurable.class).order();
if (f.getType() == String.class) type = ConfigType.STRING; if (f.getType() == String.class) type = ConfigType.STRING;
else if(f.getType() == int.class) type = ConfigType.INT; else if(f.getType() == int.class) type = ConfigType.INT;
@ -111,5 +118,11 @@ public class Configurator {
protected void set() throws IllegalAccessException { protected void set() throws IllegalAccessException {
field.set(obj, value); field.set(obj, value);
} }
@Override
public int compareTo(ConfigurationParam configurationParam) {
return this.order - configurationParam.order;
}
} }
} }