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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/main/java/de/taimos/gpsd4java/backend/GPSdEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ public GPSdEndpoint(final String server, final int port, final AbstractResultPar
if (resultParser == null) {
throw new IllegalArgumentException("resultParser can not be null!");
}

this.socket = new Socket(server, port);
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
this.out = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));

this.resultParser = resultParser;

this.daemon = daemon;
Expand Down Expand Up @@ -236,13 +233,16 @@ public void removeListener(final IObjectListener listener) {
*/
private <T extends IGPSObject> T syncCommand(final String command, final Class<T> responseClass) throws IOException {
synchronized (this.asyncMutex) {
this.out.write(command + "\n");
this.out.flush();
if (out != null) {
this.out.write(command + "\n");
this.out.flush();
}
if (responseClass == WatchObject.class) {
lastWatch = command;
}
while (true) {
// wait for awaited message
// FIXME possible infinite loop if expected result arrives but new result overrides expected result before getting to this point.
final IGPSObject result = this.waitForResult();
if ((result == null) || result.getClass().equals(responseClass)) {
return responseClass.cast(result);
Expand All @@ -254,7 +254,6 @@ private <T extends IGPSObject> T syncCommand(final String command, final Class<T
/*
* send command without response
*/
@SuppressWarnings("unused")
private void voidCommand(final String command) throws IOException {
synchronized (this.asyncMutex) {
this.out.write(command + "\n");
Expand Down Expand Up @@ -338,7 +337,9 @@ public void kickDevice(String path) throws IOException, JSONException {
*/
void handleDisconnected() throws IOException {
synchronized (this.asyncMutex) {
socket.close();
if (socket != null) {
socket.close();
}
this.socket = new Socket(server, port);
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
this.out = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
Expand Down
47 changes: 24 additions & 23 deletions src/main/java/de/taimos/gpsd4java/backend/SocketThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ public class SocketThread extends Thread {
*/
public SocketThread(final BufferedReader reader, final GPSdEndpoint endpoint,
final AbstractResultParser resultParser, final boolean daemon) {
if (reader == null) {
throw new IllegalArgumentException("reader can not be null!");
}
if (endpoint == null) {
throw new IllegalArgumentException("endpoint can not be null!");
}

if (resultParser == null) {
throw new IllegalArgumentException("resultParser can not be null!");
}
Expand All @@ -81,22 +76,24 @@ public SocketThread(final BufferedReader reader, final GPSdEndpoint endpoint, fi

@Override
public void run() {
while (this.running.get()) {
try {
// read line from socket
final String s = this.reader.readLine();
if (s == null) {
break;
}
if (!s.isEmpty()) {
// parse line and handle it accordingly
this.endpoint.handle(this.resultParser.parse(s));
if (this.reader != null) {
while (this.running.get()) {
try {
// read line from socket
final String s = this.reader.readLine();
if (s == null) {
break;
}
if (!s.isEmpty()) {
// parse line and handle it accordingly
this.endpoint.handle(this.resultParser.parse(s));
}
} catch (final SocketException e) {
break; // stop
} catch (final Exception e) {
// TODO handle this better
SocketThread.LOG.warn("Problem encountered while reading/parsing/handling line", e);
}
} catch (final SocketException e) {
break; // stop
} catch (final Exception e) {
// TODO handle this better
SocketThread.LOG.warn("Problem encountered while reading/parsing/handling line", e);
}
}
if (running.get() && !Thread.interrupted()) {
Expand All @@ -106,13 +103,17 @@ public void run() {
}

protected void retry() {
SocketThread.LOG.debug("Disconnected from GPS socket, retrying connection");
if (reader != null) {
SocketThread.LOG.debug("Disconnected from GPS socket, retrying connection");
} else {
SocketThread.LOG.debug("Connecting to GPSD socket");
}

while (this.running.get()) {
try {
running.waitFor(this.endpoint.getRetryInterval());
this.endpoint.handleDisconnected();
SocketThread.LOG.debug("Reconnected to GPS socket");
SocketThread.LOG.debug("Connected to GPS socket");
running.set(false);
} catch (InterruptedException ix) {
break;
Expand Down