Skip to content

Commit 47ea77b

Browse files
authored
Merge pull request ari4java#53 from grahambrown11/master
Auto reconnect and websocket ping - Release 0.4.1
2 parents 7a9487e + 2c380d7 commit 47ea77b

8 files changed

Lines changed: 239 additions & 109 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def env = System.getenv()
99

1010
project.ext {
1111
webapp_name = 'ari4java'
12-
app_version = '0.4.0'
12+
app_version = '0.5.0'
1313
build_number = env["BUILD_NUMBER"]
1414
version_class = 'ch/loway/oss/ari4java/BUILD.java'
1515
build_time = "" + new Date()

classes/ch/loway/oss/ari4java/ARI.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ public String getUrl() {
9999
@SuppressWarnings("unchecked")
100100
public <T> T getActionImpl(Class<T> klazz) throws ARIException {
101101

102+
// use the events method as we ref it for cleanup
103+
if (klazz == ActionEvents.class) {
104+
return (T) events();
105+
}
106+
102107
BaseAriAction action = (BaseAriAction) buildConcreteImplementation(klazz);
103108
action.setHttpClient(this.httpClient);
104109
action.setWsClient(this.wsClient);
@@ -363,11 +368,18 @@ private static String findVersionString(String response) throws ARIException {
363368
public void cleanup() throws ARIException {
364369

365370
if ( liveActionEvent != null ) {
366-
closeAction( liveActionEvent );
371+
try {
372+
closeAction(liveActionEvent);
373+
} catch (ARIException e) {
374+
// ignore on cleanup...
375+
}
376+
liveActionEvent = null;
367377
}
368378

369379
destroy( wsClient );
370-
destroy( httpClient );
380+
if (wsClient != httpClient) {
381+
destroy(httpClient);
382+
}
371383

372384
wsClient = null;
373385
httpClient = null;
@@ -410,8 +422,7 @@ public MessageQueue getWebsocketQueue() throws ARIException {
410422

411423
final MessageQueue q = new MessageQueue();
412424

413-
ActionEvents ae = events();
414-
ae.eventWebsocket( appName, new AriCallback<Message>() {
425+
events().eventWebsocket( appName, new AriCallback<Message>() {
415426

416427
@Override
417428
public void onSuccess(Message result) {
@@ -424,8 +435,6 @@ public void onFailure(RestException e) {
424435
}
425436
});
426437

427-
// register the AE so we can disconnectWs it when the erorr goes down
428-
liveActionEvent = ae;
429438
return q;
430439

431440
}
@@ -492,7 +501,9 @@ public ActionEndpoints endpoints() {
492501
* @return an Events object.
493502
*/
494503
public ActionEvents events() {
495-
return (ActionEvents) setupAction(version.builder().actionEvents());
504+
if (liveActionEvent == null)
505+
liveActionEvent = (ActionEvents) setupAction(version.builder().actionEvents());
506+
return liveActionEvent;
496507
}
497508

498509
/**

classes/ch/loway/oss/ari4java/tools/AriAsyncHandler.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class AriAsyncHandler<T> implements HttpResponseHandler {
1212
private final AriCallback<? super T> callback;
1313
private Class<T> klazz;
1414
private TypeReference<T> klazzType;
15+
private long lastResponseTime = 0;
1516

1617
public AriAsyncHandler(AriCallback<? super T> callback, Class<T> klazz) {
1718
this.callback = callback;
@@ -45,12 +46,12 @@ void handleResponse(String json) {
4546

4647
@Override
4748
public void onChReadyToWrite() {
48-
// Client connected. That's good.
49+
lastResponseTime = System.currentTimeMillis();
4950
}
5051

5152
@Override
5253
public void onResponseReceived() {
53-
// not sure what should go here...
54+
lastResponseTime = System.currentTimeMillis();
5455
}
5556

5657
@Override
@@ -69,6 +70,11 @@ public void onFailure(Throwable e) {
6970
this.callback.onFailure(new RestException(e));
7071
}
7172

73+
@Override
74+
public long getLastResponseTime() {
75+
return lastResponseTime;
76+
}
77+
7278
}
7379

7480
// $Log$

classes/ch/loway/oss/ari4java/tools/BaseAriAction.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,6 @@ public static Message deserializeEvent(String json, Class<?> klazz) throws RestE
203203
* @throws RestException
204204
*/
205205
public synchronized void disconnectWs() throws RestException {
206-
System.out.println("Closing WS connection");
207-
if (wsUpgrade && wsConnection == null) {
208-
throw new RestException("No WebSocket connection is open");
209-
}
210206
wsConnection.disconnect();
211207
wsConnection = null;
212208
}

classes/ch/loway/oss/ari4java/tools/HttpResponseHandler.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,11 @@ public interface HttpResponseHandler {
3535
* @param e
3636
*/
3737
void onFailure(Throwable e);
38+
39+
/**
40+
* The last time in epoch since last response
41+
*
42+
* @return epoch
43+
*/
44+
long getLastResponseTime();
3845
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package ch.loway.oss.ari4java.tools;
2+
3+
/**
4+
* Interface to pluggable WebSocket reconnect implementation
5+
*
6+
* @author grahambrown11
7+
*
8+
*/
9+
public interface WsClientAutoReconnect {
10+
void reconnectWs();
11+
}

0 commit comments

Comments
 (0)