Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Sep 8, 2024
1 parent 7486798 commit 1b07754
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 55 deletions.
69 changes: 42 additions & 27 deletions examples/icmp_echo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import threading
import time
from datetime import datetime
from typing import cast

import click

Expand All @@ -57,6 +58,7 @@
from pytcp.lib.net_addr.ip6_host import Ip6Host
from pytcp.lib.net_addr.ip_address import IpAddress
from pytcp.lib.net_addr.mac_address import MacAddress
from pytcp.lib.stack import github_repository, version_string
from pytcp.protocols.icmp4.message.icmp4_message__echo_request import (
Icmp4EchoRequestMessage,
)
Expand Down Expand Up @@ -114,33 +116,46 @@ def __thread__client(self) -> None:

message_seq = 0
while self._run_thread and message_count:
message = bytes(str(datetime.now()) + "\n", "utf-8")

if self._local_ip_address.version == 4:
assert isinstance(self._local_ip_address, Ip4Address)
assert isinstance(self._remote_ip_address, Ip4Address)
stack.packet_handler.send_icmp4_packet(
ip4__local_address=self._local_ip_address,
ip4__remote_address=self._remote_ip_address,
icmp4__message=Icmp4EchoRequestMessage(
id=flow_id,
seq=message_seq,
data=message,
),
)

if self._local_ip_address.version == 6:
assert isinstance(self._local_ip_address, Ip6Address)
assert isinstance(self._remote_ip_address, Ip6Address)
stack.packet_handler.send_icmp6_packet(
ip6__local_address=self._local_ip_address,
ip6__remote_address=self._remote_ip_address,
icmp6__message=Icmp6EchoRequestMessage(
id=flow_id,
seq=message_seq,
data=message,
),
)
message = bytes(
f"PyTCP {version_string}, {github_repository} - {str(datetime.now())}",
"utf-8",
)

match self._local_ip_address.version, self._remote_ip_address.version:
case 4, 4:
stack.packet_handler.send_icmp4_packet(
ip4__local_address=cast(
Ip4Address, self._local_ip_address
),
ip4__remote_address=cast(
Ip4Address, self._remote_ip_address
),
icmp4__message=Icmp4EchoRequestMessage(
id=flow_id,
seq=message_seq,
data=message,
),
)
case 6, 6:
stack.packet_handler.send_icmp6_packet(
ip6__local_address=cast(
Ip6Address, self._local_ip_address
),
ip6__remote_address=cast(
Ip6Address, self._remote_ip_address
),
icmp6__message=Icmp6EchoRequestMessage(
id=flow_id,
seq=message_seq,
data=message,
),
)
case _:
raise ValueError(
"Unsupported IP version combination: "
f"{self._local_ip_address.version=}, "
f"{self._remote_ip_address.version=}"
)

click.echo(
f"Client ICMP Echo: Sent ICMP Echo ({flow_id}/{message_seq}) "
Expand Down
14 changes: 6 additions & 8 deletions examples/lib/tcp_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import threading
import time
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING

import click
Expand All @@ -48,7 +49,7 @@
from pytcp.lib.socket import Socket


class TcpService:
class TcpService(ABC):
"""
TCP service support class.
"""
Expand Down Expand Up @@ -137,15 +138,12 @@ def __thread__service__connection_handler(
Inbound connection handler.
"""

self.service(connected_socket=connected_socket)
self._service(connected_socket=connected_socket)

def service(self, *, connected_socket: Socket) -> None:
@abstractmethod
def _service(self, *, connected_socket: Socket) -> None:
"""
Service method.
"""

click.echo(
f"Service TCP {self._service_name}: No service method defined, "
"closing connection."
)
connected_socket.close()
raise NotImplementedError
14 changes: 6 additions & 8 deletions examples/lib/udp_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import threading
import time
from abc import ABC, abstractmethod
from typing import TYPE_CHECKING

import click
Expand All @@ -48,7 +49,7 @@
from pytcp.lib.socket import Socket


class UdpService:
class UdpService(ABC):
"""
UDP service support class.
"""
Expand Down Expand Up @@ -113,9 +114,10 @@ def __thread__service(self) -> None:
)
return

self.service(listening_socket=listening_socket)
self._service(listening_socket=listening_socket)

def service(
@abstractmethod
def _service(
self,
*,
listening_socket: Socket,
Expand All @@ -124,8 +126,4 @@ def service(
Service method.
"""

click.echo(
f"Service UDP {self._service_name}: No service method defined, "
"closing connection."
)
listening_socket.close()
raise NotImplementedError
5 changes: 3 additions & 2 deletions examples/tcp_daytime_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

import time
from datetime import datetime
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, override

import click

Expand Down Expand Up @@ -87,7 +87,8 @@ def __init__(
self._message_count = message_count
self._message_delay = message_delay

def service(self, *, connected_socket: Socket) -> None:
@override
def _service(self, *, connected_socket: Socket) -> None:
"""
Inbound connection handler.
"""
Expand Down
5 changes: 3 additions & 2 deletions examples/tcp_discard_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from __future__ import annotations

import time
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, override

import click

Expand Down Expand Up @@ -75,7 +75,8 @@ def __init__(self, *, local_ip_address: IpAddress, local_port: int):
local_port=local_port,
)

def service(self, *, connected_socket: Socket) -> None:
@override
def _service(self, *, connected_socket: Socket) -> None:
"""
Inbound connection handler.
"""
Expand Down
5 changes: 3 additions & 2 deletions examples/tcp_echo_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from __future__ import annotations

import time
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, override

import click

Expand Down Expand Up @@ -77,7 +77,8 @@ def __init__(self, *, local_ip_address: IpAddress, local_port: int):
local_port=local_port,
)

def service(self, *, connected_socket: Socket) -> None:
@override
def _service(self, *, connected_socket: Socket) -> None:
"""
Inbound connection handler.
"""
Expand Down
5 changes: 3 additions & 2 deletions examples/udp_daytime_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

import time
from datetime import datetime
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, override

import click

Expand Down Expand Up @@ -77,7 +77,8 @@ def __init__(self, *, local_ip_address: IpAddress, local_port: int):
local_port=local_port,
)

def service(self, *, listening_socket: Socket) -> None:
@override
def _service(self, *, listening_socket: Socket) -> None:
"""
Inbound connection handler.
"""
Expand Down
5 changes: 3 additions & 2 deletions examples/udp_discard_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from __future__ import annotations

import time
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, override

import click

Expand Down Expand Up @@ -76,7 +76,8 @@ def __init__(self, *, local_ip_address: IpAddress, local_port: int):
local_port=local_port,
)

def service(self, *, listening_socket: Socket) -> None:
@override
def _service(self, *, listening_socket: Socket) -> None:
"""
Inbound connection handler.
"""
Expand Down
5 changes: 3 additions & 2 deletions examples/udp_echo_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from __future__ import annotations

import time
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, override

import click

Expand Down Expand Up @@ -77,7 +77,8 @@ def __init__(self, *, local_ip_address: IpAddress, local_port: int):
local_port=local_port,
)

def service(self, *, listening_socket: Socket) -> None:
@override
def _service(self, *, listening_socket: Socket) -> None:
"""
Inbound connection handler.
"""
Expand Down
3 changes: 3 additions & 0 deletions pytcp/lib/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
from pytcp.lib.net_addr import Ip4Address
from pytcp.lib.socket import Socket

version_string = "ver 3.0.2"
github_repository = "https://github.com/ccie18643/PyTCP"

timer = Timer()
rx_ring = RxRing()
tx_ring = TxRing()
Expand Down

0 comments on commit 1b07754

Please sign in to comment.