Skip to content

Commit

Permalink
Refactored ARP Cache code
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Sep 15, 2024
1 parent 8820a59 commit 3759ff4
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions pytcp/stack/arp_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

from __future__ import annotations

from dataclasses import dataclass, field
import threading
import time

Expand All @@ -43,27 +44,28 @@
from pytcp.lib.logger import log


class ArpCache:
@dataclass
class CacheEntry:
"""
Support for ARP Cache operations.
Container class for cache entries.
"""

class CacheEntry:
"""
Container class for cache entries.
"""
mac_address: MacAddress
permanent: bool = False
create_time: int = field(
init=False,
default_factory=lambda: int(time.time()),
)
hit_count: int = 0

def __init__(
self, mac_address: MacAddress, permanent: bool = False
) -> None:
"""
Class constructor.
"""

self.mac_address: MacAddress = mac_address
self.permanent: bool = permanent
self.creation_time: float = time.time()
self.hit_count: int = 0
class ArpCache:
"""
Support for ARP Cache operations.
"""

_arp_cache: dict[Ip4Address, CacheEntry]
_run_thread: bool

def __init__(self) -> None:
"""
Expand All @@ -72,8 +74,15 @@ def __init__(self) -> None:

__debug__ and log("stack", "Initializing ARP Cache")

self._arp_cache: dict[Ip4Address, ArpCache.CacheEntry] = {}
self._run_thread: bool = False
self._arp_cache = {}
self._run_thread = False

def __repr__(self) -> str:
"""
Return string representation of the ARP Cache.
"""

return repr(self._arp_cache)

def start(self) -> None:
"""
Expand Down Expand Up @@ -113,7 +122,7 @@ def _thread__arp_cache__maintain_entries(self) -> None:

# If entry age is over maximum age then discard the entry
if (
time.time() - self._arp_cache[ip4_address].creation_time
int(time.time()) - self._arp_cache[ip4_address].create_time
> stack.ARP__CACHE__ENTRY_MAX_AGE
):
mac_address = self._arp_cache.pop(ip4_address).mac_address
Expand All @@ -127,7 +136,7 @@ def _thread__arp_cache__maintain_entries(self) -> None:
# used since last refresh then send out request in attempt
# to refresh it.
elif (
time.time() - self._arp_cache[ip4_address].creation_time
int(time.time()) - self._arp_cache[ip4_address].create_time
> stack.ARP__CACHE__ENTRY_MAX_AGE
- stack.ARP__CACHE__ENTRY_REFRESH_TIME
) and self._arp_cache[ip4_address].hit_count:
Expand Down Expand Up @@ -161,7 +170,7 @@ def add_entry(
f"{mac_address}</>",
)

self._arp_cache[ip4_address] = self.CacheEntry(mac_address)
self._arp_cache[ip4_address] = CacheEntry(mac_address)

def find_entry(self, *, ip4_address: Ip4Address) -> MacAddress | None:
"""
Expand All @@ -173,7 +182,7 @@ def find_entry(self, *, ip4_address: Ip4Address) -> MacAddress | None:
__debug__ and log(
"arp-c",
f"Found {ip4_address} -> {arp_entry.mac_address} entry, "
f"age {time.time() - arp_entry.creation_time:.0f}s, "
f"age {int(time.time()) - arp_entry.create_time}s, "
f"hit_count {arp_entry.hit_count}",
)
return arp_entry.mac_address
Expand Down

0 comments on commit 3759ff4

Please sign in to comment.