Linting
This commit is contained in:
parent
7abaa7bcd4
commit
ac249716f7
|
@ -1,5 +1,6 @@
|
|||
# Copyright (C) 2020 by eichhornchen, ÿnérant
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from datetime import datetime
|
||||
from random import randint, uniform
|
||||
from typing import Any, Tuple
|
||||
|
@ -61,7 +62,8 @@ class Squirrel(Hazelnut):
|
|||
|
||||
self.history = []
|
||||
self.received_messages = dict()
|
||||
self.recent_messages = dict() #of the form [Pkt(DataTLV), date of first reception, dict(neighbour, date of the next send, nb of times it has already been sent)]
|
||||
self.recent_messages = dict() # of the form [Pkt(DataTLV), date of first reception,
|
||||
# dict(neighbour, date of the next send, nb of times it has already been sent)]
|
||||
self.history_pad = curses.newpad(curses.LINES - 2, curses.COLS)
|
||||
self.input_pad = curses.newpad(1, curses.COLS)
|
||||
self.emoji_pad = curses.newpad(18, 12)
|
||||
|
@ -73,11 +75,10 @@ class Squirrel(Hazelnut):
|
|||
|
||||
# dictionnaries of neighbours
|
||||
self.potentialhazelnuts = dict()
|
||||
self.activehazelnuts = dict() #of the form [hazelnut, time of last
|
||||
#hello, time of last long hello, is symmetric]
|
||||
self.activehazelnuts = dict() # of the form [hazelnut, time of last hello,
|
||||
# time of last long hello, is symmetric]
|
||||
self.nbNS = 0
|
||||
self.minNS = 3 #minimal number of symmetric neighbours a squirrel needs
|
||||
#to have.
|
||||
self.minNS = 3 # minimal number of symmetric neighbours a squirrel needs to have.
|
||||
|
||||
self.add_system_message(f"Listening on {self.address}:{self.port}")
|
||||
self.add_system_message(f"I am {self.id}")
|
||||
|
@ -169,7 +170,7 @@ class Squirrel(Hazelnut):
|
|||
|
||||
self.handle_key_pressed(key)
|
||||
|
||||
def handle_key_pressed(self, key: str) -> None:
|
||||
def handle_key_pressed(self, key: str) -> None: # noqa: C901
|
||||
"""
|
||||
Process the key press from the user.
|
||||
"""
|
||||
|
@ -322,8 +323,8 @@ class Squirrel(Hazelnut):
|
|||
Takes the activehazels dictionnary and returns a list of [hazel, date+random, 0]
|
||||
"""
|
||||
res = dict()
|
||||
l = list(self.activehazelnuts.items())
|
||||
for key, hazel in l:
|
||||
hazels = list(self.activehazelnuts.items())
|
||||
for key, hazel in hazels:
|
||||
if hazel[3]: # Only if the neighbour is symmetric
|
||||
next_send = uniform(1, 2)
|
||||
res[key] = [hazel[0], time.time() + next_send, 0]
|
||||
|
@ -342,7 +343,7 @@ class Squirrel(Hazelnut):
|
|||
self.recent_messages.pop((sender_id, nonce), None)
|
||||
self.refresh_lock.release()
|
||||
|
||||
def clean_inundation(self):
|
||||
def clean_inundation(self) -> None:
|
||||
"""
|
||||
Remove messages which are overdue (older than 2 minutes) from the inundation dictionnary.
|
||||
"""
|
||||
|
@ -354,7 +355,7 @@ class Squirrel(Hazelnut):
|
|||
|
||||
self.refresh_lock.release()
|
||||
|
||||
def main_inundation(self):
|
||||
def main_inundation(self) -> None:
|
||||
"""
|
||||
The main inundation function.
|
||||
"""
|
||||
|
@ -378,8 +379,7 @@ class Squirrel(Hazelnut):
|
|||
if self.recent_messages[key][2][key2][2] >= 5: # the neighbour is not reactive enough
|
||||
gatlv = GoAwayTLV().construct(GoAwayType.TIMEOUT, f"{self.id} No acknowledge")
|
||||
pkt = Packet().construct(gatlv)
|
||||
self.send_packet(
|
||||
self.recent_messages[key][2][key2][0], pkt)
|
||||
self.send_packet(self.recent_messages[key][2][key2][0], pkt)
|
||||
self.activehazelnuts.pop(key2)
|
||||
self.potentialhazelnuts[key2] = self.recent_messages[key][2][key2][0]
|
||||
self.recent_messages[key][2].pop(key2)
|
||||
|
@ -630,7 +630,6 @@ class Squirrel(Hazelnut):
|
|||
|
||||
self.refresh_lock.release()
|
||||
|
||||
|
||||
def send_neighbours(self) -> None:
|
||||
"""
|
||||
Update the number of symmetric neighbours and
|
||||
|
@ -638,11 +637,12 @@ class Squirrel(Hazelnut):
|
|||
"""
|
||||
self.refresh_lock.acquire()
|
||||
|
||||
nbNS = 0
|
||||
#could send the same to all neighbour, but it means that neighbour A could receive a message with itself in it -> if the others do not pay attention, trouble
|
||||
nb_ns = 0
|
||||
# could send the same to all neighbour, but it means that neighbour
|
||||
# A could receive a message with itself in it -> if the others do not pay attention, trouble
|
||||
for key, hazelnut in self.activehazelnuts.items():
|
||||
if time.time() - hazelnut[2] <= 2 * 60:
|
||||
nbNS+=1
|
||||
nb_ns += 1
|
||||
self.activehazelnuts[key][3] = True
|
||||
ntlv = NeighbourTLV().construct(hazelnut[0].address, hazelnut[0].port)
|
||||
pkt = Packet().construct(ntlv)
|
||||
|
@ -651,10 +651,11 @@ class Squirrel(Hazelnut):
|
|||
self.send_packet(destination[0], pkt)
|
||||
else:
|
||||
self.activehazelnuts[key][3] = False
|
||||
self.nbNS = nbNS
|
||||
self.nbNS = nb_ns
|
||||
|
||||
self.refresh_lock.release()
|
||||
|
||||
|
||||
class Worm(Thread):
|
||||
"""
|
||||
The worm is the hazel listener.
|
||||
|
@ -678,6 +679,7 @@ class Worm(Thread):
|
|||
self.squirrel.refresh_history()
|
||||
self.squirrel.refresh_input()
|
||||
|
||||
|
||||
class HazelManager(Thread):
|
||||
"""
|
||||
A process to cleanly manage the squirrel's neighbours
|
||||
|
@ -718,6 +720,7 @@ class HazelManager(Thread):
|
|||
self.squirrel.send_neighbours()
|
||||
self.last_neighbour = time.time()
|
||||
|
||||
|
||||
class Inondator(Thread):
|
||||
"""
|
||||
A process to manage the inondation
|
||||
|
|
|
@ -123,13 +123,14 @@ class PadNTLV(TLV):
|
|||
squirrel.add_system_message(f"I received {self.length} zeros, am I so a bag guy ? :cold_sweat:")
|
||||
|
||||
@staticmethod
|
||||
def construct(length: int) -> "Pad1TLV":
|
||||
def construct(length: int) -> "PadNTLV":
|
||||
tlv = PadNTLV()
|
||||
tlv.type = 1
|
||||
tlv.length = length
|
||||
tlv.mbz = b'0' * length
|
||||
return tlv
|
||||
|
||||
|
||||
class HelloTLV(TLV):
|
||||
type: int = 2
|
||||
length: int
|
||||
|
@ -157,21 +158,20 @@ class HelloTLV(TLV):
|
|||
return data
|
||||
|
||||
def handle(self, squirrel: Any, sender: Any) -> None:
|
||||
timeH = time.time()
|
||||
time_h = time.time()
|
||||
if not squirrel.is_active(sender):
|
||||
sender.id = self.source_id # The sender we are given misses an id
|
||||
timeHL = time.time()
|
||||
time_hl = time.time()
|
||||
else:
|
||||
timeHL = squirrel.activehazelnuts[(sender.address, sender.port)][2]
|
||||
time_hl = squirrel.activehazelnuts[(sender.address, sender.port)][2]
|
||||
if self.is_long and self.dest_id == squirrel.id:
|
||||
timeHL = time.time()
|
||||
time_hl = time.time()
|
||||
|
||||
# Make sure the sender is not in the potential hazelnuts
|
||||
squirrel.remove_from_potential(sender.address, sender.port)
|
||||
|
||||
# Add entry to/actualize the active hazelnuts dictionnary
|
||||
squirrel.activehazelnuts[(sender.address, sender.port)] = [sender, timeH,\
|
||||
timeHL, True]
|
||||
squirrel.activehazelnuts[(sender.address, sender.port)] = [sender, time_h, time_hl, True]
|
||||
squirrel.nbNS += 1
|
||||
# squirrel.add_system_message(f"Aaaawwww, {self.source_id} spoke to me and said me Hello "
|
||||
# + ("long" if self.is_long else "short"))
|
||||
|
@ -214,8 +214,10 @@ class NeighbourTLV(TLV):
|
|||
self.port.to_bytes(2, sys.byteorder)
|
||||
|
||||
def handle(self, squirrel: Any, sender: Any) -> None:
|
||||
if not (str(self.ip_address),self.port) in squirrel.activehazelnuts and not (str(self.ip_address),self.port) in squirrel.potentialhazelnuts:
|
||||
squirrel.potentialhazelnuts[(str(self.ip_address), self.port)] = squirrel.new_hazel(str(self.ip_address), self.port)
|
||||
if not (str(self.ip_address), self.port) in squirrel.activehazelnuts \
|
||||
and not (str(self.ip_address), self.port) in squirrel.potentialhazelnuts:
|
||||
squirrel.potentialhazelnuts[(str(self.ip_address), self.port)] = \
|
||||
squirrel.new_hazel(str(self.ip_address), self.port)
|
||||
# squirrel.add_system_message(f"New potential friend {self.ip_address}:{self.port}!")
|
||||
|
||||
@staticmethod
|
||||
|
@ -227,6 +229,7 @@ class NeighbourTLV(TLV):
|
|||
tlv.port = port
|
||||
return tlv
|
||||
|
||||
|
||||
class DataTLV(TLV):
|
||||
type: int = 4
|
||||
length: int
|
||||
|
@ -331,14 +334,15 @@ class AckTLV(TLV):
|
|||
tlv.nonce = nonce
|
||||
return tlv
|
||||
|
||||
|
||||
class GoAwayType(Enum):
|
||||
UNKNOWN = 0
|
||||
EXIT = 1
|
||||
TIMEOUT = 2
|
||||
PROTOCOL_VIOLATION = 3
|
||||
|
||||
class GoAwayTLV(TLV):
|
||||
|
||||
class GoAwayTLV(TLV):
|
||||
type: int = 6
|
||||
length: int
|
||||
code: GoAwayType
|
||||
|
@ -363,14 +367,15 @@ class GoAwayTLV(TLV):
|
|||
squirrel.add_system_message("Some told me that he went away : " + self.message)
|
||||
|
||||
@staticmethod
|
||||
def construct(GAtype: GoAwayType, message: str) -> "GoAwayTLV":
|
||||
def construct(ga_type: GoAwayType, message: str) -> "GoAwayTLV":
|
||||
tlv = GoAwayTLV()
|
||||
tlv.type = 6
|
||||
tlv.code = GAtype
|
||||
tlv.code = ga_type
|
||||
tlv.message = message
|
||||
tlv.length = 1 + len(tlv.message)
|
||||
return tlv
|
||||
|
||||
|
||||
class WarningTLV(TLV):
|
||||
type: int = 7
|
||||
length: int
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import curses
|
||||
import time
|
||||
from argparse import ArgumentParser
|
||||
from typing import Any
|
||||
|
||||
|
@ -80,9 +79,9 @@ class Squinnondation:
|
|||
pkt = Packet().construct(htlv)
|
||||
squirrel.send_packet(hazelnut, pkt)
|
||||
|
||||
## if squirrel.port != 8082:
|
||||
## hazelnut = Hazelnut(address='::1', port=8082)
|
||||
## squirrel.potentialhazelnuts['::1',8082] = hazelnut
|
||||
# if squirrel.port != 8082:
|
||||
# hazelnut = Hazelnut(address='::1', port=8082)
|
||||
# squirrel.potentialhazelnuts['::1', 8082] = hazelnut
|
||||
|
||||
Worm(squirrel).start()
|
||||
HazelManager(squirrel).start()
|
||||
|
|
Loading…
Reference in New Issue