On Wed, 2008-01-30 at 09:00 -0800, Cristiano Giovando wrote:
> I confirm what Rob said, it actually resumed from where it left. But
> it's also true it took many tries before I was able to upload the
> entire dataset. I suspect this problem is more likely with large
> datasets, and it may just be something could be tweaked in JOSM to
> extend the connection timeout limit.
Attached is a patch to JOSM to do server retries as well as do some
crude prediction of how long an upload might take. I've been using it
for months and it works quite well for me.
I _think_ gabriel just applied it to JOSM's svn. So you might want to
try the latest JOSM and see if it is there.
-- Dave
--- src/org/openstreetmap/josm/io/OsmServerWriter.java (revision 528)
+++ src/org/openstreetmap/josm/io/OsmServerWriter.java (working copy)
@@ -12,6 +12,7 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
+import java.net.SocketTimeoutException;
import java.util.Collection;
import java.util.LinkedList;
@@ -56,6 +57,27 @@
* Send the dataset to the server. Ask the user first and does nothing if he
* does not want to send the data.
*/
+ private static final int MSECS_PER_SECOND = 1000;
+ private static final int SECONDS_PER_MINUTE = 60;
+ private static final int MSECS_PER_MINUTE = MSECS_PER_SECOND * SECONDS_PER_MINUTE;
+
+ long uploadStartTime;
+ public String timeLeft(int progress, int list_size) {
+ long now = System.currentTimeMillis();
+ long elapsed = now - uploadStartTime;
+ if (elapsed == 0)
+ elapsed = 1;
+ float uploads_per_ms = (float)progress / elapsed;
+ float uploads_left = list_size - progress;
+ int ms_left = (int)(uploads_left / uploads_per_ms);
+ int minutes_left = ms_left / MSECS_PER_MINUTE;
+ int seconds_left = (ms_left / MSECS_PER_SECOND) % SECONDS_PER_MINUTE ;
+ String time_left_str = Integer.toString(minutes_left) + ":";
+ if (seconds_left < 10)
+ time_left_str += "0";
+ time_left_str += Integer.toString(seconds_left);
+ return time_left_str;
+ }
public void uploadOsm(Collection<OsmPrimitive> list) throws SAXException {
processed = new LinkedList<OsmPrimitive>();
initAuthentication();
@@ -65,13 +87,18 @@
NameVisitor v = new NameVisitor();
try {
+ uploadStartTime = System.currentTimeMillis();
for (OsmPrimitive osm : list) {
if (cancel)
return;
osm.visit(v);
- Main.pleaseWaitDlg.currentAction.setText(tr("Upload {0} {1} ({2})...", tr(v.className), v.name, osm.id));
+ int progress = Main.pleaseWaitDlg.progress.getValue();
+ String time_left_str = timeLeft(progress, list.size());
+ Main.pleaseWaitDlg.currentAction.setText(tr("Upload {0} {1} (id: {2}) {3}% {4}/{5} ({6} left)...",
+ tr(v.className), v.name, osm.id, 100.0*progress/list.size(), progress, list.size(), time_left_str));
osm.visit(this);
Main.pleaseWaitDlg.progress.setValue(Main.pleaseWaitDlg.progress.getValue()+1);
+ Main.pleaseWaitDlg.progress.setValue(progress+1);
}
} catch (RuntimeException e) {
e.printStackTrace();
@@ -149,23 +176,25 @@
* @param addBody <code>true</code>, if the whole primitive body should be added.
* <code>false</code>, if only the id is encoded.
*/
- private void sendRequest(String requestMethod, String urlSuffix,
- OsmPrimitive osm, boolean addBody) {
+ private void sendRequestRetry(String requestMethod, String urlSuffix,
+ OsmPrimitive osm, boolean addBody, int retries) {
try {
+ if (cancel)
+ return; // assume cancel
String version = Main.pref.get("osm-server.version", "0.5");
URL url = new URL(
Main.pref.get("osm-server.url") +
"/" + version +
"/" + urlSuffix +
"/" + (osm.id==0 ? "create" : osm.id));
- System.out.println("upload to: "+url);
+ System.out.print("upload to: "+url+ "..." );
activeConnection = (HttpURLConnection)url.openConnection();
activeConnection.setConnectTimeout(15000);
activeConnection.setRequestMethod(requestMethod);
if (addBody)
activeConnection.setDoOutput(true);
activeConnection.connect();
-
+ System.out.println("connected");
if (addBody) {
OutputStream out = activeConnection.getOutputStream();
OsmWriter.output(out, new OsmWriter.Single(osm, true));
@@ -180,25 +209,45 @@
activeConnection.disconnect();
if (retCode == 410 && requestMethod.equals("DELETE"))
return; // everything fine.. was already deleted.
- if (retCode != 200) {
- // Look for a detailed error message from the server
- if (activeConnection.getHeaderField("Error") != null)
- retMsg += "\n" + activeConnection.getHeaderField("Error");
+ if (retCode != 200 && retCode != 412) {
+ if (retries >= 0) {
+ retries--;
+ System.out.print("backing off for 10 seconds...");
+ Thread.sleep(10000);
+ System.out.println("retrying ("+retries+" left)");
+ sendRequestRetry(requestMethod, urlSuffix, osm, addBody, retries);
+ } else {
+ // Look for a detailed error message from the server
+ if (activeConnection.getHeaderField("Error") != null)
+ retMsg += "\n" + activeConnection.getHeaderField("Error");
- // Report our error
- ByteArrayOutputStream o = new ByteArrayOutputStream();
- OsmWriter.output(o, new OsmWriter.Single(osm, true));
- System.out.println(new String(o.toByteArray(), "UTF-8").toString());
- throw new RuntimeException(retCode+" "+retMsg);
+ // Report our error
+ ByteArrayOutputStream o = new ByteArrayOutputStream();
+ OsmWriter.output(o, new OsmWriter.Single(osm, true));
+ System.out.println(new String(o.toByteArray(), "UTF-8").toString());
+ throw new RuntimeException(retCode+" "+retMsg);
+ }
}
} catch (UnknownHostException e) {
throw new RuntimeException(tr("Unknown host")+": "+e.getMessage(), e);
+ } catch(SocketTimeoutException e) {
+ System.out.println(" timed out, retries left: " + retries);
+ if (cancel)
+ return; // assume cancel
+ if (retries-- > 0)
+ sendRequestRetry(requestMethod, urlSuffix, osm, addBody, retries);
+ else
+ throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e);
} catch (Exception e) {
if (cancel)
return; // assume cancel
if (e instanceof RuntimeException)
throw (RuntimeException)e;
- throw new RuntimeException(e.getMessage(), e);
+ throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e);
}
}
+ private void sendRequest(String requestMethod, String urlSuffix,
+ OsmPrimitive osm, boolean addBody) {
+ sendRequestRetry(requestMethod, urlSuffix, osm, addBody, 10);
+ }
}
_______________________________________________
josm-dev mailing list
[EMAIL PROTECTED]
http://lists.openstreetmap.org/cgi-bin/mailman/listinfo/josm-dev