Changeset 18134 in josm


Ignore:
Timestamp:
2021-08-20T13:53:36+02:00 (3 years ago)
Author:
Don-vip
Message:

fix #21231 - make sure remote control request handlers do not block thread with time-consuming operations

Location:
trunk/src/org/openstreetmap/josm/io/remotecontrol/handler
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddNodeHandler.java

    r14221 r18134  
    66import java.awt.Point;
    77import java.util.Collections;
    8 import java.util.Map;
    98
    109import org.openstreetmap.josm.actions.AutoScaleAction;
     
    3938    @Override
    4039    protected void handleRequest() {
    41         GuiHelper.runInEDTAndWait(() -> addNode(args));
     40        GuiHelper.runInEDT(this::addNode);
    4241    }
    4342
     
    7877    /**
    7978     * Adds a node, implements the GET /add_node?lon=...&lat=... request.
    80      * @param args request arguments
    8179     */
    82     private void addNode(Map<String, String> args) {
     80    private void addNode() {
    8381
    8482        // Parse the arguments
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/AddWayHandler.java

    r14273 r18134  
    4444    private final List<LatLon> allCoordinates = new ArrayList<>();
    4545
    46     private Way way;
    47 
    4846    /**
    4947     * The place to remember already added nodes (they are reused if needed @since 5845
     
    7876    @Override
    7977    protected void handleRequest() throws RequestHandlerErrorException, RequestHandlerBadRequestException {
    80         GuiHelper.runInEDTAndWait(() -> way = addWay());
    81         // parse parameter addtags=tag1=value1|tag2=value2
    82         AddTagsDialog.addTags(args, sender, Collections.singleton(way));
     78        GuiHelper.runInEDT(this::addWay);
    8379    }
    8480
     
    158154    }
    159155
    160     /*
     156    /**
    161157     * This function creates the way with given coordinates of nodes
    162158     */
    163     private Way addWay() {
     159    private void addWay() {
    164160        addedNodes = new HashMap<>();
    165161        Way way = new Way();
     
    179175            MainApplication.getMap().mapView.repaint();
    180176        }
    181         return way;
     177        // parse parameter addtags=tag1=value1|tag2=value2
     178        AddTagsDialog.addTags(args, sender, Collections.singleton(way));
    182179    }
    183180}
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/LoadAndZoomHandler.java

    r17330 r18134  
    3434import org.openstreetmap.josm.data.osm.search.SearchParseError;
    3535import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
     36import org.openstreetmap.josm.gui.ExceptionDialogUtil;
    3637import org.openstreetmap.josm.gui.MainApplication;
    3738import org.openstreetmap.josm.gui.MapFrame;
     
    3940import org.openstreetmap.josm.gui.util.GuiHelper;
    4041import org.openstreetmap.josm.io.OsmApiException;
     42import org.openstreetmap.josm.io.OsmTransferException;
    4143import org.openstreetmap.josm.io.remotecontrol.AddTagsDialog;
    4244import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
     
    155157                        Logging.info("RemoteControl: no download necessary");
    156158                    } else {
    157                         Future<?> future = osmTask.download(settings, new Bounds(minlat, minlon, maxlat, maxlon),
    158                                 null /* let the task manage the progress monitor */);
    159                         MainApplication.worker.submit(new PostDownloadHandler(osmTask, future))
    160                             .get(OSM_DOWNLOAD_TIMEOUT.get(), TimeUnit.SECONDS);
    161                         if (osmTask.isFailed()) {
    162                             Object error = osmTask.getErrorObjects().get(0);
    163                             throw error instanceof OsmApiException
    164                                 ? new RequestHandlerOsmApiException((OsmApiException) error)
    165                                 : new RequestHandlerErrorException(String.join(", ", osmTask.getErrorMessages()));
    166                         }
     159                        Future<?> future = MainApplication.worker.submit(
     160                                new PostDownloadHandler(osmTask, osmTask.download(settings, new Bounds(minlat, minlon, maxlat, maxlon),
     161                                        null /* let the task manage the progress monitor */)));
     162                        GuiHelper.executeByMainWorkerInEDT(() -> {
     163                            try {
     164                                future.get(OSM_DOWNLOAD_TIMEOUT.get(), TimeUnit.SECONDS);
     165                                if (osmTask.isFailed()) {
     166                                    Object error = osmTask.getErrorObjects().get(0);
     167                                    throw error instanceof OsmApiException
     168                                        ? (OsmApiException) error
     169                                        : new OsmTransferException(String.join(", ", osmTask.getErrorMessages()));
     170                                }
     171                            } catch (InterruptedException | ExecutionException | TimeoutException |
     172                                    OsmTransferException | RuntimeException ex) { // NOPMD
     173                                ExceptionDialogUtil.explainException(ex);
     174                            }
     175                        });
    167176                    }
    168177                }
    169178            }
    170         } catch (RuntimeException | InterruptedException | ExecutionException | TimeoutException ex) { // NOPMD
     179        } catch (RuntimeException ex) { // NOPMD
    171180            Logging.warn("RemoteControl: Error parsing load_and_zoom remote control request:");
    172181            Logging.error(ex);
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/OpenFileHandler.java

    r17534 r18134  
    4949            options.add(Options.ALLOW_WEB_RESOURCES);
    5050        }
    51         GuiHelper.runInEDTAndWait(() ->
     51        GuiHelper.runInEDT(() ->
    5252            OpenFileAction.openFiles(Arrays.asList(new File(args.get("filename"))), options.toArray(new Options[0])));
    5353    }
  • trunk/src/org/openstreetmap/josm/io/remotecontrol/handler/RequestHandler.java

    r17330 r18134  
    9797    /**
    9898     * Handle a specific command sent as remote control.
     99     * Any time-consuming operation must be performed asynchronously to avoid delaying the HTTP response.
    99100     *
    100101     * This method of the subclass will do the real work.
Note: See TracChangeset for help on using the changeset viewer.