@@ -56,11 +56,11 @@ public class GPSdEndpoint {
5656
5757 private static final Logger LOG = LoggerFactory .getLogger (GPSdEndpoint .class );
5858
59- private final Socket socket ;
59+ private Socket socket ;
6060
61- private final BufferedReader in ;
61+ private BufferedReader in ;
6262
63- private final BufferedWriter out ;
63+ private BufferedWriter out ;
6464
6565 private SocketThread listenThread ;
6666
@@ -73,6 +73,12 @@ public class GPSdEndpoint {
7373 private final Object asyncWaitMutex = new Object ();
7474
7575 private final AbstractResultParser resultParser ;
76+
77+ private String server ;
78+
79+ private int port ;
80+
81+ private JSONObject watch ;
7682
7783
7884 /**
@@ -87,6 +93,8 @@ public class GPSdEndpoint {
8793 * @throws IOException
8894 */
8995 public GPSdEndpoint (final String server , final int port , final AbstractResultParser resultParser ) throws UnknownHostException , IOException {
96+ this .server = server ;
97+ this .port = port ;
9098 if (server == null ) {
9199 throw new IllegalArgumentException ("server can not be null!" );
92100 }
@@ -121,6 +129,7 @@ public void start() {
121129 * Stops the endpoint.
122130 */
123131 public void stop () {
132+
124133 try {
125134 this .socket .close ();
126135 } catch (final IOException e1 ) {
@@ -169,7 +178,7 @@ public WatchObject watch(final boolean enable, final boolean dumpData) throws IO
169178 * @throws JSONException
170179 */
171180 public WatchObject watch (final boolean enable , final boolean dumpData , final String device ) throws IOException , JSONException {
172- final JSONObject watch = new JSONObject ();
181+ watch = new JSONObject ();
173182 watch .put ("class" , "WATCH" );
174183 watch .put ("enable" , enable );
175184 watch .put ("json" , dumpData );
@@ -320,4 +329,23 @@ public void kickDevice(String path) throws IOException, JSONException {
320329 d .put ("path" , path );
321330 this .voidCommand ("?DEVICE=" + d );
322331 }
332+
333+ /**
334+ * Our socket thread got disconnect and is exiting.
335+ */
336+ void handleDisconnected () throws IOException {
337+ synchronized (this .asyncMutex ) {
338+ socket .close ();
339+ this .socket = new Socket (server , port );
340+ this .in = new BufferedReader (new InputStreamReader (this .socket .getInputStream ()));
341+ this .out = new BufferedWriter (new OutputStreamWriter (this .socket .getOutputStream ()));
342+
343+ this .listenThread = new SocketThread (this .in , this , this .resultParser );
344+ this .listenThread .start ();
345+ if ( watch !=null ){
346+ this .syncCommand ("?WATCH=" + watch .toString (), WatchObject .class );
347+ }
348+ }
349+
350+ }
323351}
0 commit comments