Skip to content

Commit 34a182c

Browse files
authored
Update processor.py
nodes with the same geographical location are now evenly distributed in the circumference (conglomeration of nodes are now avoided).
1 parent 736d5e2 commit 34a182c

File tree

1 file changed

+53
-14
lines changed

1 file changed

+53
-14
lines changed

IPRadar2/processor.py

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@
4040
import os
4141
from getmac import get_mac_address
4242
import time
43-
# from memory_profiler import profile # and use @profile in functions that may introduce memory leaks
43+
# to use the memory profiler add:
44+
# from memory_profiler import profile
45+
# and use the decorator @profile in functions that may introduce memory leaks
46+
# you can also add this parameter to your configuration: -m memory_profiler
4447

4548

4649

@@ -90,6 +93,11 @@ def __init__(self, locations, min_length_km=1000, segment_length_km=500, **kwarg
9093
super().__init__(locations, **kwargs)
9194

9295

96+
# helper function
97+
def is_power_of_two(n):
98+
return (n & (n - 1)) == 0 and n != 0
99+
100+
93101
class ProcessorClass(object):
94102
# TODO: check workaround using sanitized_ip[]
95103
sanitized_ip = []
@@ -104,6 +112,8 @@ class ProcessorClass(object):
104112
sizeOfProcessingQueue = 0
105113
node_dict = {}
106114
location_dict = {}
115+
offset_dict = {}
116+
curr_offset_degrees = {}
107117
node_dict_gui = {} # current dict of new/modified nodes to be shown/updated in GUI
108118
__mutex = Lock() # for processing or accessing node_dict_gui[]
109119
__mutex_question = Lock()
@@ -1493,9 +1503,9 @@ def processPacket(self, packet):
14931503
mac_dst = ""
14941504
# create nodes
14951505
# cases: src dst (exist)
1496-
# 1.a x x
1497-
# 1.b x
1498-
# 2.a x
1506+
# 1.a x x
1507+
# 1.b x
1508+
# 2.a x
14991509
# 2.b
15001510
################
15011511
case = ""
@@ -1525,7 +1535,8 @@ def processPacket(self, packet):
15251535
# add source in node_dict
15261536
source_node = NodeDataClass(self.currentNodeNumber, source, mac_src, response_src.latitude,
15271537
response_src.longitude, response_src.latitude, response_src.longitude, 1,
1528-
response_src.country, pycountry.countries.get(alpha_2=response_src.country),
1538+
response_src.country,
1539+
pycountry.countries.get(alpha_2=response_src.country),
15291540
response_src.region, response_src.city, host_src, True, "",
15301541
host_src_resolved, ping=False, bad=False, killed=False, killed_process="",
15311542
local=src_is_local, conn_established=False,
@@ -1559,40 +1570,68 @@ def processPacket(self, packet):
15591570
if latlonsrc in self.location_dict:
15601571
if case == "2a" or case == "2b":
15611572
# increment count
1562-
self.location_dict[latlonsrc] = self.location_dict[latlonsrc] + 1 # updates value
1573+
self.location_dict[latlonsrc] = self.location_dict[latlonsrc] + 1
15631574
# update also the source position in node_dict
15641575
self.node_dict[source].position = self.location_dict[latlonsrc]
15651576
# and update the drawing position
1566-
# GeoLocationPhi = math.radians(360.0/self.node_dict[source].position)
1567-
GeoLocationPhi = math.radians(6.28 * 360.0 / self.node_dict[source].position)
1577+
GeoLocationPhi = math.radians(self.offset_dict[latlonsrc])
15681578
# set delta to CIRCLE in geo-location
15691579
latDelta = configuration.GeoLocationRadius * math.cos(GeoLocationPhi)
15701580
lonDelta = configuration.GeoLocationRadius * math.sin(GeoLocationPhi)
15711581
self.node_dict[source].lat_plot = self.node_dict[source].lat + latDelta
15721582
self.node_dict[source].lon_plot = self.node_dict[source].lon + lonDelta
1583+
# update offset to evenly distribute nodes in a circle
1584+
if self.node_dict[source].position % 4 == 0:
1585+
if is_power_of_two(self.node_dict[source].position):
1586+
self.offset_dict[latlonsrc] = (
1587+
self.offset_dict[latlonsrc] + 180.0 / self.node_dict[source].position) % 360.0
1588+
self.curr_offset_degrees[latlonsrc] = 180.0 / self.node_dict[source].position
1589+
else:
1590+
self.offset_dict[latlonsrc] = (self.offset_dict[latlonsrc] + self.curr_offset_degrees[
1591+
latlonsrc] * 2.0) % 360.0
1592+
elif self.node_dict[source].position % 2 == 0:
1593+
self.offset_dict[latlonsrc] = (self.offset_dict[latlonsrc] + 90.0) % 360.0
1594+
else:
1595+
self.offset_dict[latlonsrc] = (self.offset_dict[latlonsrc] + 180.0) % 360.0
15731596
else: # it must be case 2a or 2b
15741597
# add NEW location
1575-
self.location_dict[latlonsrc] = 1 # new value 1 in dict with key latlonsrc (its like an "append")
1598+
self.location_dict[latlonsrc] = 0 # new value 1 in dict with key latlonsrc (its like an "append")
1599+
self.offset_dict[latlonsrc] = 0.0
1600+
self.curr_offset_degrees[latlonsrc] = 90.0
15761601
# NOTE: self.node_dict[source].position already set to 1 by default
15771602
# update module variable location_dict (dst)
15781603
latlondst = "".join([str(self.node_dict[destination].lat), ",", str(self.node_dict[destination].lon)])
15791604
if latlondst in self.location_dict:
15801605
if case == "1b" or case == "2b":
15811606
# increment count
1582-
self.location_dict[latlondst] = self.location_dict[latlondst] + 1 # updates value
1583-
# update also the source position in node_dict
1607+
self.location_dict[latlondst] = self.location_dict[latlondst] + 1
1608+
# update also the destination position in node_dict
15841609
self.node_dict[destination].position = self.location_dict[latlondst]
15851610
# and update the drawing position
1586-
# GeoLocationPhi = math.radians(360.0/self.node_dict[destination].position)
1587-
GeoLocationPhi = math.radians(6.28 * 360.0 / self.node_dict[destination].position)
1611+
GeoLocationPhi = math.radians(self.offset_dict[latlondst])
15881612
# set delta to CIRCLE in geo-location
15891613
latDelta = configuration.GeoLocationRadius * math.cos(GeoLocationPhi)
15901614
lonDelta = configuration.GeoLocationRadius * math.sin(GeoLocationPhi)
15911615
self.node_dict[destination].lat_plot = self.node_dict[destination].lat + latDelta
15921616
self.node_dict[destination].lon_plot = self.node_dict[destination].lon + lonDelta
1617+
# update offset to evenly distribute nodes in a circle
1618+
if self.node_dict[destination].position % 4 == 0:
1619+
if is_power_of_two(self.node_dict[destination].position):
1620+
self.offset_dict[latlondst] = (
1621+
self.offset_dict[latlondst] + 180.0 / self.node_dict[destination].position)% 360.0
1622+
self.curr_offset_degrees[latlondst] = 180.0 / self.node_dict[destination].position
1623+
else:
1624+
self.offset_dict[latlondst] = (self.offset_dict[latlondst] + self.curr_offset_degrees[
1625+
latlondst] * 2.0) % 360.0
1626+
elif self.node_dict[destination].position % 2 == 0:
1627+
self.offset_dict[latlondst] = (self.offset_dict[latlondst] + 90.0) % 360.0
1628+
else:
1629+
self.offset_dict[latlondst] = (self.offset_dict[latlondst] + 180.0) % 360.0
15931630
else: # it must be case 1b or 2b
15941631
# add NEW location
1595-
self.location_dict[latlondst] = 1 # new value (its like an "append")
1632+
self.location_dict[latlondst] = 0 # new value (its like an "append")
1633+
self.offset_dict[latlondst] = 0.0
1634+
self.curr_offset_degrees[latlondst] = 90.0
15961635
# NOTE: self.node_dict[destination].position already set to 1 by default
15971636
# src in black list / NOT in white list?
15981637
if configuration.USE_WHITE_LIST:

0 commit comments

Comments
 (0)