diff --git a/app/lib/commons-codec-1.2.jar b/app/lib/commons-codec-1.2.jar new file mode 100644 index 000000000..cce1e07c7 Binary files /dev/null and b/app/lib/commons-codec-1.2.jar differ diff --git a/app/lib/commons-httpclient-3.1.jar b/app/lib/commons-httpclient-3.1.jar new file mode 100644 index 000000000..7c59774ae Binary files /dev/null and b/app/lib/commons-httpclient-3.1.jar differ diff --git a/app/lib/commons-logging-1.0.4.jar b/app/lib/commons-logging-1.0.4.jar new file mode 100644 index 000000000..b73a80fab Binary files /dev/null and b/app/lib/commons-logging-1.0.4.jar differ diff --git a/app/src/processing/app/Preferences.java b/app/src/processing/app/Preferences.java index 23ca12287..4e5ff6d76 100644 --- a/app/src/processing/app/Preferences.java +++ b/app/src/processing/app/Preferences.java @@ -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; diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index 11f7034f6..c89bca930 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -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; } diff --git a/app/src/processing/app/debug/HttpUploader.java b/app/src/processing/app/debug/HttpUploader.java new file mode 100644 index 000000000..a8e96566e --- /dev/null +++ b/app/src/processing/app/debug/HttpUploader.java @@ -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"); + } + +} diff --git a/app/src/processing/app/debug/UploaderFactory.java b/app/src/processing/app/debug/UploaderFactory.java new file mode 100644 index 000000000..a31e6f82b --- /dev/null +++ b/app/src/processing/app/debug/UploaderFactory.java @@ -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 preferences) { + if ("http".equals(preferences.get("upload.tool"))) { + return new HttpUploader(); + } + + return new BasicUploader(); + } + +} diff --git a/app/test/processing/app/AbstractWithPreferencesTest.java b/app/test/processing/app/AbstractWithPreferencesTest.java new file mode 100644 index 000000000..008df0b9f --- /dev/null +++ b/app/test/processing/app/AbstractWithPreferencesTest.java @@ -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); + } +} diff --git a/app/test/processing/app/debug/UploaderFactoryTest.java b/app/test/processing/app/debug/UploaderFactoryTest.java new file mode 100644 index 000000000..e8300689b --- /dev/null +++ b/app/test/processing/app/debug/UploaderFactoryTest.java @@ -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 prefs = new HashMap(); + prefs.put("upload.tool", "http"); + Uploader uploader = new UploaderFactory().newUploader(prefs); + + assertTrue(uploader instanceof HttpUploader); + } + + @Test + public void shouldCreateAnInstanceOfBasicUploader() throws Exception { + Map prefs = new HashMap(); + prefs.put("upload.tool", "whatever"); + Uploader uploader = new UploaderFactory().newUploader(prefs); + + assertTrue(uploader instanceof BasicUploader); + } +}