Exemplo n.º 1
0
  private synchronized Socket getOrCreateSocket(boolean resend) throws NetworkIOException {
    if (reconnectPolicy.shouldReconnect()) {
      logger.debug("Reconnecting due to reconnectPolicy dictating it");
      UILogger.getInstance().add("Reconnecting due to reconnectPolicy dictating it");
      Utilities.close(socket);
      socket = null;
    }

    if (socket == null || socket.isClosed()) {
      try {
        if (proxy == null) {
          socket = factory.createSocket(host, port);
          logger.debug("Connected new socket {}", socket);
          UILogger.getInstance().add("Connected new socket" + socket.toString());
        } else if (proxy.type() == Proxy.Type.HTTP) {
          TlsTunnelBuilder tunnelBuilder = new TlsTunnelBuilder();
          socket =
              tunnelBuilder.build(
                  (SSLSocketFactory) factory, proxy, proxyUsername, proxyPassword, host, port);
          logger.debug("Connected new socket through http tunnel {}", socket);
          UILogger.getInstance()
              .add("Connected new socket through http tunnel " + socket.toString());
        } else {
          boolean success = false;
          Socket proxySocket = null;
          try {
            proxySocket = new Socket(proxy);
            proxySocket.connect(new InetSocketAddress(host, port), connectTimeout);
            socket = ((SSLSocketFactory) factory).createSocket(proxySocket, host, port, false);
            success = true;
          } finally {
            if (!success) {
              Utilities.close(proxySocket);
            }
          }
          logger.debug("Connected new socket through socks tunnel {}", socket);
          UILogger.getInstance()
              .add("Connected new socket through socks tunnel " + socket.toString());
        }

        socket.setSoTimeout(readTimeout);
        socket.setKeepAlive(true);

        if (errorDetection) {
          monitorSocket(socket);
        }

        reconnectPolicy.reconnected();
        logger.debug("Made a new connection to APNS");
        UILogger.getInstance().add("Made a new connection to APNS");
      } catch (IOException e) {
        logger.error("Couldn't connect to APNS server", e);
        UILogger.getInstance().add("Couldn't connect to APNS server" + e.toString());
        // indicate to clients whether this is a resend or initial send
        throw new NetworkIOException(e, resend);
      }
    }
    return socket;
  }
Exemplo n.º 2
0
 public ApnsConnectionImpl copy() {
   return new ApnsConnectionImpl(
       factory,
       host,
       port,
       proxy,
       proxyUsername,
       proxyPassword,
       reconnectPolicy.copy(),
       delegate,
       errorDetection,
       threadFactory,
       cacheLength,
       autoAdjustCacheLength,
       readTimeout,
       connectTimeout);
 }
Exemplo n.º 3
0
 public void testConnection() throws NetworkIOException {
   ApnsConnectionImpl testConnection = null;
   try {
     testConnection =
         new ApnsConnectionImpl(
             factory,
             host,
             port,
             proxy,
             proxyUsername,
             proxyPassword,
             reconnectPolicy.copy(),
             delegate);
     final ApnsNotification notification =
         new EnhancedApnsNotification(0, 0, new byte[] {0}, new byte[] {0});
     testConnection.sendMessage(notification);
   } finally {
     if (testConnection != null) {
       testConnection.close();
     }
   }
 }