Changeset 25791 in osm for applications


Ignore:
Timestamp:
2011-04-04T21:43:54+02:00 (13 years ago)
Author:
glebius
Message:

Clean up connection/disconnection state machine.

Now plugin successfully reconnects if gpsd restarts,
as well as if the layer is restarted.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/livegps/src/livegps/LiveGpsAcquirer.java

    r23191 r25791  
    121121        while (!shutdownFlag) {
    122122
    123             try {
    124                 if (!connected)
     123            while (!connected) {
     124                try {
    125125                    connect();
    126 
    127                 if (connected) {
     126                } catch (IOException iox) {
     127                    fireGpsStatusChangeEvent( LiveGpsStatus.GpsStatus.CONNECTION_FAILED, tr("Connection Failed"));
     128                    try {
     129                        Thread.sleep(1000);
     130                    } catch (InterruptedException ignore) {}
     131                };
     132            }
     133
     134            assert (connected);
     135
     136            try {
    128137                    String line;
    129138
     
    134143                    // </FIXXME>
    135144                    if (line == null)
    136                         break;
     145                        throw new IOException();
    137146
    138147                    if (JSONProtocol == true)
     
    146155                    fireGpsDataChangeEvent(oldGpsData, gpsData);
    147156                    oldGpsData = gpsData;
    148                 } else {
    149                     fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.DISCONNECTED, tr("Not connected"));
    150                     try {
    151                         Thread.sleep(1000);
    152                     } catch (InterruptedException ignore) {}
    153                 }
    154157            } catch (IOException iox) {
    155                 connected = false;
    156                 if (gpsData != null) {
    157                     gpsData.setFix(false);
    158                     fireGpsDataChangeEvent(oldGpsData, gpsData);
    159                 }
     158                System.out.println("LiveGps: lost connection to gpsd");
    160159                fireGpsStatusChangeEvent(
    161160                        LiveGpsStatus.GpsStatus.CONNECTION_FAILED,
    162161                        tr("Connection Failed"));
     162                disconnect();
    163163                try {
    164164                    Thread.sleep(1000);
     
    168168        }
    169169
     170        System.out.println("LiveGps: Disconnected from gpsd");
    170171        fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.DISCONNECTED,
    171172                tr("Not connected"));
    172         if (gpsdSocket != null) {
    173             try {
    174                 gpsdSocket.close();
    175                 gpsdSocket = null;
    176                 System.out.println("LiveGps: Disconnected from gpsd");
    177             } catch (Exception e) {
    178                 System.out.println("LiveGps: Unable to close socket; reconnection may not be possible");
    179             }
    180         }
     173        disconnect();
    181174    }
    182175
     
    197190                gpsdSocket = new Socket(addrs[i], gpsdPort);
    198191                break;
    199             } catch (Exception e) {
     192            } catch (IOException e) {
    200193                System.out.println("LiveGps: Could not open connection to gpsd: " + e);
    201194                gpsdSocket = null;
     
    203196        }
    204197
    205         if (gpsdSocket == null)
    206             return;
    207 
    208         fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.CONNECTING, tr("Connecting"));
     198        if (gpsdSocket == null || gpsdSocket.isConnected() == false)
     199            throw new IOException();
    209200
    210201        /*
     
    248239            fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.CONNECTED, tr("Connected"));
    249240        }
     241    }
     242
     243    private void disconnect() {
     244        assert(gpsdSocket != null);
     245
     246        connected = false;
     247
     248        try {
     249                gpsdSocket.close();
     250                gpsdSocket = null;
     251        } catch (Exception e) {
     252                System.out.println("LiveGps: Unable to close socket; reconnection may not be possible");
     253        }
    250254    }
    251255
Note: See TracChangeset for help on using the changeset viewer.