Ignore:
Timestamp:
2023-09-20T17:53:20+02:00 (14 months ago)
Author:
taylor.smock
Message:

Fix #23171: UnknownHostException may cause issues in GeoChat

This appears to be something that can occur when running under WebStart.
To fix this, we check if (a) the JOSM website is offline and (b) if we are
running under webstart. If so, we don't check for messages.

Additionally, this patch converts geochat from the older time classes to the
newer java.time classes and fixes some lint issues.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/geochat/src/geochat/ChatServerConnection.java

    r36122 r36146  
    77import java.io.UnsupportedEncodingException;
    88import java.net.URLEncoder;
     9import java.net.UnknownHostException;
     10import java.time.Instant;
    911import java.util.ArrayList;
    10 import java.util.Date;
    1112import java.util.HashMap;
    1213import java.util.HashSet;
     
    1415import java.util.Map;
    1516import java.util.Set;
     17import java.util.concurrent.ScheduledThreadPoolExecutor;
     18import java.util.concurrent.TimeUnit;
    1619
    1720import jakarta.json.JsonArray;
     
    2225import org.openstreetmap.josm.data.coor.LatLon;
    2326import org.openstreetmap.josm.data.coor.conversion.DecimalDegreesCoordinateFormat;
     27import org.openstreetmap.josm.data.preferences.JosmUrls;
    2428import org.openstreetmap.josm.data.projection.Projection;
    2529import org.openstreetmap.josm.data.projection.ProjectionRegistry;
    2630import org.openstreetmap.josm.gui.MainApplication;
    2731import org.openstreetmap.josm.gui.MapView;
     32import org.openstreetmap.josm.io.NetworkManager;
     33import org.openstreetmap.josm.io.OnlineResource;
    2834import org.openstreetmap.josm.spi.preferences.Config;
    2935import org.openstreetmap.josm.tools.Logging;
     36import org.openstreetmap.josm.tools.Utils;
    3037
    3138/**
     
    3744    public static final String TOKEN_PREFIX = "=";
    3845    private static final String TOKEN_PATTERN = "^[a-zA-Z0-9]{10}$";
     46    private static final ScheduledThreadPoolExecutor EXECUTOR = new ScheduledThreadPoolExecutor(1);
    3947
    4048    private int userId;
     
    4250    private static ChatServerConnection instance;
    4351    private final Set<ChatServerConnectionListener> listeners;
    44     private final LogRequest requestThread;
    4552
    4653    private ChatServerConnection() {
     
    4855        userName = null;
    4956        listeners = new HashSet<>();
    50         requestThread = new LogRequest();
    51         new Thread(requestThread).start();
     57        LogRequest requestThread = new LogRequest();
     58        final int interval = Config.getPref().getInt("geochat.interval", 2);
     59        EXECUTOR.scheduleAtFixedRate(requestThread, interval, interval, TimeUnit.SECONDS);
    5260    }
    5361
     
    116124            return;
    117125        }
    118         new Thread(() -> {
     126        // Blocking the geochat executor here isn't a big deal, since we need to be logged in for chat anyway.
     127        EXECUTOR.schedule(() -> {
    119128            try {
    120129                int cnt = 10;
     
    127136            }
    128137            autoLogin(userName);
    129         }).start();
     138        }, 200, TimeUnit.MILLISECONDS);
    130139    }
    131140
     
    315324        private long lastId;
    316325        private boolean lastStatus;
    317         private boolean stopping;
    318326
    319327        @Override
    320328        public void run() {
    321329            //            lastId = Config.getPref().getLong("geochat.lastid", 0);
    322             int interval = Config.getPref().getInt("geochat.interval", 2);
    323             while (!stopping) {
     330            if (!NetworkManager.isOffline(OnlineResource.JOSM_WEBSITE) || !Utils.isRunningWebStart()) {
    324331                process();
    325                 try {
    326                     Thread.sleep(interval * 1000L);
    327                 } catch (InterruptedException e) {
    328                     Thread.currentThread().interrupt();
    329                     stopping = true;
    330                     Logging.trace(e);
    331                 }
    332             }
    333         }
    334 
    335         public void stop() {
    336             stopping = true;
    337         }
    338 
    339         public void process() {
     332            }
     333        }
     334
     335        private void process() {
    340336            if (!isLoggedIn()) {
    341337                fireStatusChanged(false);
     
    371367                Logging.trace(ex);
    372368                json = null; // ?
     369                final Throwable root = Utils.getRootCause(ex);
     370                if (root instanceof UnknownHostException) {
     371                    UnknownHostException uhe = (UnknownHostException) root;
     372                    NetworkManager.addNetworkError(uhe.getMessage(), uhe);
     373                    if (JosmUrls.getInstance().getJOSMWebsite().endsWith(uhe.getMessage())) {
     374                        NetworkManager.setOffline(OnlineResource.JOSM_WEBSITE);
     375                    }
     376                }
    373377            }
    374378            if (json == null) {
     
    424428                    boolean incoming = msg.getBoolean("incoming");
    425429                    ChatMessage cm = new ChatMessage(id, new LatLon(lat, lon), author,
    426                             incoming, message, new Date(timeStamp * 1000));
     430                            incoming, message, Instant.ofEpochSecond(timeStamp));
    427431                    cm.setPrivate(priv);
    428432                    if (msg.get("recipient") != null && !incoming)
Note: See TracChangeset for help on using the changeset viewer.