package com.splunk.mint.network.socket;

import com.splunk.mint.Logger;
import com.splunk.mint.Properties;
import com.splunk.mint.network.Counter;
import com.splunk.mint.network.Metric;
import com.splunk.mint.network.MonitorRegistry;
import com.splunk.mint.network.NetLogManager;
import com.splunk.mint.network.io.InputStreamMonitor;
import com.splunk.mint.network.io.InputStreamMonitorKitKat;
import com.splunk.mint.network.io.OutputStreamMonitor;
import com.splunk.mint.network.util.Delegator;
import com.splunk.mint.network.util.Delegator.DelegatorMethodFinder;
import com.splunk.mint.network.util.ReflectionUtil;
import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class MonitoringSocketImpl extends SocketImpl
{
  public static final String ENCAPSULATED_SOCKET_IMPL = "java.net.PlainSocketImpl";
  private static final String[] HTTP_CLASSES = { "HttpClient", "URLConnection", "HttpsURLConnectionImpl", "HttpURLConnectionImpl" };
  private static final String[] SYSTEM_PACKAGES = { "android", "java", "org.apache", "splunk", "libcore" };
  private final Delegator delegator = new Delegator(this, SocketImpl.class, "java.net.PlainSocketImpl");
  private final boolean http = ReflectionUtil.callingClassAnyOf(HTTP_CLASSES);
  private InputStreamMonitor mInputStreamMonitor = null;
  private InputStreamMonitorKitKat mInputStreamMonitorKitKat = null;
  private OutputStreamMonitor mOutputStreamMonitor = null;
  private final String method = ReflectionUtil.extractCallingMethod(SYSTEM_PACKAGES);
  private String name;
  private String protocol = "NA";
  private boolean readingDone = false;
  private final MonitorRegistry registry;
  private long startTime;

  public MonitoringSocketImpl(MonitorRegistry paramMonitorRegistry)
  {
    this.registry = paramMonitorRegistry;
  }

  private void createActionEventFromCollectedStats(ArrayList<Metric<?>> paramArrayList, HashMap<String, List<String>> paramHashMap1, HashMap<String, List<String>> paramHashMap2, long paramLong, String paramString1, String paramString2)
  {
    Object localObject = "";
    if (paramHashMap1 != null);
    try
    {
      localObject = (String)((List)paramHashMap1.get("Host")).get(0);
      try
      {
        label28: String str = (String)localObject + (String)((List)paramHashMap1.get("splk-host2")).get(0);
        localObject = str;
        label70: if (paramString1 != null)
          localObject = paramString2;
        int i = 0;
        if (paramHashMap2 != null);
        try
        {
          int j = Integer.valueOf((String)((List)paramHashMap2.get("splk-statuscode")).get(0)).intValue();
          i = j;
          Long localLong = Long.valueOf(0L);
          long l1 = 0L;
          Iterator localIterator = paramArrayList.iterator();
          while (localIterator.hasNext())
          {
            Metric localMetric = (Metric)localIterator.next();
            if ((localMetric instanceof Counter))
              if (((Counter)localMetric).getName().endsWith("-bytes-out"))
                localLong = (Long)localMetric.getValue();
              else if (((Counter)localMetric).getName().endsWith("-bytes-in"))
                l1 = ((Long)localMetric.getValue()).longValue();
          }
          try
          {
            long l2 = Long.valueOf((String)((List)paramHashMap2.get("Content-Length")).get(0)).longValue();
            l1 = l2;
            NetLogManager.getInstance().logNetworkRequest((String)localObject, this.protocol, this.startTime, paramLong, i, localLong.longValue(), l1, paramString1, null);
            return;
          }
          catch (Exception localException1)
          {
            while (true)
              Logger.logInfo("Could not read the Content-Length HTTP header value");
          }
        }
        catch (Exception localException2)
        {
          while (true)
            i = 0;
        }
      }
      catch (Exception localException4)
      {
        break label70;
      }
    }
    catch (Exception localException3)
    {
      break label28;
    }
  }

  private void setProtocolFromPort(int paramInt)
  {
    if (paramInt == 80)
      this.protocol = "HTTP";
    while (paramInt != 443)
      return;
    this.protocol = "HTTPS";
  }

  protected void accept(SocketImpl paramSocketImpl)
    throws IOException
  {
    try
    {
      this.delegator.invoke(new Object[] { paramSocketImpl });
      return;
    }
    catch (Exception localException)
    {
      if ((localException instanceof IOException))
        throw ((IOException)localException);
      createActionEventFromCollectedStats(this.registry.getMetricsForName(this.name), null, null, System.currentTimeMillis(), localException.getMessage(), this.name);
    }
  }

  protected int available()
    throws IOException
  {
    try
    {
      int j = ((Integer)this.delegator.invoke(new Object[0])).intValue();
      i = j;
      return i;
    }
    catch (Exception localException)
    {
      boolean bool;
      do
      {
        bool = localException instanceof IOException;
        int i = 0;
      }
      while (!bool);
      throw ((IOException)localException);
    }
  }

  protected void bind(InetAddress paramInetAddress, int paramInt)
    throws IOException
  {
    this.name = paramInetAddress.getHostName();
    try
    {
      Delegator localDelegator = this.delegator;
      Object[] arrayOfObject = new Object[2];
      arrayOfObject[0] = paramInetAddress;
      arrayOfObject[1] = Integer.valueOf(paramInt);
      localDelegator.invoke(arrayOfObject);
      this.startTime = System.currentTimeMillis();
      return;
    }
    catch (Exception localException)
    {
      while (true)
      {
        if ((localException instanceof IOException))
          throw ((IOException)localException);
        createActionEventFromCollectedStats(this.registry.getMetricsForName(this.name), null, null, System.currentTimeMillis(), localException.getMessage(), this.name);
      }
    }
  }

  protected void close()
    throws IOException
  {
    try
    {
      this.delegator.invoke(new Object[0]);
      return;
    }
    catch (Exception localException)
    {
      Logger.logError("Error closing socket impl: " + localException.getMessage());
      if ((localException instanceof IOException))
        throw ((IOException)localException);
      createActionEventFromCollectedStats(this.registry.getMetricsForName(this.name), null, null, System.currentTimeMillis(), localException.getMessage(), this.name);
    }
  }

  protected void connect(String paramString, int paramInt)
    throws IOException
  {
    this.name = paramString;
    try
    {
      Delegator localDelegator = this.delegator;
      Object[] arrayOfObject = new Object[2];
      arrayOfObject[0] = paramString;
      arrayOfObject[1] = Integer.valueOf(paramInt);
      localDelegator.invoke(arrayOfObject);
      this.startTime = System.currentTimeMillis();
      setProtocolFromPort(paramInt);
      return;
    }
    catch (Exception localException)
    {
      while (true)
      {
        if ((localException instanceof IOException))
          throw ((IOException)localException);
        createActionEventFromCollectedStats(this.registry.getMetricsForName(this.name), null, null, System.currentTimeMillis(), NetLogManager.exceptionToString(localException), this.name);
      }
    }
  }

  protected void connect(InetAddress paramInetAddress, int paramInt)
    throws IOException
  {
    this.name = paramInetAddress.getHostName();
    try
    {
      Delegator localDelegator = this.delegator;
      Class[] arrayOfClass = new Class[2];
      arrayOfClass[0] = InetAddress.class;
      arrayOfClass[1] = Integer.TYPE;
      Delegator.DelegatorMethodFinder localDelegatorMethodFinder = localDelegator.delegateTo("connect", arrayOfClass);
      Object[] arrayOfObject = new Object[2];
      arrayOfObject[0] = paramInetAddress;
      arrayOfObject[1] = Integer.valueOf(paramInt);
      localDelegatorMethodFinder.invoke(arrayOfObject);
      this.startTime = System.currentTimeMillis();
      setProtocolFromPort(paramInt);
      return;
    }
    catch (Exception localException)
    {
      while (true)
      {
        if ((localException instanceof IOException))
          throw ((IOException)localException);
        createActionEventFromCollectedStats(this.registry.getMetricsForName(this.name), null, null, System.currentTimeMillis(), localException.getMessage(), this.name);
      }
    }
  }

  protected void connect(SocketAddress paramSocketAddress, int paramInt)
    throws IOException
  {
    if ((paramSocketAddress instanceof InetSocketAddress))
    {
      InetSocketAddress localInetSocketAddress = (InetSocketAddress)paramSocketAddress;
      this.name = localInetSocketAddress.getHostName();
      setProtocolFromPort(localInetSocketAddress.getPort());
    }
    try
    {
      while (true)
      {
        Delegator localDelegator = this.delegator;
        Object[] arrayOfObject = new Object[2];
        arrayOfObject[0] = paramSocketAddress;
        arrayOfObject[1] = Integer.valueOf(paramInt);
        localDelegator.invoke(arrayOfObject);
        this.startTime = System.currentTimeMillis();
        return;
        this.name = paramSocketAddress.toString();
      }
    }
    catch (Exception localException)
    {
      while (true)
      {
        if ((localException instanceof IOException))
          throw ((IOException)localException);
        createActionEventFromCollectedStats(this.registry.getMetricsForName(this.name), null, null, System.currentTimeMillis(), NetLogManager.exceptionToString(localException), this.name);
      }
    }
  }

  protected void create(boolean paramBoolean)
    throws IOException
  {
    try
    {
      Delegator localDelegator = this.delegator;
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = Boolean.valueOf(paramBoolean);
      localDelegator.invoke(arrayOfObject);
      return;
    }
    catch (Exception localException)
    {
      if ((localException instanceof IOException))
        throw ((IOException)localException);
      createActionEventFromCollectedStats(this.registry.getMetricsForName(this.name), null, null, System.currentTimeMillis(), localException.getMessage(), this.name);
    }
  }

  protected FileDescriptor getFileDescriptor()
  {
    try
    {
      FileDescriptor localFileDescriptor = (FileDescriptor)this.delegator.invoke(new Object[0]);
      return localFileDescriptor;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return null;
  }

  protected InetAddress getInetAddress()
  {
    try
    {
      InetAddress localInetAddress = (InetAddress)this.delegator.invoke(new Object[0]);
      return localInetAddress;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return null;
  }

  protected InputStream getInputStream()
    throws IOException
  {
    try
    {
      localInputStream = (InputStream)this.delegator.invoke(new Object[0]);
      InputStreamMonitorKitKat localInputStreamMonitorKitKat = null;
      if (localInputStream != null)
      {
        if (Properties.isKitKat)
        {
          if (this.mInputStreamMonitorKitKat == null)
            this.mInputStreamMonitorKitKat = new InputStreamMonitorKitKat(this.name, this.registry, localInputStream, this);
          localInputStreamMonitorKitKat = this.mInputStreamMonitorKitKat;
        }
      }
      else
        return localInputStreamMonitorKitKat;
    }
    catch (Exception localException)
    {
      InputStream localInputStream;
      while (true)
      {
        if ((localException instanceof IOException))
          throw ((IOException)localException);
        createActionEventFromCollectedStats(this.registry.getMetricsForName(this.name), null, null, System.currentTimeMillis(), localException.getMessage(), this.name);
        localInputStream = null;
      }
      if (this.mInputStreamMonitor == null)
        this.mInputStreamMonitor = new InputStreamMonitor(this.name, this.registry, localInputStream, this);
    }
    return this.mInputStreamMonitor;
  }

  protected int getLocalPort()
  {
    try
    {
      int i = ((Integer)this.delegator.invoke(new Object[0])).intValue();
      return i;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return -1;
  }

  public String getMethod()
  {
    return this.method;
  }

  public Object getOption(int paramInt)
    throws SocketException
  {
    try
    {
      Delegator localDelegator = this.delegator;
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = Integer.valueOf(paramInt);
      Object localObject = localDelegator.invoke(arrayOfObject);
      return localObject;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return null;
  }

  protected OutputStream getOutputStream()
    throws IOException
  {
    try
    {
      localOutputStream = (OutputStream)this.delegator.invoke(new Object[0]);
      if (localOutputStream != null)
      {
        if (this.mOutputStreamMonitor == null)
          this.mOutputStreamMonitor = new OutputStreamMonitor(this.name, this.registry, localOutputStream);
        return this.mOutputStreamMonitor;
      }
    }
    catch (Exception localException)
    {
      while (true)
      {
        if ((localException instanceof IOException))
          throw ((IOException)localException);
        localException.printStackTrace();
        OutputStream localOutputStream = null;
      }
    }
    return null;
  }

  protected int getPort()
  {
    try
    {
      int i = ((Integer)this.delegator.invoke(new Object[0])).intValue();
      return i;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return -1;
  }

  public boolean isHttp()
  {
    return this.http;
  }

  protected void listen(int paramInt)
    throws IOException
  {
    try
    {
      Delegator localDelegator = this.delegator;
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = Integer.valueOf(paramInt);
      localDelegator.invoke(arrayOfObject);
      return;
    }
    catch (Exception localException)
    {
      if ((localException instanceof IOException))
        throw ((IOException)localException);
      localException.printStackTrace();
    }
  }

  public void readingDone()
  {
    if (!this.readingDone)
    {
      this.readingDone = true;
      if (!Properties.isKitKat)
        break label73;
      if ((this.mOutputStreamMonitor != null) && (this.mInputStreamMonitorKitKat != null))
        createActionEventFromCollectedStats(this.registry.getMetricsForName(this.name), this.mOutputStreamMonitor.getHeaders(), this.mInputStreamMonitorKitKat.getHeaders(), System.currentTimeMillis(), this.mInputStreamMonitorKitKat.exception, null);
    }
    label73: 
    while ((this.mOutputStreamMonitor == null) || (this.mInputStreamMonitor == null))
      return;
    createActionEventFromCollectedStats(this.registry.getMetricsForName(this.name), this.mOutputStreamMonitor.getHeaders(), this.mInputStreamMonitor.getHeaders(), System.currentTimeMillis(), this.mInputStreamMonitor.exception, null);
  }

  protected void sendUrgentData(int paramInt)
    throws IOException
  {
    try
    {
      Delegator localDelegator = this.delegator;
      Object[] arrayOfObject = new Object[1];
      arrayOfObject[0] = Integer.valueOf(paramInt);
      localDelegator.invoke(arrayOfObject);
      return;
    }
    catch (Exception localException)
    {
      if ((localException instanceof IOException))
        throw ((IOException)localException);
      localException.printStackTrace();
    }
  }

  public void setOption(int paramInt, Object paramObject)
    throws SocketException
  {
    try
    {
      Delegator localDelegator = this.delegator;
      Object[] arrayOfObject = new Object[2];
      arrayOfObject[0] = Integer.valueOf(paramInt);
      arrayOfObject[1] = paramObject;
      localDelegator.invoke(arrayOfObject);
      return;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  protected void setPerformancePreferences(int paramInt1, int paramInt2, int paramInt3)
  {
    try
    {
      Delegator localDelegator = this.delegator;
      Object[] arrayOfObject = new Object[3];
      arrayOfObject[0] = Integer.valueOf(paramInt1);
      arrayOfObject[1] = Integer.valueOf(paramInt2);
      arrayOfObject[2] = Integer.valueOf(paramInt3);
      localDelegator.invoke(arrayOfObject);
      return;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  protected void shutdownInput()
    throws IOException
  {
    if (Properties.isKitKat)
      if (this.mInputStreamMonitorKitKat != null)
        this.mInputStreamMonitorKitKat.close();
    try
    {
      while (true)
      {
        this.delegator.invoke(new Object[0]);
        return;
        if (this.mInputStreamMonitor != null)
          this.mInputStreamMonitor.close();
      }
    }
    catch (IOException localIOException)
    {
      throw localIOException;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
  }

  protected void shutdownOutput()
    throws IOException
  {
    if (this.mOutputStreamMonitor != null)
      this.mOutputStreamMonitor.close();
    try
    {
      this.delegator.invoke(new Object[0]);
      return;
    }
    catch (IOException localIOException)
    {
      Logger.logError("AAAAA");
      throw localIOException;
    }
    catch (Exception localException)
    {
      Logger.logError("VBBBBBB");
      localException.printStackTrace();
    }
  }

  protected boolean supportsUrgentData()
  {
    try
    {
      boolean bool = ((Boolean)this.delegator.invoke(new Object[0])).booleanValue();
      return bool;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
    }
    return false;
  }
}

/* Location:           C:\Documents and Settings\Cesar Cabello Cea\Mis documentos\Downloads\apk-downloader\com.ikea.kompis-6_dex2jar.jar
 * Qualified Name:     com.splunk.mint.network.socket.MonitoringSocketImpl
 * JD-Core Version:    0.6.2
 */