Skip to content

Commit

Permalink
Made the connection process more resume- and retry-friendly.
Browse files Browse the repository at this point in the history
  • Loading branch information
jchambers committed Sep 28, 2013
1 parent 34c714f commit ea71c80
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions src/main/java/com/relayrides/pushy/apns/ApnsClientThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ private enum ClientState {
private volatile boolean shutdownNotificationWritten;
private volatile boolean notificationRejectedAfterShutdownRequest;

private ChannelFuture connectFuture;
private Future<Channel> handshakeFuture;

private final Object shutdownMutex = new Object();
private SendableApnsPushNotification<KnownBadPushNotification> shutdownNotification;
private ChannelFuture shutdownWriteFuture;
Expand Down Expand Up @@ -228,8 +231,7 @@ public void run() {
boolean finishedConnecting = false;

try {
this.connect();
finishedConnecting = true;
finishedConnecting = this.connect();
} catch (InterruptedException e) {
continue;
}
Expand Down Expand Up @@ -401,35 +403,56 @@ public void run() {
}
}

private void connect() throws InterruptedException {
log.debug(String.format("%s beginning connection process.", this.getName()));
private boolean connect() throws InterruptedException {
if (this.connectFuture == null) {
log.debug(String.format("%s beginning connection process.", this.getName()));
this.connectFuture = this.bootstrap.connect(
this.pushManager.getEnvironment().getApnsGatewayHost(),
this.pushManager.getEnvironment().getApnsGatewayPort());
}

final ChannelFuture connectFuture =
this.bootstrap.connect(
this.pushManager.getEnvironment().getApnsGatewayHost(),
this.pushManager.getEnvironment().getApnsGatewayPort()).await();
this.connectFuture.await();

if (connectFuture.isSuccess()) {
if (this.connectFuture.isSuccess()) {
log.debug(String.format("%s connected.", this.getName()));

this.channel = connectFuture.channel();
this.channel = this.connectFuture.channel();

if (this.pushManager.getEnvironment().isTlsRequired()) {
log.debug(String.format("%s waiting for TLS handshake.", this.getName()));
if (this.handshakeFuture == null) {
log.debug(String.format("%s waiting for TLS handshake.", this.getName()));
this.handshakeFuture = this.channel.pipeline().get(SslHandler.class).handshakeFuture();
}

final Future<Channel> handshakeFuture = this.channel.pipeline().get(SslHandler.class).handshakeFuture().await();
this.handshakeFuture.await();

if (handshakeFuture.isSuccess()) {
if (this.handshakeFuture.isSuccess()) {
log.debug(String.format("%s successfully completed TLS handshake.", this.getName()));

this.connectFuture = null;
this.handshakeFuture = null;

return true;
} else {
log.error(String.format("%s failed to complete TLS handshake with APNs gateway.", this.getName()),
handshakeFuture.cause());
this.handshakeFuture.cause());

this.connectFuture = null;
this.handshakeFuture = null;

return false;
}
} else {
log.debug(String.format("%s does not require a TLS handshake.", this.getName()));

this.connectFuture = null;
return true;
}
} else {
log.error(String.format("%s failed to connect to APNs gateway.", this.getName()), connectFuture.cause());

this.connectFuture = null;
return false;
}
}

Expand Down

0 comments on commit ea71c80

Please sign in to comment.