20
20
import java .io .IOException ;
21
21
import java .io .InputStreamReader ;
22
22
import java .io .PrintStream ;
23
+ import java .net .InetSocketAddress ;
23
24
import java .net .ServerSocket ;
24
25
import java .net .Socket ;
25
26
import java .net .SocketException ;
27
+ import java .util .Date ;
26
28
import java .util .HashMap ;
27
29
import java .util .Map ;
28
30
import java .util .regex .Matcher ;
29
31
import java .util .regex .Pattern ;
30
32
31
33
public class PlexSubscription {
32
- public static final String ACTION_SUBSCRIBE = "com.atomjack.vcfp.action_subscribe" ;
33
- public static final String ACTION_SUBSCRIBED = "com.atomjack.vcfp.action_subscribed" ;
34
- public static final String ACTION_UNSUBSCRIBE = "com.atomjack.vcfp.action_unsubscribe" ;
35
-
36
- // Boolean indicating whether or not to notify an activity of an action done
37
- public static final String EXTRA_NOTIFY = "com.atomjack.vcfp.extra_unsubscribe_notify" ;
38
-
39
- public static final String ACTION_UNSUBSCRIBED = "com.atomjack.vcfp.action_unsubscribed" ;
40
- public static final String ACTION_BROADCAST = "com.atomjack.vcfp.action_broadcast" ;
41
- public static final String ACTION_MESSAGE = "com.atomjack.vcfp.action_message" ;
42
-
43
- public static final String EXTRA_CLASS = "com.atomjack.vcfp.extra_class" ;
44
- public static final String EXTRA_CLIENT = "com.atomjack.vcfp.extra_client" ;
45
- public static final String EXTRA_TIMELINES = "com.atomjack.vcfp.extra_timelines" ;
46
-
47
34
private static final int SUBSCRIBE_INTERVAL = 30000 ; // Send subscribe message every 30 seconds to keep us alive
48
35
49
36
private static Serializer serial = new Persister ();
50
37
51
- public PlexClient mClient ; // the mClient we are subscribing to
52
-
53
- private String uuid ;
54
- private String appName ;
38
+ public PlexClient mClient ; // the client we are subscribing to
55
39
56
40
private VCFPActivity listener ;
57
41
private VCFPActivity notificationListener ; // This will be the listener but will not be reset, and will be used for changing the notification
@@ -71,17 +55,17 @@ public class PlexSubscription {
71
55
private PlayerState currentState = PlayerState .STOPPED ;
72
56
private Timeline currentTimeline ;
73
57
58
+ public Date timeLastHeardFromClient ;
59
+
74
60
public PlexSubscription () {
75
61
mHandler = new Handler ();
76
- uuid = VoiceControlForPlexApplication .getInstance ().prefs .getUUID ();
77
62
}
78
63
79
64
public void setListener (VCFPActivity _listener ) {
80
65
if (_listener != null )
81
66
Logger .d ("Setting listener to %s" , _listener .getClass ().getSimpleName ());
82
67
listener = _listener ;
83
68
if (_listener != null ) {
84
- appName = _listener .getString (R .string .app_name );
85
69
notificationListener = _listener ;
86
70
}
87
71
}
@@ -125,12 +109,19 @@ public void run() {
125
109
Logger .d ("starting serverthread" );
126
110
Socket socket = null ;
127
111
try {
128
- serverSocket = new ServerSocket (subscriptionPort );
112
+ serverSocket = new ServerSocket ();
113
+ serverSocket .setReuseAddress (true );
114
+ serverSocket .bind (new InetSocketAddress (subscriptionPort ));
115
+ // subscriptionPort = serverSocket.getLocalPort();
129
116
} catch (IOException e ) {
117
+
130
118
e .printStackTrace ();
131
119
}
132
120
133
121
Logger .d ("running" );
122
+
123
+
124
+
134
125
onSocketReady .run ();
135
126
if (onReady != null ) {
136
127
// onReady.run();
@@ -258,27 +249,45 @@ public void subscribe(PlexClient client, final boolean isHeartbeat) {
258
249
if (client == null )
259
250
return ;
260
251
mClient = client ;
261
- PlexHttpClient .subscribe (client , subscriptionPort , commandId , uuid , appName , new PlexHttpResponseHandler () {
252
+
253
+ PlexHttpClient .subscribe (client , subscriptionPort , commandId , VoiceControlForPlexApplication .getInstance ().getUUID (), VoiceControlForPlexApplication .getInstance ().getString (R .string .app_name ), new PlexHttpResponseHandler () {
262
254
@ Override
263
255
public void onSuccess (PlexResponse response ) {
264
256
failedHeartbeats = 0 ;
265
- Logger .d ("PlexSubscription: Subscribed: %s" , response .status );
266
- commandId ++;
267
- subscribed = true ;
257
+ if (!isHeartbeat )
258
+ Logger .d ("PlexSubscription: Subscribed: %s" , response != null ? response .status : "" );
259
+ else
260
+ Logger .d ("PlexSubscription: Heartbeat: %s" , response != null ? response .status : "" );
261
+
262
+
263
+
264
+ if (response .code != 200 ) {
265
+ this .onFailure (new Throwable (response .status ));
266
+ // Close the server socket so it's no longer listening on the subscriptionPort
267
+ try {
268
+ serverSocket .close ();
269
+ serverSocket = null ;
270
+ } catch (Exception e ) {}
268
271
269
- if (!isHeartbeat ) {
270
- // Start the heartbeat subscription (so the server knows we're still here)
271
- mHandler .removeCallbacks (subscriptionHeartbeat );
272
- mHandler .postDelayed (subscriptionHeartbeat , SUBSCRIBE_INTERVAL );
273
- onSubscribed ();
272
+
273
+ } else {
274
+ timeLastHeardFromClient = new Date ();
275
+
276
+ commandId ++;
277
+ subscribed = true ;
278
+
279
+ if (!isHeartbeat ) {
280
+ // Start the heartbeat subscription (so the plex client knows we're still here)
281
+ mHandler .removeCallbacks (subscriptionHeartbeat );
282
+ mHandler .postDelayed (subscriptionHeartbeat , SUBSCRIBE_INTERVAL );
283
+ onSubscribed ();
284
+ }
274
285
}
275
286
}
276
287
277
288
@ Override
278
289
public void onFailure (final Throwable error ) {
279
290
error .printStackTrace ();
280
-
281
-
282
291
if (isHeartbeat ) {
283
292
failedHeartbeats ++;
284
293
Logger .d ("%d failed heartbeats" , failedHeartbeats );
@@ -352,7 +361,7 @@ public void unsubscribe(final boolean notify, final Runnable onFinish) {
352
361
if (listener == null )
353
362
return ;
354
363
355
- PlexHttpClient .unsubscribe (mClient , commandId , uuid , listener .getString (R .string .app_name ), new PlexHttpResponseHandler () {
364
+ PlexHttpClient .unsubscribe (mClient , commandId , VoiceControlForPlexApplication . getInstance (). prefs . getUUID () , listener .getString (R .string .app_name ), new PlexHttpResponseHandler () {
356
365
@ Override
357
366
public void onSuccess (PlexResponse response ) {
358
367
Logger .d ("Unsubscribed" );
@@ -364,7 +373,8 @@ public void onSuccess(PlexResponse response) {
364
373
serverSocket .close ();
365
374
serverSocket = null ;
366
375
} catch (Exception ex ) {
367
- // ex.printStackTrace();
376
+ Logger .d ("Exception attempting to close socket." );
377
+ ex .printStackTrace ();
368
378
}
369
379
if (notify )
370
380
onUnsubscribed ();
@@ -378,6 +388,15 @@ public void onFailure(Throwable error) {
378
388
Logger .d ("failure unsubscribing" );
379
389
subscribed = false ;
380
390
mHandler .removeCallbacks (subscriptionHeartbeat );
391
+
392
+ try {
393
+ serverSocket .close ();
394
+ serverSocket = null ;
395
+ } catch (Exception ex ) {
396
+ Logger .d ("Exception attempting to close socket due to failed unsubscribe." );
397
+ ex .printStackTrace ();
398
+ }
399
+
381
400
onUnsubscribed ();
382
401
}
383
402
});
0 commit comments