Ver Mensaje Individual
  #5 (permalink)  
Antiguo 31/10/2013, 21:26
martin0290
 
Fecha de Ingreso: octubre-2009
Mensajes: 84
Antigüedad: 14 años, 6 meses
Puntos: 0
Respuesta: Servidor HTTP Proxy en Java

Tu proxy esta muy bien hecho. Me ayudo muchísimo en mi investigación. Te dejo un consejo para limitar el ancho de banda a tu gusto.

- En la clase "InputStreamConsumer" donde haces el write, "this.output.write(bytesBuffer, 0, bytesReaded);" lo remplazas por esto:

Código:
     
            int total = 0; // Total de la descarga actual
            int limitador = 130; // Limite kbps
            long ComienzoDescarga = System.currentTimeMillis();
            do {

              .......


                try {
                    this.output.write(bytesBuffer, 0, bytesLeidos);

                    total += bytesLeidos;
                    long FinDescarga = System.currentTimeMillis() - ComienzoDescarga;
                    int kbps = (int) (((1000f * total) / FinDescarga) / 1000);

                    if (kbps < Integer.MAX_VALUE) {
                        if (kbps >= limitador) {
                            try {
                                Thread.sleep((ConfiguracionPrivada.PROXY_BUFFER / limitador) * 5);
                            } catch (ArithmeticException ex) {
                                Thread.sleep(0);
                            }
                            
                        }
                    }


                    this.output.flush();

                } catch (IOException | InterruptedException e) {
                }

- Lo que hace es simple, toma la descarga inicial y cuenta el tiempo hasta que termine, luego hace un SLEEP el cual va a limitar el los kbps.

- Estoy viendo el código y esta bastante limpio, estuve mucho tiempo buscando proxys estables para hacer diferentes pruebas y el tuyo es el mas simple y mejor echo!


Saludos

Última edición por martin0290; 11/04/2017 a las 09:54