2
2
3
3
import org .apache .commons .httpclient .HttpClient ;
4
4
import org .apache .commons .httpclient .HttpStatus ;
5
+ import org .apache .commons .httpclient .methods .GetMethod ;
5
6
import org .apache .commons .httpclient .methods .PostMethod ;
6
7
import org .apache .commons .httpclient .methods .multipart .FilePart ;
7
8
import org .apache .commons .httpclient .methods .multipart .MultipartRequestEntity ;
@@ -29,15 +30,16 @@ public class HttpUploader extends Uploader {
29
30
30
31
private final HttpClient client ;
31
32
private final String ipAddress ;
33
+ private final String baseUrl ;
32
34
33
35
public HttpUploader (String port ) {
34
36
this .client = new HttpClient ();
35
- this .client .getHttpConnectionManager ().getParams ().setConnectionTimeout (5000 );
36
37
Matcher matcher = IPV4_ADDRESS .matcher (port );
37
38
if (!matcher .find ()) {
38
39
throw new IllegalArgumentException (port );
39
40
}
40
41
this .ipAddress = matcher .group (1 );
42
+ this .baseUrl = "https://" + ipAddress ;
41
43
}
42
44
43
45
public boolean requiresAuthorization () {
@@ -55,6 +57,21 @@ public boolean uploadUsingPreferences(String buildPath, String className, boolea
55
57
return false ;
56
58
}
57
59
60
+ this .client .getHttpConnectionManager ().getParams ().setConnectionTimeout (5000 );
61
+
62
+ int sleptTimes = 1 ;
63
+ while (boardNotReady ()) {
64
+ try {
65
+ Thread .sleep (100 );
66
+ } catch (InterruptedException e ) {
67
+ throw new RunnerException (e );
68
+ }
69
+ if (sleptTimes >= 3 ) {
70
+ throw new RunnerException ("The board is not yet ready" );
71
+ }
72
+ sleptTimes += 1 ;
73
+ }
74
+
58
75
FilePart sketch ;
59
76
try {
60
77
sketch = new FilePart ("sketch" , new File (buildPath , className + ".hex" ));
@@ -88,8 +105,17 @@ public boolean uploadUsingPreferences(String buildPath, String className, boolea
88
105
}
89
106
}
90
107
108
+ protected boolean boardNotReady () {
109
+ GetMethod get = new GetMethod (baseUrl + "/ready" );
110
+ try {
111
+ return client .executeMethod (get ) != HttpStatus .SC_OK ;
112
+ } catch (IOException e ) {
113
+ return true ;
114
+ }
115
+ }
116
+
91
117
protected PostMethod newPostMethod () {
92
- return new PostMethod ("https://" + ipAddress + "/upload" );
118
+ return new PostMethod (baseUrl + "/upload" );
93
119
}
94
120
95
121
@ Override
0 commit comments