108 lines
2.9 KiB
Python
108 lines
2.9 KiB
Python
import time
|
|
|
|
|
|
class SuperviseurLocalPlaceHolder():
|
|
"""Class de base pour le superviseur
|
|
"""
|
|
|
|
def __init__(self, client_CoAP) -> None:
|
|
self.client = client_CoAP
|
|
self._RTTs = []
|
|
self._taux_retransmition = 0
|
|
|
|
def envoie_message(self, message) -> None:
|
|
self.envoie_token(message.token)
|
|
|
|
def reception_message(self, message) -> None:
|
|
self.reception_token(message.token)
|
|
|
|
def envoie_token(self, token) -> None:
|
|
pass
|
|
|
|
def reception_token(self, tokken) -> None:
|
|
pass
|
|
|
|
@property
|
|
def RTTs(self):
|
|
return self._RTTs
|
|
|
|
@property
|
|
def taux_retransmition(self):
|
|
return self._taux_retransmition
|
|
|
|
@property
|
|
def min_RTT(self):
|
|
"""Valeur minimum du RTT"""
|
|
return min(self.RTTs)
|
|
|
|
@property
|
|
def avg_RTT(self):
|
|
"""Moyenne du RTT."""
|
|
return sum(self.RTTs)/len(self.RTTs)
|
|
|
|
|
|
class SuperviseurLocal(SuperviseurLocalPlaceHolder):
|
|
"""
|
|
Class implementant la supervision local de chaque client.
|
|
"""
|
|
|
|
def __init__(self, client_CoAP) -> None:
|
|
super().__init__(client_CoAP)
|
|
self._dict_envoie = {}
|
|
self._n_envoie = 0
|
|
self._n_tokken = 0
|
|
|
|
def envoie_token(self, token) -> None:
|
|
"""Enregistre l'envoie d'un token
|
|
|
|
Args:
|
|
token (int): Token à enregistrer
|
|
"""
|
|
self._n_envoie += 1
|
|
self._n_tokken += not(token in self._dict_envoie)
|
|
self._dict_envoie[token] = time.time()
|
|
|
|
def reception_token(self, token) -> None:
|
|
"""Enregistre l'arrivée d'un token
|
|
|
|
Args:
|
|
token (int): Token à enregister
|
|
"""
|
|
if token in self._dict_envoie:
|
|
rtt = time.time() - self._dict_envoie[token]
|
|
self._RTTs.append(time.time() - self._dict_envoie[token])
|
|
del self._dict_envoie[token]
|
|
self.callback_new_rtt(rtt)
|
|
else:
|
|
pass #raise ValueError("Tokken inconnue")
|
|
|
|
@property
|
|
def tau_retransmission(self):
|
|
"""Rapport du nombre de message correspondant à des restransmission sur le nombre de message total."""
|
|
return 1 - self._n_tokken/self._n_envoie
|
|
|
|
def callback_new_rtt(self, rtt):
|
|
pass
|
|
|
|
|
|
class SuperviseurLocalFiltre(SuperviseurLocal):
|
|
def __init__(self, client_CoAP, rtt_init=0.001, alpha_l=0.01, alpha_s=0.1) -> None:
|
|
super().__init__(client_CoAP)
|
|
self._dict_envoie = {}
|
|
self.alpha_l = alpha_l
|
|
self.alpha_s = alpha_s
|
|
self._RTT_L = rtt_init
|
|
self._RTT_S = rtt_init
|
|
|
|
def callback_new_rtt(self, rtt):
|
|
self._RTT_L = rtt*self.alpha_l + (1 - self.alpha_l) * self._RTT_L
|
|
self._RTT_S = rtt*self.alpha_s + (1 - self.alpha_s) * self._RTT_S
|
|
return super().callback_new_rtt(rtt)
|
|
|
|
@property
|
|
def RTT_L(self):
|
|
return self._RTT_L
|
|
|
|
@property
|
|
def RTT_S(self):
|
|
return self._RTT_S
|