Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bc17add

Browse files
author
Tim Williscroft
committed
Make the reconnect retry interval configurable.
1 parent b719dac commit bc17add

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/main/java/de/taimos/gpsd4java/backend/GPSdEndpoint.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.net.UnknownHostException;
3030
import java.util.ArrayList;
3131
import java.util.List;
32+
import java.util.concurrent.atomic.AtomicLong;
3233

3334
import org.json.JSONException;
3435
import org.json.JSONObject;
@@ -78,8 +79,9 @@ public class GPSdEndpoint {
7879

7980
private int port;
8081

81-
private JSONObject watch;
82+
private String lastWatch;
8283

84+
private AtomicLong retryInterval = new AtomicLong(1000);
8385

8486
/**
8587
* Instantiate this class to connect to a GPSd server
@@ -178,7 +180,7 @@ public WatchObject watch(final boolean enable, final boolean dumpData) throws IO
178180
* @throws JSONException
179181
*/
180182
public WatchObject watch(final boolean enable, final boolean dumpData, final String device) throws IOException, JSONException {
181-
watch = new JSONObject();
183+
JSONObject watch = new JSONObject();
182184
watch.put("class", "WATCH");
183185
watch.put("enable", enable);
184186
watch.put("json", dumpData);
@@ -237,7 +239,9 @@ private <T extends IGPSObject> T syncCommand(final String command, final Class<T
237239
synchronized (this.asyncMutex) {
238240
this.out.write(command + "\n");
239241
this.out.flush();
240-
242+
if( responseClass == WatchObject.class ){
243+
lastWatch=command;
244+
}
241245
while (true) {
242246
// wait for awaited message
243247
final IGPSObject result = this.waitForResult();
@@ -342,10 +346,20 @@ void handleDisconnected() throws IOException{
342346

343347
this.listenThread = new SocketThread(this.in, this, this.resultParser);
344348
this.listenThread.start();
345-
if( watch!=null){
346-
this.syncCommand("?WATCH=" + watch.toString(), WatchObject.class);
349+
if( lastWatch!=null){ // restore watch if we had one.
350+
this.syncCommand("?WATCH=" + lastWatch, WatchObject.class);
347351
}
348352
}
349353

350354
}
355+
/**
356+
* Set a retry interval for reconnecting to GPSD if the socket closes.
357+
* Default value is 1000ms.
358+
*
359+
* @param millis how long to wait between each reconnection attempts.
360+
*/
361+
public void setRetryInterval(long millis){
362+
retryInterval.set(millis);
363+
}
364+
351365
}

0 commit comments

Comments
 (0)