def main(num: int, file: str): grid = EnglandGrid.EnglandGrid(num) grid.load() city_it = city_iter(grid) CORE_PORT = 10000 SEARCH_PORT = 20000 DIRECTOR_PORT = 40000 config = {"nodes": [], "internal_hosts": {}} nodes = config["nodes"] internal_hosts = config["internal_hosts"] for i in range(num): entity = next(city_it) easting = entity.coords[0] northing = entity.coords[1] #EXTERNAL INFO entry = { "uri": "127.0.0.1:{}".format(SEARCH_PORT + i), "core_uri": "127.0.0.1:{}".format(CORE_PORT + i), "director_uri": "127.0.0.1:{}".format(DIRECTOR_PORT + i), "name": entity.name, "location": (easting, northing), "peers": [link[0].name for link in entity.links if link[1] == "GND"] } nodes.append(entry) # inside the container network we have domains for each container instead of 127.0.0.1 internal_hosts[entity.name] = entity.name + "-container" with open(file, "w") as f: json.dump(config, f)
async def set_locations_and_connections(director: Director, city_num: int): grid = EnglandGrid.EnglandGrid(city_num) grid.load() core_names = get_core_names(grid) peers = {} addresses = {} print(director.addresses) for key, entity in grid.entities.items(): if entity.kind != "CITY": continue name = next(core_names) a = director.get_address(name) if len(a) == 0: continue core_name = name + "-core" print("SET LOCATION {}@{}:{} NODE TO {}".format( core_name, a[0], a[1], entity.name)) addresses[core_name] = a peers[name] = [ link[0].name for link in entity.links if link[1] == "GND" ] await director.set_location(name, core_name.encode("utf-8"), (entity.coords[1], entity.coords[0]) ) #lon, lat for key in peers: targets = [("Search" + str(addresses[peer][1] - 40000), addresses[peer][0], addresses[peer][1] - 20000) for peer in peers[key] if peer in addresses] print("ADD PEERS for search {}: ".format(key), targets) await director.add_peers(key, targets)
def get_map_data(num: int): grid = EnglandGrid.EnglandGrid(num) grid.load() data = {} for key, entity in grid.entities.items(): if entity.kind != "CITY": continue peers = [link[0].name for link in entity.links if link[1] == "GND"] data[entity.name] = ((entity.coords[0], entity.coords[1]), peers) return data
def get_core_names(grid=None): if grid is None: grid = EnglandGrid.EnglandGrid() grid.load() core_names = [] for key, entity in grid.entities.items(): if entity.kind != "CITY": continue core_names.append((entity.name, entity.attributes["pop"])) core_names = map(lambda x: x[0], sorted(core_names, key=lambda x: x[1], reverse=True)) return core_names
def __init__(self): #self.world = World.World() self.grid = EnglandGrid.EnglandGrid() self.grid.load() oef_agent_factory = OefAgentFactory() search_network = SearchNetwork(oef_agent_factory.connection_factory, self.grid.entities) search_network.build_from_entities(self.grid.entities) self.agents = CrawlerAgents.CrawlerAgents(oef_agent_factory, self.grid) self.app = bottle.Bottle()
async def set_locations(director: Director): grid = EnglandGrid.EnglandGrid() grid.load() i = 0 names = director.get_node_names() for key, entity in grid.entities.items(): core_name = (entity.name + "-core").encode("UTF-8") await director.set_location(names[i], core_name, (entity.coords[1], entity.coords[0]) ) #lon, lat i += 1 if i >= len(names): break
async def set_locations(director: Director, city_num: int): grid = EnglandGrid.EnglandGrid(city_num) grid.load() core_names = get_core_names(grid) i = 0 names = director.get_node_names() for key, entity in grid.entities.items(): if entity.kind != "CITY": continue print("SET LOCATION {} NODE TO {}".format(names[i], entity.name)) core_name = next(core_names)+"-core" await director.set_location(names[i], core_name.encode("UTF-8"), (entity.coords[1], entity.coords[0])) #lon, lat i += 1 if i >= len(names): break
def run(self, args): self.grid = EnglandGrid.EnglandGrid() self.grid.load() self.server = bottle.Bottle() self.server.route('/', method='GET', callback=functools.partial(self.getRoot)) self.server.route('/svg', method='GET', callback=functools.partial(self.getSVG)) self.server.route('/pop', method='GET', callback=functools.partial(self.getPop)) self.server.route('/genpop', method='GET', callback=functools.partial(self.genPop)) self.server.run(host="0.0.0.0", port=args.http_port)