Changeset 36146 in osm for applications/editors/josm/plugins/geochat/src/geochat/ChatServerConnection.java
- Timestamp:
- 2023-09-20T17:53:20+02:00 (14 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/geochat/src/geochat/ChatServerConnection.java
r36122 r36146 7 7 import java.io.UnsupportedEncodingException; 8 8 import java.net.URLEncoder; 9 import java.net.UnknownHostException; 10 import java.time.Instant; 9 11 import java.util.ArrayList; 10 import java.util.Date;11 12 import java.util.HashMap; 12 13 import java.util.HashSet; … … 14 15 import java.util.Map; 15 16 import java.util.Set; 17 import java.util.concurrent.ScheduledThreadPoolExecutor; 18 import java.util.concurrent.TimeUnit; 16 19 17 20 import jakarta.json.JsonArray; … … 22 25 import org.openstreetmap.josm.data.coor.LatLon; 23 26 import org.openstreetmap.josm.data.coor.conversion.DecimalDegreesCoordinateFormat; 27 import org.openstreetmap.josm.data.preferences.JosmUrls; 24 28 import org.openstreetmap.josm.data.projection.Projection; 25 29 import org.openstreetmap.josm.data.projection.ProjectionRegistry; 26 30 import org.openstreetmap.josm.gui.MainApplication; 27 31 import org.openstreetmap.josm.gui.MapView; 32 import org.openstreetmap.josm.io.NetworkManager; 33 import org.openstreetmap.josm.io.OnlineResource; 28 34 import org.openstreetmap.josm.spi.preferences.Config; 29 35 import org.openstreetmap.josm.tools.Logging; 36 import org.openstreetmap.josm.tools.Utils; 30 37 31 38 /** … … 37 44 public static final String TOKEN_PREFIX = "="; 38 45 private static final String TOKEN_PATTERN = "^[a-zA-Z0-9]{10}$"; 46 private static final ScheduledThreadPoolExecutor EXECUTOR = new ScheduledThreadPoolExecutor(1); 39 47 40 48 private int userId; … … 42 50 private static ChatServerConnection instance; 43 51 private final Set<ChatServerConnectionListener> listeners; 44 private final LogRequest requestThread;45 52 46 53 private ChatServerConnection() { … … 48 55 userName = null; 49 56 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); 52 60 } 53 61 … … 116 124 return; 117 125 } 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(() -> { 119 128 try { 120 129 int cnt = 10; … … 127 136 } 128 137 autoLogin(userName); 129 } ).start();138 }, 200, TimeUnit.MILLISECONDS); 130 139 } 131 140 … … 315 324 private long lastId; 316 325 private boolean lastStatus; 317 private boolean stopping;318 326 319 327 @Override 320 328 public void run() { 321 329 // 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()) { 324 331 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() { 340 336 if (!isLoggedIn()) { 341 337 fireStatusChanged(false); … … 371 367 Logging.trace(ex); 372 368 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 } 373 377 } 374 378 if (json == null) { … … 424 428 boolean incoming = msg.getBoolean("incoming"); 425 429 ChatMessage cm = new ChatMessage(id, new LatLon(lat, lon), author, 426 incoming, message, new Date(timeStamp * 1000));430 incoming, message, Instant.ofEpochSecond(timeStamp)); 427 431 cm.setPrivate(priv); 428 432 if (msg.get("recipient") != null && !incoming)
Note:
See TracChangeset
for help on using the changeset viewer.