package org.chenyang.http.impl.client;

import org.chenyang.http.client.HttpClient;
import org.chenyang.http.client.ResponseHandler;
import org.chenyang.http.client.methods.HttpUriRequest;
import org.chenyang.http.concurrent.FutureCallback;
import org.chenyang.http.protocol.HttpContext;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicBoolean;

class HttpRequestTaskCallable<V> implements Callable<V> {
   private final HttpUriRequest request;
   private final HttpClient httpclient;
   private final AtomicBoolean cancelled = new AtomicBoolean(false);
   private final long scheduled = System.currentTimeMillis();
   private long started = -1L;
   private long ended = -1L;
   private final HttpContext context;
   private final ResponseHandler<V> responseHandler;
   private final FutureCallback<V> callback;
   private final FutureRequestExecutionMetrics metrics;

   HttpRequestTaskCallable(HttpClient httpClient, HttpUriRequest request, HttpContext context, ResponseHandler<V> responseHandler, FutureCallback<V> callback, FutureRequestExecutionMetrics metrics) {
      this.httpclient = httpClient;
      this.responseHandler = responseHandler;
      this.request = request;
      this.context = context;
      this.callback = callback;
      this.metrics = metrics;
   }

   public long getScheduled() {
      return this.scheduled;
   }

   public long getStarted() {
      return this.started;
   }

   public long getEnded() {
      return this.ended;
   }

   public V call() throws Exception {
      if (!this.cancelled.get()) {
         Object var2;
         try {
            this.metrics.getActiveConnections().incrementAndGet();
            this.started = System.currentTimeMillis();

            try {
               this.metrics.getScheduledConnections().decrementAndGet();
               V result = (V)this.httpclient.execute(this.request, this.responseHandler, this.context);
               this.ended = System.currentTimeMillis();
               this.metrics.getSuccessfulConnections().increment(this.started);
               if (this.callback != null) {
                  this.callback.completed(result);
               }

               var2 = result;
            } catch (Exception var6) {
               this.metrics.getFailedConnections().increment(this.started);
               this.ended = System.currentTimeMillis();
               if (this.callback != null) {
                  this.callback.failed(var6);
               }

               throw var6;
            }
         } finally {
            this.metrics.getRequests().increment(this.started);
            this.metrics.getTasks().increment(this.started);
            this.metrics.getActiveConnections().decrementAndGet();
         }

         return (V)var2;
      } else {
         throw new IllegalStateException("call has been cancelled for request " + this.request.getURI());
      }
   }

   public void cancel() {
      this.cancelled.set(true);
      if (this.callback != null) {
         this.callback.cancelled();
      }

   }
}
