package com.l7tech.gateway.common.cluster;

import java.util.Vector;
import java.util.logging.Logger;

public class GatewayStatus {
   public static final int REFRESH_INTERVAL = 5;
   public static final int STATUS_REFRESH_TIMER = 5000;
   public static final int NUMBER_OF_SAMPLE_PER_MINUTE = 12;
   public static final int MAX_UPDATE_FAILURE_COUNT = 2;
   public static final int NODE_STATUS_UNKNOWN = -1;
   public static final int NODE_STATUS_ACTIVE = 1;
   public static final int NODE_STATUS_INACTIVE = 0;
   private final ClusterNodeInfo a;
   private int b;
   private int c;
   private int d;
   private boolean e;
   private boolean f;
   private Vector g = new Vector();
   private Vector h = new Vector();
   private long i;
   private int j;
   static Logger k = Logger.getLogger(GatewayStatus.class.getName());

   public GatewayStatus(ClusterNodeInfo var1) {
      this.a = var1;
      this.e = true;
      this.f = true;
      this.i = -1L;
      this.j = 0;
      this.c = -1;
   }

   public String getName() {
      return this.a.getName();
   }

   public int getStatus() {
      return this.b;
   }

   public int getLastState() {
      return this.c;
   }

   public void setLastState(int var1) {
      this.c = var1;
   }

   public int getLoadSharing() {
      return this.d;
   }

   public int getRequestRouted() {
      long var1 = this.a(this.g);
      if (var1 > 0L) {
         long var3 = this.a(this.h);
         return Long.valueOf(var3 * 100L / var1).intValue();
      } else {
         return 0;
      }
   }

   public double getAvgLoad() {
      return this.a.getAvgLoad();
   }

   public long getUptime() {
      long var1 = this.a.getUptime();
      if (this.b == -1) {
         var1 = -1L;
      }

      return var1;
   }

   public String getAddress() {
      return this.a.getAddress();
   }

   public long getLastUpdateTimeStamp() {
      return this.a.getLastUpdateTimeStamp();
   }

   public void setStatus(int var1) {
      this.b = var1;
   }

   public void setLoadSharing(int var1) {
      this.d = var1;
   }

   public void updateRequestCounterCache(long var1) {
      if (this.e) {
         this.a(this.g, var1, true);
         this.e = false;
      } else {
         this.a(this.g, var1, false);
      }
   }

   public void updateCompletedCounterCache(long var1) {
      if (this.f) {
         this.a(this.h, var1, true);
         this.f = false;
      } else {
         this.a(this.h, var1, false);
      }
   }

   private void a(Vector var1, long var2, boolean var4) {
      if (var4) {
         if (var1.size() <= 12) {
            var1.add(var2);
         } else {
            var1.remove(0);
            var1.add(var2);
         }
      } else {
         Long var5 = (Long)var1.lastElement() + var2;
         var1.remove(var1.size() - 1);
         var1.add(var5);
      }
   }

   private long a(Vector var1) {
      long var2 = 0L;
      int var4 = var1.size() - 1;

      for (int var5 = 0; var5 < var1.size() - 1; var4--) {
         var2 += var1.get(var4) - var1.get(var4 - 1);
         var5++;
      }

      return var2;
   }

   public long getRequestCount() {
      return this.a(this.g);
   }

   public void setRequestCounterCache(Vector var1) {
      this.g = var1;
   }

   public void setCompletedCounterCache(Vector var1) {
      this.h = var1;
   }

   public Vector getRequestCounterCache() {
      return this.g;
   }

   public Vector getCompletedCounterCache() {
      return this.h;
   }

   public void resetCacheUpdateFlag() {
      this.e = true;
      this.f = true;
   }

   public long getSecondLastUpdateTimeStamp() {
      return this.i;
   }

   public void setSecondLastUpdateTimeStamp(long var1) {
      this.i = var1;
   }

   public String getNodeId() {
      return this.a.getNodeIdentifier();
   }

   public void incrementTimeStampUpdateFailureCount() {
      if (this.j > 2) {
         this.j = 2;
      } else {
         this.j++;
      }
   }

   public void resetTimeStampUpdateFailureCount() {
      this.j = 0;
   }

   public int getTimeStampUpdateFailureCount() {
      return this.j;
   }

   public void setTimeStampUpdateFailureCount(int var1) {
      this.j = var1;
   }
}
