Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. public class BasicApnsConnection {
  2.  
  3. private final Socket apnsSocket;
  4. private final DataInputStream apnsIn;
  5. private final OutputStream apnsOut;
  6. private final ExecutorService apnsInExecutor;
  7. private final Future<ApnsException> inputstreamTask;
  8.  
  9. BasicApnsConnection(SocketFactory socketFactory, String host, int port)
  10. throws UnknownHostException, IOException {
  11. apnsSocket = socketFactory.createSocket();
  12. apnsSocket.setKeepAlive(true); // I tried to add this line, but it didn't work
  13. apnsSocket.connect(new InetSocketAddress(host, port));
  14.  
  15. apnsIn = new DataInputStream(this.apnsSocket.getInputStream());
  16. apnsOut = this.apnsSocket.getOutputStream();
  17. apnsInExecutor = Executors.newSingleThreadExecutor();
  18.  
  19. inputstreamTask = apnsInExecutor.submit(new InputstreamHandler());
  20. }
  21.  
  22. public synchronized void push(ApnsNotification notification)
  23. throws ApnsException {
  24. if (notification == null) {
  25. throw new IllegalArgumentException("notification is null");
  26. }
  27.  
  28. checkIfExceptionOccurred();
  29.  
  30. ByteArrayOutputStream bytesBuffer = new ByteArrayOutputStream();
  31. notification.serialize(bytesBuffer);
  32. byte[] dataBytes = bytesBuffer.toByteArray();
  33. try {
  34. apnsOut.write(dataBytes);
  35. apnsOut.flush();
  36. } catch (IOException ex) {
  37. throw new ApnsNetworkException("IOException occurred during " +
  38. "pushing a notification to the APNS server", ex);
  39. }
  40. }
  41.  
  42. private void checkIfExceptionOccurred() throws ApnsException {
  43. try {
  44. if (inputstreamTask.isDone()) {
  45. throw inputstreamTask.get();
  46. }
  47. } catch (InterruptedException e) {
  48. e.printStackTrace();
  49. } catch (ExecutionException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53.  
  54. private class InputstreamHandler implements Callable<ApnsException> {
  55.  
  56. @Override
  57. public ApnsException call() throws IOException, ApnsException {
  58. byte[] errorResponse = new byte[6];
  59. try {
  60. apnsIn.readFully(errorResponse);
  61.  
  62. byte command = errorResponse[0];
  63. if (ApnsCommand.ERROR_COMMAND.getCode() != command) {
  64. return new ApnsException(String.format(
  65. "Unknwon command: %b from the APNS error response",
  66. command));
  67. }
  68.  
  69. byte status = errorResponse[1];
  70. int identifier = SerializationUtils.toInt(errorResponse, 2);
  71. if (status == ErrorStatus.NO_ERRORS) {
  72. return new ApnsException("no errors"); // never reaches
  73. } else if (status > ErrorStatus.NO_ERRORS &&
  74. status <= ErrorStatus.INVALID_TOKEN) {
  75. return new ApnsMalformedException(status, identifier);
  76. } else if (status == ErrorStatus.SHUTDOWN) {
  77. return new ApnsShutdownException(identifier);
  78. } else {
  79. return new ApnsException("Unknown");
  80. }
  81. } catch (IOException ex) {
  82. return new ApnsNetworkException(
  83. "IOException occurred while pushing a notification",
  84. ex);
  85. } catch (Exception ex) {
  86. return new ApnsException("Unknown exception", ex);
  87. }
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement