Ignore:
Timestamp:
2008-01-30T18:02:38+01:00 (16 years ago)
Author:
gebner
Message:

Part one of patch by Dave Hansen <dave@…>

  • Remove unused imports
  • Main.debug
  • Make attribute merging aware of TIGER-import attributes
  • Better upload progress information
  • Retry uploads
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java

    r343 r529  
    1313import java.net.URL;
    1414import java.net.UnknownHostException;
     15import java.net.SocketTimeoutException;
    1516import java.util.Collection;
    1617import java.util.LinkedList;
     
    5758         * does not want to send the data.
    5859         */
     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        }       
    5981        public void uploadOsm(Collection<OsmPrimitive> list) throws SAXException {
    6082                processed = new LinkedList<OsmPrimitive>();
     
    6688                NameVisitor v = new NameVisitor();
    6789                try {
     90                        uploadStartTime = System.currentTimeMillis();
    6891                        for (OsmPrimitive osm : list) {
    6992                                if (cancel)
    7093                                        return;
    7194                                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));
    7399                                osm.visit(this);
    74100                                Main.pleaseWaitDlg.progress.setValue(Main.pleaseWaitDlg.progress.getValue()+1);
     101                                Main.pleaseWaitDlg.progress.setValue(progress+1);
    75102                        }
    76103                } catch (RuntimeException e) {
     
    150177         *              <code>false</code>, if only the id is encoded.
    151178         */
    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) {
    154181                try {
     182                        if (cancel)
     183                                return; // assume cancel
    155184                        String version = Main.pref.get("osm-server.version", "0.5");
    156185                        URL url = new URL(
     
    159188                                        "/" + urlSuffix +
    160189                                        "/" + (osm.id==0 ? "create" : osm.id));
    161                         System.out.println("upload to: "+url);
     190                        System.out.print("upload to: "+url+ "..." );
    162191                        activeConnection = (HttpURLConnection)url.openConnection();
    163192                        activeConnection.setConnectTimeout(15000);
     
    166195                                activeConnection.setDoOutput(true);
    167196                        activeConnection.connect();
    168 
     197                        System.out.println("connected");
    169198                        if (addBody) {
    170199                                OutputStream out = activeConnection.getOutputStream();
     
    181210                        if (retCode == 410 && requestMethod.equals("DELETE"))
    182211                                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                                }
    193230                        }
    194231                } catch (UnknownHostException e) {
    195232                        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);
    196241                } catch (Exception e) {
    197242                        if (cancel)
     
    199244                        if (e instanceof RuntimeException)
    200245                                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);
    203252        }
    204253}
Note: See TracChangeset for help on using the changeset viewer.