first upload code

This commit is contained in:
Federico Fissore 2013-04-05 17:05:21 +02:00
parent 6b6e9248d1
commit e6468f0387
9 changed files with 145 additions and 15 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -218,7 +218,7 @@ public class Preferences {
// data model
static Hashtable defaults;
static Hashtable table = new Hashtable();;
static Hashtable table = new Hashtable();
static File preferencesFile;

View File

@ -23,11 +23,8 @@
package processing.app;
import processing.app.debug.BasicUploader;
import processing.app.debug.*;
import processing.app.debug.Compiler;
import processing.app.debug.RunnerException;
import processing.app.debug.Sizer;
import processing.app.debug.Uploader;
import processing.app.helpers.PreferencesMap;
import processing.app.packages.Library;
import processing.app.packages.LibraryList;
@ -1659,22 +1656,19 @@ public class Sketch {
System.out.println(_("Low memory available, stability problems may occur"));
}
protected String upload(String buildPath, String suggestedClassName, boolean usingProgrammer)
throws RunnerException, SerialException {
protected String upload(String buildPath, String suggestedClassName, boolean usingProgrammer) throws RunnerException, SerialException {
Uploader uploader;
TargetPlatform target = Base.getTargetPlatform();
String board = Preferences.get("board");
// download the program
//
uploader = new BasicUploader();
boolean success = uploader.uploadUsingPreferences(buildPath,
suggestedClassName,
usingProgrammer);
Uploader uploader = new UploaderFactory().newUploader(target.getBoards().get(board));
boolean success = uploader.uploadUsingPreferences(buildPath, suggestedClassName, usingProgrammer);
return success ? suggestedClassName : null;
}
public boolean exportApplicationPrompt() throws IOException, RunnerException {
return false;
}

View File

@ -0,0 +1,75 @@
package processing.app.debug;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import processing.app.Preferences;
import processing.app.SerialException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import static processing.app.I18n._;
public class HttpUploader extends Uploader {
private final HttpClient client;
private final String ipAddress;
public HttpUploader() {
this.client = new HttpClient();
this.ipAddress = Preferences.get("serial.port").substring(0, Preferences.get("serial.port").indexOf(" "));
}
@Override
public boolean uploadUsingPreferences(String buildPath, String className, boolean usingProgrammer) throws RunnerException, SerialException {
if (usingProgrammer) {
System.err.println("Http upload using programmer not supported");
return false;
}
FilePart filePart;
try {
filePart = new FilePart("sketch.hex", new File(buildPath, className + ".hex"));
} catch (FileNotFoundException e) {
throw new RunnerException(e);
}
Part[] parts = {filePart};
PostMethod post = newPostMethod();
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
int statusCode;
try {
statusCode = client.executeMethod(post);
} catch (IOException e) {
throw new RunnerException(e);
}
if (statusCode == HttpStatus.SC_OK) {
System.out.println(_("Sketch uploaded"));
return true;
}
try {
System.err.println(post.getResponseBodyAsString());
return false;
} catch (IOException e) {
throw new RunnerException(e);
}
}
protected PostMethod newPostMethod() {
return new PostMethod("http://" + ipAddress + ":8000/upload");
}
@Override
public boolean burnBootloader() throws RunnerException {
throw new RunnerException("Can't burn bootloader via http");
}
}

View File

@ -0,0 +1,19 @@
package processing.app.debug;
import processing.app.Base;
import processing.app.Preferences;
import processing.app.helpers.Maps;
import java.util.Map;
public class UploaderFactory {
public Uploader newUploader(Map<String, String> preferences) {
if ("http".equals(preferences.get("upload.tool"))) {
return new HttpUploader();
}
return new BasicUploader();
}
}

View File

@ -0,0 +1,12 @@
package processing.app;
import org.junit.BeforeClass;
public abstract class AbstractWithPreferencesTest {
@BeforeClass
public static void setUp() throws Exception {
Base.initPlatform();
Preferences.init(null);
}
}

View File

@ -0,0 +1,30 @@
package processing.app.debug;
import org.junit.Test;
import processing.app.AbstractWithPreferencesTest;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertTrue;
public class UploaderFactoryTest extends AbstractWithPreferencesTest {
@Test
public void shouldCreateAnInstanceOfHttpUploader() throws Exception {
Map<String, String> prefs = new HashMap<String, String>();
prefs.put("upload.tool", "http");
Uploader uploader = new UploaderFactory().newUploader(prefs);
assertTrue(uploader instanceof HttpUploader);
}
@Test
public void shouldCreateAnInstanceOfBasicUploader() throws Exception {
Map<String, String> prefs = new HashMap<String, String>();
prefs.put("upload.tool", "whatever");
Uploader uploader = new UploaderFactory().newUploader(prefs);
assertTrue(uploader instanceof BasicUploader);
}
}