def __init__(self, conf_path): self.hardcoded_ips = set() self._stable_ip_port = {} self._unstable_ip_port = {} self._all_subnets = set() self._local_exists = False self._sample_unstable_addrs = [] self.abs_local_filename = os.path.join(conf_path, LOCAL_UNSTABLE_FILENAME) filename = HARDCODED_STABLE_FILENAME f = utils.get_open_file(filename) for line in f or []: addr = _sanitize_bootstrap_addr(line) self.hardcoded_ips.add(addr[0]) self._stable_ip_port[addr[0]] = addr[1] self._all_subnets.add(utils.get_subnet(addr)) logger.debug( '%s: %d hardcoded, %d stable' % (filename, len(self.hardcoded_ips), len(self._stable_ip_port))) # local (unstable) try: f = open(self.abs_local_filename) except: logger.debug("File does not exist") local_exists = False else: #TODO: use unstable if too few addrs in local? I don't think so local_exists = True for line in f: addr = _sanitize_bootstrap_addr(line) self._unstable_ip_port[addr[0]] = addr[1] self._all_subnets.add(utils.get_subnet(addr)) logger.debug( '%s: %d hardcoded, %d unstable' % (filename, len(self.hardcoded_ips), len(self._unstable_ip_port))) filename = HARDCODED_UNSTABLE_FILENAME f = utils.get_open_file(filename) for line in f or []: addr = _sanitize_bootstrap_addr(line) self.hardcoded_ips.add(addr[0]) if not local_exists: self._unstable_ip_port[addr[0]] = addr[1] self._all_subnets.add(utils.get_subnet(addr)) logger.debug( '%s: %d hardcoded, %d unstable' % (filename, len(self.hardcoded_ips), len(self._unstable_ip_port))) #long-term variables self.next_long_uptime_add_ts = time.time() # do first add asap self.longest_uptime = MIN_LONG_UPTIME self.longest_uptime_addr = None
def __init__(self, conf_path): self.hardcoded_ips = set() self._stable_ip_port = {} self._unstable_ip_port = {} self._all_subnets = set() self._local_exists = False self._sample_unstable_addrs = [] self.abs_local_filename = os.path.join(conf_path, LOCAL_UNSTABLE_FILENAME) filename = HARDCODED_STABLE_FILENAME f = utils.get_open_file(filename) for line in f or []: addr = _sanitize_bootstrap_addr(line) self.hardcoded_ips.add(addr[0]) self._stable_ip_port[addr[0]] = addr[1] self._all_subnets.add(utils.get_subnet(addr)) logger.debug('%s: %d hardcoded, %d stable' % ( filename, len(self.hardcoded_ips), len(self._stable_ip_port))) # local (unstable) try: f = open(self.abs_local_filename) except: logger.debug("File does not exist") local_exists = False else: #TODO: use unstable if too few addrs in local? I don't think so local_exists = True for line in f: addr = _sanitize_bootstrap_addr(line) self._unstable_ip_port[addr[0]] = addr[1] self._all_subnets.add(utils.get_subnet(addr)) logger.debug('%s: %d hardcoded, %d unstable' % ( filename, len(self.hardcoded_ips), len(self._unstable_ip_port))) filename = HARDCODED_UNSTABLE_FILENAME f = utils.get_open_file(filename) for line in f or []: addr = _sanitize_bootstrap_addr(line) self.hardcoded_ips.add(addr[0]) if not local_exists: self._unstable_ip_port[addr[0]] = addr[1] self._all_subnets.add(utils.get_subnet(addr)) logger.debug('%s: %d hardcoded, %d unstable' % ( filename, len(self.hardcoded_ips), len(self._unstable_ip_port))) #long-term variables self.next_long_uptime_add_ts = time.time() # do first add asap self.longest_uptime = MIN_LONG_UPTIME self.longest_uptime_addr = None
def report_unreachable(self, addr): """ Use only during overlay bootstrap """ if addr[0] not in self._unstable_ip_port: # Addr not present in a bootstrap list. Ignore. return # Reported addrs will be deleted from UNSTABLE list. We consider the # case of a temporaly off-line node that incorrectly report nodes as # unreachable, when in reality the reported node may be reachable. This # case is common in Android (battery-saving settings shut off radio to # save battery). # The idea is that if addrs are reported as unreachable in the same order # as pinged (i.e. same as self._sample_unstable_addrs), we assume the # local node is off-line and do not remove the addr from UNSTABLE. # To make it simpler, we don't allow creating multiple samples (see # get_sample_unstable_addrs). if self._sample_unstable_addrs: if addr == self._sample_unstable_addrs.pop(0): # assume local node is off-line, do not remove logger.debug('OFF-LINE %r' % (addr,)) return else: self._sample_unstable_addrs = [] # end off-line mode #remove from dict (if present) del self._unstable_ip_port[addr[0]] self._all_subnets.remove(utils.get_subnet(addr)) logger.debug('REMOVED %r' % (addr,))
def report_unreachable(self, addr): """ Use only during overlay bootstrap """ if addr[0] not in self._unstable_ip_port: # Addr not present in a bootstrap list. Ignore. return # Reported addrs will be deleted from UNSTABLE list. We consider the # case of a temporaly off-line node that incorrectly report nodes as # unreachable, when in reality the reported node may be reachable. This # case is common in Android (battery-saving settings shut off radio to # save battery). # The idea is that if addrs are reported as unreachable in the same order # as pinged (i.e. same as self._sample_unstable_addrs), we assume the # local node is off-line and do not remove the addr from UNSTABLE. # To make it simpler, we don't allow creating multiple samples (see # get_sample_unstable_addrs). if self._sample_unstable_addrs: if addr == self._sample_unstable_addrs.pop(0): # assume local node is off-line, do not remove logger.debug('OFF-LINE %r' % (addr, )) return else: self._sample_unstable_addrs = [] # end off-line mode #remove from dict (if present) del self._unstable_ip_port[addr[0]] self._all_subnets.remove(utils.get_subnet(addr)) logger.debug('REMOVED %r' % (addr, ))
def report_reachable(self, addr, uptime=0): """ - uptime == 0: This node has been discovered during overlay boostrap. It will be added to the UNSTABLE list if there is enough room. **Use only from lookup manager (overlay bootstrap lookup). - uptime > 0: This node has been in the routing table for some time (uptime seconds). Once in a while, a long-term reachable node is written to the UNSTABLE file. **Use only from routing table manager. """ addr_subnet = utils.get_subnet(addr) if len(self._unstable_ip_port) >= MAX_LONG_UPTIME_ADDRS: # Enough addrs in the list. Ignore. return if addr_subnet in self._all_subnets: # Subnet already in a bootstrap list. Ignore. return if uptime == 0: if len(self._unstable_ip_port) < MAX_ZERO_UPTIME_ADDRS: logger.debug('added short %r' % (addr,)) self._unstable_ip_port[addr[0]] = addr[1] self._all_subnets.add(addr_subnet) elif uptime >= self.longest_uptime: assert uptime >= MIN_LONG_UPTIME self.longest_uptime = uptime self.longest_uptime_addr = addr if time.time() >= self.next_long_uptime_add_ts: logger.debug('added long: %r, %f hours' % ( addr, uptime / 3600)) self._unstable_ip_port[addr[0]] = addr[1] self._all_subnets.add(addr_subnet) self.longest_uptime = MIN_LONG_UPTIME self.next_long_uptime_add_ts += ADD_LONG_UPTIME_ADDR_EACH assert self.longest_uptime_addr self.longest_uptime_addr = None self.save_to_file()
def report_reachable(self, addr, uptime=0): """ - uptime == 0: This node has been discovered during overlay boostrap. It will be added to the UNSTABLE list if there is enough room. **Use only from lookup manager (overlay bootstrap lookup). - uptime > 0: This node has been in the routing table for some time (uptime seconds). Once in a while, a long-term reachable node is written to the UNSTABLE file. **Use only from routing table manager. """ addr_subnet = utils.get_subnet(addr) if len(self._unstable_ip_port) >= MAX_LONG_UPTIME_ADDRS: # Enough addrs in the list. Ignore. return if addr_subnet in self._all_subnets: # Subnet already in a bootstrap list. Ignore. return if uptime == 0: if len(self._unstable_ip_port) < MAX_ZERO_UPTIME_ADDRS: logger.debug('added short %r' % (addr, )) self._unstable_ip_port[addr[0]] = addr[1] self._all_subnets.add(addr_subnet) elif uptime >= self.longest_uptime: assert uptime >= MIN_LONG_UPTIME self.longest_uptime = uptime self.longest_uptime_addr = addr if time.time() >= self.next_long_uptime_add_ts: logger.debug('added long: %r, %f hours' % (addr, uptime / 3600)) self._unstable_ip_port[addr[0]] = addr[1] self._all_subnets.add(addr_subnet) self.longest_uptime = MIN_LONG_UPTIME self.next_long_uptime_add_ts += ADD_LONG_UPTIME_ADDR_EACH assert self.longest_uptime_addr self.longest_uptime_addr = None self.save_to_file()
def subnet_network(request): if request.method == 'POST': form = SubnetCreateFrom(request.POST) if form.is_valid(): subnet = form.cleaned_data['Subnet_Address'] ip = form.cleaned_data['IP_Address'] user_host = get_hosts(user=request.user) hosts_list = get_subnet(user_host, subnet,ip) subnet_network = form.save() network_obj = Network.objects.get(name__exact = form.cleaned_data['name']) for hosts in hosts_list: network_entry = NetworkHost(network_id = network_obj.id, host_id = hosts.id) network_entry.save() extra_context = { 'form': SubnetCreateFrom(initial={'user': request.user.pk}), 'host_list': hosts_list } return redirect_to(request, url=subnet_network.get_absolute_url()) else: form = SubnetCreateFrom() extra_context = { 'form':SubnetCreateFrom(initial={'user': request.user.pk}) } return direct_to_template(request,'networks/subnet_form.html',extra_context)