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

Skip to content

Commit 62dc2d8

Browse files
authored
Merge pull request #27 from szaboz89/i25_lazy_socket_connection
Do not open socket in main thread (#25)
2 parents d3af942 + 814659c commit 62dc2d8

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ public GPSdEndpoint(final String server, final int port, final AbstractResultPar
108108
if (resultParser == null) {
109109
throw new IllegalArgumentException("resultParser can not be null!");
110110
}
111-
112-
this.socket = new Socket(server, port);
113-
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
114-
this.out = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
111+
115112
this.resultParser = resultParser;
116113

117114
this.daemon = daemon;
@@ -236,13 +233,16 @@ public void removeListener(final IObjectListener listener) {
236233
*/
237234
private <T extends IGPSObject> T syncCommand(final String command, final Class<T> responseClass) throws IOException {
238235
synchronized (this.asyncMutex) {
239-
this.out.write(command + "\n");
240-
this.out.flush();
236+
if (out != null) {
237+
this.out.write(command + "\n");
238+
this.out.flush();
239+
}
241240
if (responseClass == WatchObject.class) {
242241
lastWatch = command;
243242
}
244243
while (true) {
245244
// wait for awaited message
245+
// FIXME possible infinite loop if expected result arrives but new result overrides expected result before getting to this point.
246246
final IGPSObject result = this.waitForResult();
247247
if ((result == null) || result.getClass().equals(responseClass)) {
248248
return responseClass.cast(result);
@@ -254,7 +254,6 @@ private <T extends IGPSObject> T syncCommand(final String command, final Class<T
254254
/*
255255
* send command without response
256256
*/
257-
@SuppressWarnings("unused")
258257
private void voidCommand(final String command) throws IOException {
259258
synchronized (this.asyncMutex) {
260259
this.out.write(command + "\n");
@@ -338,7 +337,9 @@ public void kickDevice(String path) throws IOException, JSONException {
338337
*/
339338
void handleDisconnected() throws IOException {
340339
synchronized (this.asyncMutex) {
341-
socket.close();
340+
if (socket != null) {
341+
socket.close();
342+
}
342343
this.socket = new Socket(server, port);
343344
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
344345
this.out = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));

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

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,7 @@ public class SocketThread extends Thread {
5252
*/
5353
public SocketThread(final BufferedReader reader, final GPSdEndpoint endpoint,
5454
final AbstractResultParser resultParser, final boolean daemon) {
55-
if (reader == null) {
56-
throw new IllegalArgumentException("reader can not be null!");
57-
}
58-
if (endpoint == null) {
59-
throw new IllegalArgumentException("endpoint can not be null!");
60-
}
55+
6156
if (resultParser == null) {
6257
throw new IllegalArgumentException("resultParser can not be null!");
6358
}
@@ -81,22 +76,24 @@ public SocketThread(final BufferedReader reader, final GPSdEndpoint endpoint, fi
8176

8277
@Override
8378
public void run() {
84-
while (this.running.get()) {
85-
try {
86-
// read line from socket
87-
final String s = this.reader.readLine();
88-
if (s == null) {
89-
break;
90-
}
91-
if (!s.isEmpty()) {
92-
// parse line and handle it accordingly
93-
this.endpoint.handle(this.resultParser.parse(s));
79+
if (this.reader != null) {
80+
while (this.running.get()) {
81+
try {
82+
// read line from socket
83+
final String s = this.reader.readLine();
84+
if (s == null) {
85+
break;
86+
}
87+
if (!s.isEmpty()) {
88+
// parse line and handle it accordingly
89+
this.endpoint.handle(this.resultParser.parse(s));
90+
}
91+
} catch (final SocketException e) {
92+
break; // stop
93+
} catch (final Exception e) {
94+
// TODO handle this better
95+
SocketThread.LOG.warn("Problem encountered while reading/parsing/handling line", e);
9496
}
95-
} catch (final SocketException e) {
96-
break; // stop
97-
} catch (final Exception e) {
98-
// TODO handle this better
99-
SocketThread.LOG.warn("Problem encountered while reading/parsing/handling line", e);
10097
}
10198
}
10299
if (running.get() && !Thread.interrupted()) {
@@ -106,13 +103,17 @@ public void run() {
106103
}
107104

108105
protected void retry() {
109-
SocketThread.LOG.debug("Disconnected from GPS socket, retrying connection");
106+
if (reader != null) {
107+
SocketThread.LOG.debug("Disconnected from GPS socket, retrying connection");
108+
} else {
109+
SocketThread.LOG.debug("Connecting to GPSD socket");
110+
}
110111

111112
while (this.running.get()) {
112113
try {
113114
running.waitFor(this.endpoint.getRetryInterval());
114115
this.endpoint.handleDisconnected();
115-
SocketThread.LOG.debug("Reconnected to GPS socket");
116+
SocketThread.LOG.debug("Connected to GPS socket");
116117
running.set(false);
117118
} catch (InterruptedException ix) {
118119
break;

0 commit comments

Comments
 (0)