Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added getDeliveryErrorDevices() to ApnsService #305

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/main/java/com/notnoop/apns/ApnsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.Set;

import com.notnoop.exceptions.NetworkIOException;

Expand Down Expand Up @@ -160,6 +161,16 @@ public interface ApnsService {
*/
Map<String, Date> getInactiveDevices() throws NetworkIOException;

/**
* Returns the list of devices got a bad status when tring to send
*
* The result is map, mapping the device tokens as Hex Strings
* mapped to a set of DeliveryError
* @throws NetworkIOException if a network error occurred
* while retrieving invalid device connection
*/
Map<String, Set<DeliveryError>> getDeliveryErrorDevices();

/**
* Test that the service is setup properly and the Apple servers
* are reachable.
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/notnoop/apns/internal/ApnsConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@
package com.notnoop.apns.internal;

import java.io.Closeable;
import java.util.Map;
import java.util.Set;

import com.notnoop.apns.ApnsNotification;
import com.notnoop.apns.DeliveryError;
import com.notnoop.exceptions.NetworkIOException;

public interface ApnsConnection extends Closeable {
Expand All @@ -49,4 +52,6 @@ public interface ApnsConnection extends Closeable {
void setCacheLength(int cacheLength);

int getCacheLength();

Map<String, Set<DeliveryError>> getDeliveryErrorDevices();
}
27 changes: 23 additions & 4 deletions src/main/java/com/notnoop/apns/internal/ApnsConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
Expand All @@ -61,6 +65,7 @@ public class ApnsConnectionImpl implements ApnsConnection {

private static final Logger logger = LoggerFactory.getLogger(ApnsConnectionImpl.class);

Map<String, Set<DeliveryError>> deliveryErrorDevices = new HashMap<String, Set<DeliveryError>>();
private final SocketFactory factory;
private final String host;
private final int port;
Expand Down Expand Up @@ -132,7 +137,7 @@ public synchronized void close() {
Utilities.close(socket);
}

private void monitorSocket(final Socket socket) {
private void monitorSocket(final ApnsNotification apnsNotification, final Socket socket) {
logger.debug("Launching Monitoring Thread for socket {}", socket);

Thread t = threadFactory.newThread(new Runnable() {
Expand Down Expand Up @@ -164,6 +169,15 @@ public void run() {
int statusCode = bytes[1] & 0xFF;
DeliveryError e = DeliveryError.ofCode(statusCode);

String token = Utilities.encodeHex(apnsNotification.getDeviceToken());
Set<DeliveryError> deliveryErrors = deliveryErrorDevices.get(token);
if (deliveryErrors == null) {
deliveryErrors = new HashSet<DeliveryError>();

deliveryErrorDevices.put(token, deliveryErrors);
}
deliveryErrors.add(e);

int id = Utilities.parseBytes(bytes[2], bytes[3], bytes[4], bytes[5]);

logger.debug("Closed connection cause={}; id={}", e, id);
Expand Down Expand Up @@ -256,7 +270,7 @@ private boolean readPacket(final InputStream in, final byte[] bytes) throws IOEx
t.start();
}

private synchronized Socket getOrCreateSocket(boolean resend) throws NetworkIOException {
private synchronized Socket getOrCreateSocket(ApnsNotification apnsNotification, boolean resend) throws NetworkIOException {
if (reconnectPolicy.shouldReconnect()) {
logger.debug("Reconnecting due to reconnectPolicy dictating it");
Utilities.close(socket);
Expand Down Expand Up @@ -292,7 +306,7 @@ private synchronized Socket getOrCreateSocket(boolean resend) throws NetworkIOEx
socket.setKeepAlive(true);

if (errorDetection) {
monitorSocket(socket);
monitorSocket(apnsNotification, socket);
}

reconnectPolicy.reconnected();
Expand Down Expand Up @@ -325,7 +339,7 @@ private synchronized void sendMessage(ApnsNotification m, boolean fromBuffer) th
while (true) {
try {
attempts++;
Socket socket = getOrCreateSocket(fromBuffer);
Socket socket = getOrCreateSocket(m, fromBuffer);
socket.getOutputStream().write(m.marshall());
socket.getOutputStream().flush();
cacheNotification(m);
Expand Down Expand Up @@ -408,4 +422,9 @@ public void setCacheLength(int cacheLength) {
public int getCacheLength() {
return cacheLength;
}

@Override
public Map<String, Set<DeliveryError>> getDeliveryErrorDevices() {
return deliveryErrorDevices;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
*/
package com.notnoop.apns.internal;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.*;
import com.notnoop.apns.ApnsNotification;
import com.notnoop.apns.DeliveryError;
import com.notnoop.exceptions.NetworkIOException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.slf4j.Logger;
Expand Down Expand Up @@ -118,4 +121,9 @@ public synchronized void setCacheLength(int cacheLength) {
public int getCacheLength() {
return prototypes.peek().getCacheLength();
}

@Override
public Map<String, Set<DeliveryError>> getDeliveryErrorDevices() {
return prototype.getDeliveryErrorDevices();
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/notnoop/apns/internal/ApnsServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
*/
package com.notnoop.apns.internal;

import java.util.Map;
import java.util.Set;

import com.notnoop.apns.ApnsNotification;
import com.notnoop.apns.DeliveryError;
import com.notnoop.exceptions.NetworkIOException;

public class ApnsServiceImpl extends AbstractApnsService {
Expand All @@ -56,4 +60,9 @@ public void stop() {
public void testConnection() {
connection.testConnection();
}

@Override
public Map<String, Set<DeliveryError>> getDeliveryErrorDevices() {
return connection.getDeliveryErrorDevices();
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/notnoop/apns/internal/BatchApnsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@

import static java.util.concurrent.Executors.defaultThreadFactory;

import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
Expand All @@ -41,6 +43,7 @@
import java.util.concurrent.TimeUnit;

import com.notnoop.apns.ApnsNotification;
import com.notnoop.apns.DeliveryError;
import com.notnoop.exceptions.NetworkIOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -140,4 +143,9 @@ public void run() {
}
}
}

@Override
public Map<String, Set<DeliveryError>> getDeliveryErrorDevices() {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
Expand All @@ -43,6 +44,7 @@

import com.notnoop.apns.ApnsNotification;
import com.notnoop.apns.ApnsService;
import com.notnoop.apns.DeliveryError;
import com.notnoop.exceptions.NetworkIOException;

public class QueuedApnsService extends AbstractApnsService {
Expand Down Expand Up @@ -123,4 +125,9 @@ public void testConnection() throws NetworkIOException {
service.testConnection();
}

@Override
public Map<String, Set<DeliveryError>> getDeliveryErrorDevices() {
throw new UnsupportedOperationException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
import static org.junit.Assert.*;

import java.io.IOException;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Semaphore;

import org.junit.Test;
import static org.mockito.Mockito.*;

import com.notnoop.apns.ApnsNotification;
import com.notnoop.apns.ApnsService;
import com.notnoop.apns.DeliveryError;
import com.notnoop.apns.EnhancedApnsNotification;
import com.notnoop.exceptions.NetworkIOException;

Expand Down Expand Up @@ -146,5 +149,10 @@ public void setCacheLength(int cacheLength) {
public int getCacheLength() {
return -1;
}

@Override
public Map<String, Set<DeliveryError>> getDeliveryErrorDevices() {
throw new UnsupportedOperationException();
}
}
}