package com.worklight.location.internal.events;

import com.worklight.common.Logger;
import com.worklight.location.api.WLEventTransmissionPolicy;
import com.worklight.location.internal.DeviceContextImpl;
import com.worklight.location.internal.PiggbackRegisterer;
import com.worklight.location.internal.events.server.EventServer;
import com.worklight.location.internal.events.storage.Chunk;
import com.worklight.location.internal.events.storage.IChunkStorage;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Timer;
import org.json.JSONObject;

public class EventTransmitter
{
  private static final long BYTES_IN_KILOBYTE = 1024L;
  private final DeviceContextImpl dc;
  private boolean eventsHaveBeenAdded = false;
  private boolean inTestingEnv = false;
  private boolean isClientConnected = false;
  private boolean isTransmitting = false;
  private long lastFlush = -1L;
  private Logger logger = Logger.getInstance("EventTransmitter");
  private Chunk openChunk;
  private WLEventTransmissionPolicy policy;
  private int purgeCounter = 0;
  private int retriesLeft;
  private EventServer server;
  private final IChunkStorage storage;
  private Timer timer;

  EventTransmitter(EventServer paramEventServer, DeviceContextImpl paramDeviceContextImpl, Chunk paramChunk, IChunkStorage paramIChunkStorage, WLEventTransmissionPolicy paramWLEventTransmissionPolicy, PiggbackRegisterer paramPiggbackRegisterer, PiggybackerFactory paramPiggybackerFactory, boolean paramBoolean)
  {
    if (paramEventServer == null)
      throw new IllegalArgumentException("server is null");
    this.server = paramEventServer;
    if (paramDeviceContextImpl == null)
      throw new IllegalArgumentException("dc is null");
    this.dc = paramDeviceContextImpl;
    if (paramChunk == null)
      throw new IllegalArgumentException("openChunk is null");
    this.openChunk = paramChunk;
    if (paramIChunkStorage == null)
      throw new IllegalArgumentException("storage is null");
    this.storage = paramIChunkStorage;
    this.policy = paramWLEventTransmissionPolicy;
    this.retriesLeft = paramWLEventTransmissionPolicy.getNumRetries();
    this.inTestingEnv = paramBoolean;
    setEventTransmissionPolicy(paramWLEventTransmissionPolicy);
    paramPiggbackRegisterer.register(paramPiggybackerFactory.create(this, 2000L));
  }

  public EventTransmitter(EventServer paramEventServer, DeviceContextImpl paramDeviceContextImpl, IChunkStorage paramIChunkStorage)
  {
    this(paramEventServer, paramDeviceContextImpl, new Chunk(1024L * WLEventTransmissionPolicy.getDefaultPolicy().getMaxChunkSize()), paramIChunkStorage, WLEventTransmissionPolicy.getDefaultPolicy(), new PiggbackRegisterer(), new PiggybackerFactory(), false);
  }

  private void addEventToChunk(JSONObject paramJSONObject, DeviceContextImpl paramDeviceContextImpl)
  {
    try
    {
      if (!paramJSONObject.has("deviceContext"))
        paramDeviceContextImpl.addToEvent(paramJSONObject);
      if (!this.openChunk.add(paramJSONObject))
      {
        this.storage.store(this.openChunk);
        this.openChunk = this.openChunk.createNewChunk();
        this.openChunk.add(paramJSONObject);
      }
      return;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      throw new AssertionError(localException.getMessage());
    }
    finally
    {
    }
  }

  private void clearTimer()
  {
    if (this.timer != null)
    {
      this.timer.cancel();
      this.timer = null;
    }
  }

  private void debugLog(String paramString)
  {
    if (!this.inTestingEnv)
      this.logger.debug(Thread.currentThread().getName() + " - " + paramString);
  }

  private boolean hasEvents()
  {
    return (this.eventsHaveBeenAdded) && ((this.openChunk.numberOfEvents() > 0) || (!this.storage.isEmpty()));
  }

  private void resetTimer()
  {
    clearTimer();
    if ((hasEvents()) && (!this.isTransmitting))
    {
      long l = this.policy.getInterval();
      if (this.lastFlush > 0L)
        l -= (System.currentTimeMillis() - this.lastFlush) % this.policy.getInterval();
      scheduleTimer(l);
    }
  }

  private void scheduleTimer(long paramLong)
  {
    this.timer = new Timer("Event Transmitter Timer");
    this.timer.schedule(new EventFlusher(this), paramLong);
  }

  public void flushEvents()
  {
    while (true)
    {
      try
      {
        debugLog("Flush called");
        boolean bool = this.isTransmitting;
        if (bool)
          return;
        if (!this.isClientConnected)
          continue;
        this.isTransmitting = true;
        clearTimer();
        this.lastFlush = System.currentTimeMillis();
        if (this.storage.isEmpty())
        {
          if (this.openChunk.numberOfEvents() > 0)
          {
            this.storage.store(this.openChunk);
            this.openChunk = this.openChunk.createNewChunk();
          }
        }
        else
        {
          int i = this.purgeCounter;
          debugLog("Transmitting...:");
          this.server.send(this.storage.getFirst(), new ChunkTransmissionRequestListener(this, i));
          continue;
        }
      }
      finally
      {
      }
      this.isTransmitting = false;
    }
  }

  public void flushEventsFromAsync()
  {
    try
    {
      if ((!this.isTransmitting) && (this.eventsHaveBeenAdded))
        flushEvents();
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  public IChunkStorage forTestOnly_getChunkStorage()
  {
    try
    {
      IChunkStorage localIChunkStorage = this.storage;
      return localIChunkStorage;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  public void forTestOnly_setEventServer(EventServer paramEventServer)
  {
    try
    {
      this.server = paramEventServer;
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  boolean isTransmitting()
  {
    try
    {
      boolean bool = this.isTransmitting;
      return bool;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  public void purgeEvents()
  {
    try
    {
      this.storage.purgeAll();
      this.openChunk = this.openChunk.createNewChunk();
      this.purgeCounter = (1 + this.purgeCounter);
      clearTimer();
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  public void setClientConnected(boolean paramBoolean)
  {
    try
    {
      this.isClientConnected = paramBoolean;
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  public void setEventTransmissionPolicy(WLEventTransmissionPolicy paramWLEventTransmissionPolicy)
  {
    if (paramWLEventTransmissionPolicy == null)
      try
      {
        throw new IllegalArgumentException("p is null");
      }
      finally
      {
      }
    this.policy = paramWLEventTransmissionPolicy.clone();
    long l = 1024L * paramWLEventTransmissionPolicy.getMaxChunkSize();
    if (paramWLEventTransmissionPolicy.isEventStorageEnabled())
      this.storage.setMaxmiumMemorySize(Math.max(l, 1024L * paramWLEventTransmissionPolicy.getMaxMemSize() - l));
    while (true)
    {
      if (this.openChunk.currentChunkSize() != 0L)
        this.storage.store(this.openChunk);
      this.openChunk = this.openChunk.createNewChunk(l);
      resetTimer();
      return;
      this.storage.setMaxmiumMemorySize(9223372036854775807L);
    }
  }

  public void transmitEvent(JSONObject paramJSONObject, boolean paramBoolean)
  {
    try
    {
      transmitEvents(Collections.singletonList(paramJSONObject), paramBoolean);
      return;
    }
    finally
    {
      localObject = finally;
      throw localObject;
    }
  }

  public void transmitEvents(List<JSONObject> paramList, boolean paramBoolean)
  {
    try
    {
      DeviceContextImpl localDeviceContextImpl = this.dc.clone();
      if (!paramList.isEmpty())
        this.eventsHaveBeenAdded = true;
      Iterator localIterator = paramList.iterator();
      while (localIterator.hasNext())
        addEventToChunk((JSONObject)localIterator.next(), localDeviceContextImpl);
    }
    finally
    {
    }
    if (!this.isTransmitting)
    {
      if (!paramBoolean)
        break label85;
      flushEvents();
    }
    while (true)
    {
      return;
      label85: if (this.timer == null)
        resetTimer();
    }
  }

  void txFailure()
  {
    try
    {
      this.isTransmitting = false;
      if (this.retriesLeft == 0)
      {
        debugLog("Failed to transmit " + this.policy.getNumRetries() + " times, giving up this transmission, will try again according to the given interval");
        this.retriesLeft = this.policy.getNumRetries();
        resetTimer();
      }
      while (true)
      {
        return;
        debugLog("Failed to transmit chunk, retrying");
        this.retriesLeft = (-1 + this.retriesLeft);
        scheduleTimer(this.policy.getRetryInterval());
      }
    }
    finally
    {
    }
  }

  void txSuccess(int paramInt)
  {
    try
    {
      debugLog("Succesfully transmitted a chunk");
      if (paramInt == this.purgeCounter)
        this.storage.removeFirst();
      this.retriesLeft = this.policy.getNumRetries();
      this.isTransmitting = false;
      if (hasEvents())
        flushEvents();
      return;
    }
    finally
    {
    }
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.worklight.location.internal.events.EventTransmitter
 * JD-Core Version:    0.6.2
 */