109 lines
3.1 KiB
Python
109 lines
3.1 KiB
Python
|
import time
|
||
|
|
||
|
|
||
|
class SuperviseurLocalBase():
|
||
|
"""Class de base pour le superviseur
|
||
|
"""
|
||
|
def __init__(self, client_CoAP) -> None:
|
||
|
self.client = client_CoAP
|
||
|
self._n_tokken = 0
|
||
|
self._n_envoie = 0
|
||
|
|
||
|
def envoie_message(self, message) -> None:
|
||
|
self.envoie_token(message.token)
|
||
|
|
||
|
def reception_message(self, message) -> None:
|
||
|
self.reception_token(message.token)
|
||
|
|
||
|
@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)
|
||
|
|
||
|
@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
|
||
|
|
||
|
|
||
|
class SuperviseurLocal(SuperviseurLocalBase):
|
||
|
"""
|
||
|
Class implementant la supervision local de chaque client.
|
||
|
"""
|
||
|
|
||
|
def __init__(self, client_CoAP) -> None:
|
||
|
super().__init__(client_CoAP)
|
||
|
self._dict_envoie = {}
|
||
|
self._dict_reception = {}
|
||
|
|
||
|
@property
|
||
|
def RTTs(self):
|
||
|
RTTs = []
|
||
|
for token in self._dict_reception:
|
||
|
if token in self._dict_envoie:
|
||
|
RTTs.append(
|
||
|
self._dict_reception[token][0] - self._dict_envoie[token][-1])
|
||
|
return RTTs
|
||
|
|
||
|
def envoie_token(self, token) -> None:
|
||
|
"""Enregistre l'envoie d'un token
|
||
|
|
||
|
Args:
|
||
|
token (int): Token à enregistrer
|
||
|
"""
|
||
|
self._n_envoie += 1
|
||
|
if token in self._dict_envoie:
|
||
|
self._dict_envoie[token].append(time.time())
|
||
|
else:
|
||
|
self._n_tokken += 1
|
||
|
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_reception:
|
||
|
self._dict_reception[token].append(time.time())
|
||
|
else:
|
||
|
self._dict_reception[token] = [time.time()]
|
||
|
|
||
|
|
||
|
class SuperviseurLocalFiltre(SuperviseurLocalBase):
|
||
|
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
|
||
|
self.RTTs = []
|
||
|
|
||
|
def envoie_token(self, token) -> None:
|
||
|
"""Enregistre l'envoie d'un token
|
||
|
|
||
|
Args:
|
||
|
token (int): Token à enregistrer
|
||
|
"""
|
||
|
self._n_envoie += 1
|
||
|
if token in self._dict_envoie:
|
||
|
self._dict_envoie[token].append(time.time())
|
||
|
else:
|
||
|
self._n_tokken += 1
|
||
|
self._dict_envoie[token] = [time.time()]
|
||
|
|
||
|
def reception_token(self, token) -> None:
|
||
|
if token in self._dict_envoie:
|
||
|
r = time.time() - self._dict_envoie[token][-1]
|
||
|
self.RTTs.append(r)
|
||
|
self.RTT_L = r*self.alpha_l + (1 - self.alpha_l) * self.RTT_L
|
||
|
self.RTT_S = r*self.alpha_s + (1 - self.alpha_s) * self.RTT_S
|
||
|
else:
|
||
|
raise ValueError
|