refactor mac similarity check into its own function

This commit is contained in:
Nils Schneider 2012-07-07 23:11:37 +02:00
parent f7f8225d1f
commit a8a9ed9c8d

View file

@ -64,24 +64,12 @@ class NodeDB:
# MACs
if 'gateway' in x and x['label'] == "TT":
try:
router = list(int(i, 16) for i in x['router'].split(":"))
gateway = list(int(i, 16) for i in x['gateway'].split(":"))
mac_a = list(int(i, 16) for i in x['router'].split(":"))
mac_b = list(int(i, 16) for i in x['gateway'].split(":"))
except ValueError:
continue
# first byte must only differ in bit 2
if router[0] == gateway[0] | 2:
# count different bytes
a = [x for x in zip(router[1:], gateway[1:]) if x[0] != x[1]]
# no more than two additional bytes must differ
if len(a) <= 2:
delta = 0
if len(a) > 0:
delta = sum(abs(i[0] -i[1]) for i in a)
if delta < 8:
# This TT link looks like a mac of the router!
if is_similar(mac_a, mac_b):
node.add_mac(x['gateway'])
# skip processing as regular link
@ -292,3 +280,23 @@ class NodeDB:
if data[2]:
node.name = data[2]
# compares two MACs and decides whether they are
# similar and could be from the same node
def is_similar(a, b):
# first byte must only differ in bit 2
if a[0] | 2 == b[0] | 2:
# count different bytes
a = [x for x in zip(a[1:], b[1:]) if x[0] != x[1]]
else:
return False
# no more than two additional bytes must differ
if len(a) <= 2:
delta = 0
if len(a) > 0:
delta = sum(abs(i[0] -i[1]) for i in a)
# This TT link looks like a mac of the router!
return delta < 8