#!python
"""
Created on Mar 25, 2015

@author: Maribel Acosta
"""

from sys import argv, exit
from delayproxy.basicproxy import ProxyHandler, ThreadingHTTPServer

protocol="HTTP/1.0"

if __name__ == '__main__':
    if len(argv) < 6:
        print "Proxy usage: run-delay-proxy port server alpha beta randomseed [buffsize]"
        print "\t port \t\tListening port for the proxy (int)"
        print "\t server \tIp address and port of the server (ip:port)"
        print "\t alpha \t\tAlpha parameter for gamma distribution (int)"
        print "\t beta \t\tBeta parameter for gamma distribution (float)"
        print "\t randomseed \tSeed for the delays in the proxy (int)"
        print "\t buffsize \tBuffersize that specifies the size of the data chunks sent to the client (int)"
        exit(0)

    ProxyHandler.address = ('', int(argv[1]))
    ProxyHandler.server = argv[2]
    ProxyHandler.alpha = int(argv[3])
    ProxyHandler.beta = float(argv[4])
    ProxyHandler.seed = int(argv[5])

    if len(argv) > 6:
        ProxyHandler.rbufsize = int(argv[6])

    ProxyHandler.protocol_version = protocol
    httpd = ThreadingHTTPServer(ProxyHandler.address, ProxyHandler)

    sa = httpd.socket.getsockname()
    print "Serving HTTP on", sa[0], "port", sa[1], "..."
    httpd.serve_forever()
