Changeset 529 in josm for trunk/src/org/openstreetmap/josm/io
- Timestamp:
- 2008-01-30T18:02:38+01:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java
r343 r529 13 13 import java.net.URL; 14 14 import java.net.UnknownHostException; 15 import java.net.SocketTimeoutException; 15 16 import java.util.Collection; 16 17 import java.util.LinkedList; … … 57 58 * does not want to send the data. 58 59 */ 60 private static final int MSECS_PER_SECOND = 1000; 61 private static final int SECONDS_PER_MINUTE = 60; 62 private static final int MSECS_PER_MINUTE = MSECS_PER_SECOND * SECONDS_PER_MINUTE; 63 64 long uploadStartTime; 65 public String timeLeft(int progress, int list_size) { 66 long now = System.currentTimeMillis(); 67 long elapsed = now - uploadStartTime; 68 if (elapsed == 0) 69 elapsed = 1; 70 float uploads_per_ms = (float)progress / elapsed; 71 float uploads_left = list_size - progress; 72 int ms_left = (int)(uploads_left / uploads_per_ms); 73 int minutes_left = ms_left / MSECS_PER_MINUTE; 74 int seconds_left = (ms_left / MSECS_PER_SECOND) % SECONDS_PER_MINUTE ; 75 String time_left_str = Integer.toString(minutes_left) + ":"; 76 if (seconds_left < 10) 77 time_left_str += "0"; 78 time_left_str += Integer.toString(seconds_left); 79 return time_left_str; 80 } 59 81 public void uploadOsm(Collection<OsmPrimitive> list) throws SAXException { 60 82 processed = new LinkedList<OsmPrimitive>(); … … 66 88 NameVisitor v = new NameVisitor(); 67 89 try { 90 uploadStartTime = System.currentTimeMillis(); 68 91 for (OsmPrimitive osm : list) { 69 92 if (cancel) 70 93 return; 71 94 osm.visit(v); 72 Main.pleaseWaitDlg.currentAction.setText(tr("Upload {0} {1} ({2})...", tr(v.className), v.name, osm.id)); 95 int progress = Main.pleaseWaitDlg.progress.getValue(); 96 String time_left_str = timeLeft(progress, list.size()); 97 Main.pleaseWaitDlg.currentAction.setText(tr("Upload {0} {1} (id: {2}) {3}% {4}/{5} ({6} left)...", 98 tr(v.className), v.name, osm.id, 100.0*progress/list.size(), progress, list.size(), time_left_str)); 73 99 osm.visit(this); 74 100 Main.pleaseWaitDlg.progress.setValue(Main.pleaseWaitDlg.progress.getValue()+1); 101 Main.pleaseWaitDlg.progress.setValue(progress+1); 75 102 } 76 103 } catch (RuntimeException e) { … … 150 177 * <code>false</code>, if only the id is encoded. 151 178 */ 152 private void sendRequest (String requestMethod, String urlSuffix,153 OsmPrimitive osm, boolean addBody ) {179 private void sendRequestRetry(String requestMethod, String urlSuffix, 180 OsmPrimitive osm, boolean addBody, int retries) { 154 181 try { 182 if (cancel) 183 return; // assume cancel 155 184 String version = Main.pref.get("osm-server.version", "0.5"); 156 185 URL url = new URL( … … 159 188 "/" + urlSuffix + 160 189 "/" + (osm.id==0 ? "create" : osm.id)); 161 System.out.print ln("upload to: "+url);190 System.out.print("upload to: "+url+ "..." ); 162 191 activeConnection = (HttpURLConnection)url.openConnection(); 163 192 activeConnection.setConnectTimeout(15000); … … 166 195 activeConnection.setDoOutput(true); 167 196 activeConnection.connect(); 168 197 System.out.println("connected"); 169 198 if (addBody) { 170 199 OutputStream out = activeConnection.getOutputStream(); … … 181 210 if (retCode == 410 && requestMethod.equals("DELETE")) 182 211 return; // everything fine.. was already deleted. 183 if (retCode != 200) { 184 // Look for a detailed error message from the server 185 if (activeConnection.getHeaderField("Error") != null) 186 retMsg += "\n" + activeConnection.getHeaderField("Error"); 187 188 // Report our error 189 ByteArrayOutputStream o = new ByteArrayOutputStream(); 190 OsmWriter.output(o, new OsmWriter.Single(osm, true)); 191 System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 192 throw new RuntimeException(retCode+" "+retMsg); 212 if (retCode != 200 && retCode != 412) { 213 if (retries >= 0) { 214 retries--; 215 System.out.print("backing off for 10 seconds..."); 216 Thread.sleep(10000); 217 System.out.println("retrying ("+retries+" left)"); 218 sendRequestRetry(requestMethod, urlSuffix, osm, addBody, retries); 219 } else { 220 // Look for a detailed error message from the server 221 if (activeConnection.getHeaderField("Error") != null) 222 retMsg += "\n" + activeConnection.getHeaderField("Error"); 223 224 // Report our error 225 ByteArrayOutputStream o = new ByteArrayOutputStream(); 226 OsmWriter.output(o, new OsmWriter.Single(osm, true)); 227 System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 228 throw new RuntimeException(retCode+" "+retMsg); 229 } 193 230 } 194 231 } catch (UnknownHostException e) { 195 232 throw new RuntimeException(tr("Unknown host")+": "+e.getMessage(), e); 233 } catch(SocketTimeoutException e) { 234 System.out.println(" timed out, retries left: " + retries); 235 if (cancel) 236 return; // assume cancel 237 if (retries-- > 0) 238 sendRequestRetry(requestMethod, urlSuffix, osm, addBody, retries); 239 else 240 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 196 241 } catch (Exception e) { 197 242 if (cancel) … … 199 244 if (e instanceof RuntimeException) 200 245 throw (RuntimeException)e; 201 throw new RuntimeException(e.getMessage(), e); 202 } 246 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 247 } 248 } 249 private void sendRequest(String requestMethod, String urlSuffix, 250 OsmPrimitive osm, boolean addBody) { 251 sendRequestRetry(requestMethod, urlSuffix, osm, addBody, 10); 203 252 } 204 253 }
Note:
See TracChangeset
for help on using the changeset viewer.