Esempio n. 1
0
  def __init__(self, load=None, **kwargs):
    if load is None:
      args = {}
    else:
      args = util.load_params(load, 'train')
      
    util.update(args, mode=RL.Mode.TRAIN, **kwargs)
    print(args)
    Default.__init__(self, **args)
    
    if self.init:
      self.model.init()
      self.model.save()
    else:
      self.model.restore()

    context = zmq.Context.instance()

    self.experience_socket = context.socket(zmq.PULL)
    experience_addr = "tcp://%s:%d" % (self.dump, util.port(self.model.name + "/experience"))
    self.experience_socket.bind(experience_addr)
    
    self.params_socket = context.socket(zmq.PUB)
    params_addr = "tcp://%s:%d" % (self.dump, util.port(self.model.name + "/params"))
    print("Binding params socket to", params_addr)
    self.params_socket.bind(params_addr)

    self.sweep_size = self.batches * self.batch_size
    print("Sweep size", self.sweep_size)
    
    self.buffer = util.CircularQueue(self.sweep_size)
    
    self.last_save = time.time()
Esempio n. 2
0
	def addself(self):
		connection = httplib.HTTPConnection(util.host(self.controller), util.port(controller), True, 10)
		connection.request("GET", "/add" + self.type + "?host=" + self.selfhost)
		response = connection.getresponse()
		data = response.read()
		lines = data.split("\n")
		self.handle_connect(lines)
Esempio n. 3
0
def request(host, path, postdata = None):
	if postdata == None:
		type = "GET"
		body = ""
	else:
		type = "POST"
		body = postdata
	connection = httplib.HTTPConnection(util.host(host), util.port(host), True, 10)
	connection.request(type, path, body)
	response = connection.getresponse()
	data = response.read()
	connection.close()
	return data
Esempio n. 4
0
def generate(config):
    iptables_location = config["iptables_location"]
    public_ip = config["public_ip"]
    current_ip = config["base_ip"]
    current_port = config["base_port"]

    iptables_content = generate_iptables('80', public_ip, current_ip, current_port, iptables_location)
    current_port += 1
    iptables_content += generate_iptables('443', public_ip, current_ip, current_port, iptables_location)
    current_port += 1
    for group in config["groups"].values():
        for proxy in group["proxies"]:
            if proxy["dnat"]:
                current_ip = long2ip(ip2long(current_ip) + 1)
                for protocol in proxy["protocols"]:
                    iptables_content += generate_iptables(port(protocol), public_ip, current_ip, current_port, iptables_location)
                    current_port += 1
    return iptables_content
Esempio n. 5
0
def generate(config):
    public_ip = config["public_ip"]
    current_ip = config["base_ip"]
    current_port = config["base_port"]

    rinetd_content = generate_rinetd('80', public_ip, current_ip, current_port)
    current_port += 1
    rinetd_content += generate_rinetd('443', public_ip, current_ip, current_port)
    current_port += 1

    for group in config["groups"].values():
        for proxy in group["proxies"]:
            if proxy["dnat"]:
                current_ip = long2ip(ip2long(current_ip) + 1)
                for protocol in proxy["protocols"]:
                    rinetd_content += generate_rinetd(port(protocol), public_ip, current_ip, current_port)
                    current_port += 1
    return rinetd_content
Esempio n. 6
0
def generate(config):
    public_ip = config["public_ip"]
    current_ip = config["base_ip"]
    current_port = config["base_port"]

    netsh_content = generate_netsh('80', public_ip, current_ip, current_port)
    current_port += 1
    netsh_content += generate_netsh('443', public_ip, current_ip, current_port)
    current_port += 1

    for group in config["groups"].values():
        for proxy in group["proxies"]:
            if proxy["dnat"]:
                current_ip = long2ip(ip2long(current_ip) + 1)
                for protocol in proxy["protocols"]:
                    netsh_content += generate_netsh(port(protocol), public_ip,
                                                    current_ip, current_port)
                    current_port += 1
    return netsh_content
Esempio n. 7
0
def generate(config):
    iptables_location = config["iptables_location"]
    public_ip = config["public_ip"]
    current_ip = config["base_ip"]
    current_port = config["base_port"]

    iptables_content = generate_iptables('80', public_ip, current_ip,
                                         current_port, iptables_location)
    current_port += 1
    iptables_content += generate_iptables('443', public_ip, current_ip,
                                          current_port, iptables_location)
    current_port += 1
    for group in config["groups"].values():
        for proxy in group["proxies"]:
            if proxy["dnat"]:
                current_ip = long2ip(ip2long(current_ip) + 1)
                for protocol in proxy["protocols"]:
                    iptables_content += generate_iptables(
                        port(protocol), public_ip, current_ip, current_port,
                        iptables_location)
                    current_port += 1
    return iptables_content
Esempio n. 8
0
	def port(self):
		return util.port(self.selfhost)
Esempio n. 9
0
def generate(config, dnat=False, test=True):
    bind_ip = config["bind_ip"]
    server_options = config["server_options"]
    if "base_port" in config:
        current_port = config["base_port"]
    elif dnat:
        return

    haproxy_content = generate_global()
    haproxy_content += generate_defaults()

    if not dnat:
        http_port = 80
        https_port = 443
    else:
        http_port = current_port
        https_port = current_port + 1

    haproxy_catchall_frontend_content = generate_frontend(
        'catchall', 'http', bind_ip, http_port, True)
    haproxy_catchall_backend_content = generate_backend(
        'catchall', 'http', None, None, None, True)

    haproxy_catchall_frontend_ssl_content = generate_frontend(
        'catchall', 'https', bind_ip, https_port, True)
    haproxy_catchall_backend_ssl_content = generate_backend(
        'catchall', 'https', None, None, None, True)

    if config["stats"]["enabled"]:
        haproxy_content += generate_stats(config["stats"], bind_ip)

    for group in config["groups"].values():
        for proxy in group["proxies"]:
            if not dnat or (dnat and proxy["dnat"]):
                for protocol in proxy["protocols"]:
                    if protocol == 'http':
                        haproxy_catchall_frontend_content += generate_frontend_catchall_entry(
                            proxy["domain"], protocol)
                        haproxy_catchall_backend_content += generate_backend_catchall_entry(
                            proxy["domain"], protocol, port(protocol),
                            server_options)
                    elif protocol == 'https':
                        haproxy_catchall_frontend_ssl_content += generate_frontend_catchall_entry(
                            proxy["domain"], protocol)
                        haproxy_catchall_backend_ssl_content += generate_backend_catchall_entry(
                            proxy["domain"], protocol, port(protocol),
                            server_options)
    if test:
        haproxy_catchall_frontend_content += generate_frontend_catchall_entry(
            'ptest.verdandi.is', 'http')
        haproxy_catchall_backend_content += generate_backend_catchall_entry(
            'ptest.verdandi.is', 'http', '80', server_options)

        haproxy_catchall_frontend_ssl_content += generate_frontend_catchall_entry(
            'ptest.verdandi.is', 'https')
        haproxy_catchall_backend_ssl_content += generate_backend_catchall_entry(
            'ptest.verdandi.is', 'https', '443', server_options)

    haproxy_content += haproxy_catchall_frontend_content + os.linesep
    haproxy_content += haproxy_catchall_backend_content
    haproxy_content += haproxy_catchall_frontend_ssl_content + os.linesep
    haproxy_content += haproxy_catchall_backend_ssl_content

    if dnat:
        current_port += 2
        for group in config["groups"].values():
            for proxy in group["proxies"]:
                if proxy["dnat"]:
                    for protocol in proxy["protocols"]:
                        haproxy_content += generate_frontend(
                            proxy["alias"], protocol, bind_ip, current_port,
                            False)
                        haproxy_content += generate_backend(
                            proxy["alias"], protocol, proxy["domain"],
                            port(protocol), server_options, False)
                        current_port += 1

    haproxy_content += generate_deadend('http')
    haproxy_content += generate_deadend('https')

    return haproxy_content
Esempio n. 10
0
    def __init__(self, **kwargs):
        Default.__init__(self, **kwargs)

        self.model = self.agent.model
        self.rlConfig = self.model.rlConfig

        if self.dump:
            try:
                import zmq
            except ImportError as err:
                print("ImportError: {0}".format(err))
                sys.exit("Install pyzmq to dump experiences")

            context = zmq.Context()

            self.socket = context.socket(zmq.PUSH)
            self.sock_addr = "tcp://%s:%d" % (self.dump,
                                              util.port(self.model.name))
            print("Connecting to " + self.sock_addr)
            self.socket.connect(self.sock_addr)

            self.dump_size = self.rlConfig.experience_length
            self.dump_state_actions = (self.dump_size *
                                       ssbm.SimpleStateAction)()

            self.dump_frame = 0
            self.dump_count = 0

        self.first_frame = True
        self.action_counter = 0
        self.toggle = False

        self.user = os.path.expanduser(self.user)

        self.state = ssbm.GameMemory()
        # track players 1 and 2 (pids 0 and 1)
        self.sm = state_manager.StateManager([0, 1])
        self.write_locations()

        if self.tag is not None:
            random.seed(self.tag)

        self.pids = [1]
        self.agents = {1: self.agent}
        self.characters = {1: self.agent.char or self.p2}

        reload_every = self.rlConfig.experience_length
        self.agent.reload_every = reload_every

        enemy = None
        if self.self_play:
            enemy = agent.Agent(reload_every=self.self_play * reload_every,
                                swap=True,
                                **kwargs)
        elif self.enemy:
            with open(self.enemy + 'agent', 'r') as f:
                import json
                enemy_kwargs = json.load(f)
            enemy_kwargs.update(reload_every=None,
                                swap=True,
                                dump=None,
                                path=self.enemy)
            enemy = agent.Agent(**enemy_kwargs)

        if enemy:
            self.pids.append(0)
            self.agents[0] = enemy
            self.characters[0] = enemy.char or self.p1

        self.menu_managers = {
            i: MenuManager(characters[c], pid=i)
            for i, c in self.characters.items()
        }

        print('Creating MemoryWatcher.')
        mwType = memory_watcher.MemoryWatcher
        if self.zmq:
            mwType = memory_watcher.MemoryWatcherZMQ
        self.mw = mwType(self.user + '/MemoryWatcher/MemoryWatcher')

        pipe_dir = self.user + '/Pipes/'
        print('Creating Pads at %s. Open dolphin now.' % pipe_dir)
        util.makedirs(self.user + '/Pipes/')

        paths = [pipe_dir + 'phillip%d' % i for i in self.pids]
        self.get_pads = util.async_map(Pad, paths)

        self.init_stats()

        # sets the game mode and random stage
        self.movie = movie.Movie(movie.endless_netplay +
                                 movie.stages[self.stage])
Esempio n. 11
0
def generate(config, dnat=False, test=True):
    bind_ip = config["bind_ip"]
    server_options = config["server_options"]
    if "base_port" in config:
        current_port = config["base_port"]
    elif dnat:
        return

    haproxy_content = generate_global()
    haproxy_content += generate_defaults()

    if not dnat:
        http_port = 80
        https_port = 443
    else:
        http_port = current_port
        https_port = current_port + 1

    haproxy_catchall_frontend_content = generate_frontend('catchall', 'http', bind_ip, http_port, True)
    haproxy_catchall_backend_content = generate_backend('catchall', 'http', None, None, None, True)

    haproxy_catchall_frontend_ssl_content = generate_frontend('catchall', 'https', bind_ip, https_port, True)
    haproxy_catchall_backend_ssl_content = generate_backend('catchall', 'https', None, None, None, True)

    if config["stats"]["enabled"]:
        haproxy_content += generate_stats(config["stats"], bind_ip)

    for group in config["groups"].values():
        for proxy in group["proxies"]:
            if not dnat or (dnat and not proxy["dnat"]):
                for protocol in proxy["protocols"]:
                    if protocol == 'http':
                        haproxy_catchall_frontend_content += generate_frontend_catchall_entry(proxy["domain"], protocol)
                        haproxy_catchall_backend_content += generate_backend_catchall_entry(proxy["domain"], protocol, port(protocol), server_options)
                    elif protocol == 'https':
                        haproxy_catchall_frontend_ssl_content += generate_frontend_catchall_entry(proxy["domain"], protocol)
                        haproxy_catchall_backend_ssl_content += generate_backend_catchall_entry(proxy["domain"], protocol, port(protocol), server_options)
    if test:
        haproxy_catchall_frontend_content += generate_frontend_catchall_entry('ptest.verdandi.is', 'http')
        haproxy_catchall_backend_content += generate_backend_catchall_entry('ptest.verdandi.is', 'http', '80', server_options)

        haproxy_catchall_frontend_ssl_content += generate_frontend_catchall_entry('ptest.verdandi.is', 'https')
        haproxy_catchall_backend_ssl_content += generate_backend_catchall_entry('ptest.verdandi.is', 'https', '443', server_options)

    haproxy_content += haproxy_catchall_frontend_content + os.linesep
    haproxy_content += haproxy_catchall_backend_content
    haproxy_content += haproxy_catchall_frontend_ssl_content + os.linesep
    haproxy_content += haproxy_catchall_backend_ssl_content

    if dnat:
        current_port += 2
        for group in config["groups"].values():
            for proxy in group["proxies"]:
                if proxy["dnat"]:
                    for protocol in proxy["protocols"]:
                        haproxy_content += generate_frontend(proxy["alias"], protocol, bind_ip, current_port, False)
                        haproxy_content += generate_backend(proxy["alias"], protocol, proxy["domain"], port(protocol), server_options, False)
                        current_port += 1

    haproxy_content += generate_deadend('http')
    haproxy_content += generate_deadend('https')

    return haproxy_content
Esempio n. 12
0
model = RL.Model(mode=RL.Mode.TRAIN, **args.__dict__)

# do this in RL?
if args.init:
    model.init()
    model.save()
else:
    model.restore()

import zmq

context = zmq.Context()

socket = context.socket(zmq.PULL)
sock_addr = "tcp://%s:%d" % (args.dump, util.port(model.name))
print("Binding to " + sock_addr)
socket.bind(sock_addr)

import numpy as np
from collections import defaultdict
from gc import get_objects


def count_objects():
    counts = defaultdict(int)
    for obj in get_objects():
        counts[type(obj)] += 1
    return counts