package org.chenyang.http.impl.conn;

import org.chenyang.commons.logging.Log;
import org.chenyang.http.HttpClientConnection;
import org.chenyang.http.annotation.Contract;
import org.chenyang.http.annotation.ThreadingBehavior;
import org.chenyang.http.conn.ManagedHttpClientConnection;
import org.chenyang.http.conn.routing.HttpRoute;
import org.chenyang.http.pool.PoolEntry;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Contract(
   threading = ThreadingBehavior.SAFE
)
class CPoolEntry extends PoolEntry<HttpRoute, ManagedHttpClientConnection> {
   private final Log log;
   private volatile boolean routeComplete;

   public CPoolEntry(Log log, String id, HttpRoute route, ManagedHttpClientConnection conn, long timeToLive, TimeUnit timeUnit) {
      super(id, route, conn, timeToLive, timeUnit);
      this.log = log;
   }

   public void markRouteComplete() {
      this.routeComplete = true;
   }

   public boolean isRouteComplete() {
      return this.routeComplete;
   }

   public void closeConnection() throws IOException {
      HttpClientConnection conn = (HttpClientConnection)this.getConnection();
      conn.close();
   }

   public void shutdownConnection() throws IOException {
      HttpClientConnection conn = (HttpClientConnection)this.getConnection();
      conn.shutdown();
   }

   public boolean isExpired(long now) {
      boolean expired = super.isExpired(now);
      if (expired && this.log.isDebugEnabled()) {
         this.log.debug("Connection " + this + " expired @ " + new Date(this.getExpiry()));
      }

      return expired;
   }

   public boolean isClosed() {
      HttpClientConnection conn = (HttpClientConnection)this.getConnection();
      return !conn.isOpen();
   }

   public void close() {
      try {
         this.closeConnection();
      } catch (IOException ex) {
         this.log.debug("I/O error closing connection", ex);
      }

   }
}
