Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Sep 8, 2024
1 parent 1b07754 commit 1536d13
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 120 deletions.
21 changes: 9 additions & 12 deletions examples/icmp_echo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def __thread__client(self) -> None:
)
@click.option(
"--ip6-address",
"ip6_host",
type=ClickTypeIp6Host(),
default=None,
help="IPv6 address/mask to be assigned to the interface.",
Expand All @@ -192,6 +193,7 @@ def __thread__client(self) -> None:
)
@click.option(
"--ip4-address",
"ip4_host",
type=ClickTypeIp4Host(),
default=None,
help="IPv4 address/mask to be assigned to the interface.",
Expand All @@ -212,9 +214,9 @@ def cli(
*,
interface: str,
mac_address: MacAddress | None,
ip6_address: Ip6Host | None,
ip6_host: Ip6Host | None,
ip6_gateway: Ip6Address | None,
ip4_address: Ip4Host | None,
ip4_host: Ip4Host | None,
ip4_gateway: Ip4Address | None,
remote_ip_address: IpAddress,
) -> None:
Expand All @@ -225,16 +227,11 @@ def cli(

fd, mtu = initialize_interface(interface)

ip6_host = (
None
if ip6_address is None
else Ip6Host(ip6_address, gateway=ip6_gateway)
)
ip4_host = (
None
if ip4_address is None
else Ip4Host(ip4_address, gateway=ip4_gateway)
)
if ip6_host:
ip6_host.gateway = ip6_gateway

if ip4_host:
ip4_host.gateway = ip4_gateway

stack = TcpIpStack(
fd=fd,
Expand Down
24 changes: 12 additions & 12 deletions examples/run_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
)
@click.option(
"--ip6-address",
"ip6_host",
type=ClickTypeIp6Host(),
default=None,
help="IPv6 address/mask to be assigned to the interface.",
Expand All @@ -79,6 +80,7 @@
)
@click.option(
"--ip4-address",
"ip4_host",
type=ClickTypeIp4Host(),
default=None,
help="IPv4 address/mask to be assigned to the interface.",
Expand All @@ -93,9 +95,9 @@ def cli(
*,
interface: str,
mac_address: MacAddress | None,
ip6_address: Ip6Host | None,
ip6_host: Ip6Host | None,
ip6_gateway: Ip6Address | None,
ip4_address: Ip4Host | None,
ip4_host: Ip4Host | None,
ip4_gateway: Ip4Address | None,
) -> None:
"""
Expand All @@ -104,20 +106,18 @@ def cli(

fd, mtu = initialize_interface(interface)

if ip6_host:
ip6_host.gateway = ip6_gateway

if ip4_host:
ip4_host.gateway = ip4_gateway

stack = TcpIpStack(
fd=fd,
mtu=mtu,
mac_address=mac_address,
ip6_host=(
None
if ip6_address is None
else Ip6Host(ip6_address, gateway=ip6_gateway)
),
ip4_host=(
None
if ip4_address is None
else Ip4Host(ip4_address, gateway=ip4_gateway)
),
ip6_host=ip6_host,
ip4_host=ip4_host,
)

try:
Expand Down
21 changes: 9 additions & 12 deletions examples/tcp_daytime_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def _service(self, *, connected_socket: Socket) -> None:
)
@click.option(
"--ip6-address",
"ip6_host",
type=ClickTypeIp6Host(),
default=None,
help="IPv6 address/mask to be assigned to the interface.",
Expand All @@ -151,6 +152,7 @@ def _service(self, *, connected_socket: Socket) -> None:
)
@click.option(
"--ip4-address",
"ip4_host",
type=ClickTypeIp4Host(),
default=None,
help="IPv4 address/mask to be assigned to the interface.",
Expand All @@ -171,9 +173,9 @@ def cli(
*,
interface: str,
mac_address: MacAddress | None,
ip6_address: Ip6Host | None,
ip6_host: Ip6Host | None,
ip6_gateway: Ip6Address | None,
ip4_address: Ip4Host | None,
ip4_host: Ip4Host | None,
ip4_gateway: Ip4Address | None,
local_port: int,
) -> None:
Expand All @@ -184,16 +186,11 @@ def cli(

fd, mtu = initialize_interface(interface)

ip6_host = (
None
if ip6_address is None
else Ip6Host(ip6_address, gateway=ip6_gateway)
)
ip4_host = (
None
if ip4_address is None
else Ip4Host(ip4_address, gateway=ip4_gateway)
)
if ip6_host:
ip6_host.gateway = ip6_gateway

if ip4_host:
ip4_host.gateway = ip4_gateway

stack = TcpIpStack(
fd=fd,
Expand Down
21 changes: 9 additions & 12 deletions examples/tcp_discard_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def _service(self, *, connected_socket: Socket) -> None:
)
@click.option(
"--ip6-address",
"ip6_host",
type=ClickTypeIp6Host(),
default=None,
help="IPv6 address/mask to be assigned to the interface.",
Expand All @@ -156,6 +157,7 @@ def _service(self, *, connected_socket: Socket) -> None:
)
@click.option(
"--ip4-address",
"ip4_host",
type=ClickTypeIp4Host(),
default=None,
help="IPv4 address/mask to be assigned to the interface.",
Expand All @@ -176,9 +178,9 @@ def cli(
*,
interface: str,
mac_address: MacAddress | None,
ip6_address: Ip6Host | None,
ip6_host: Ip6Host | None,
ip6_gateway: Ip6Address | None,
ip4_address: Ip4Host | None,
ip4_host: Ip4Host | None,
ip4_gateway: Ip4Address | None,
local_port: int,
) -> None:
Expand All @@ -189,16 +191,11 @@ def cli(

fd, mtu = initialize_interface(interface)

ip6_host = (
None
if ip6_address is None
else Ip6Host(ip6_address, gateway=ip6_gateway)
)
ip4_host = (
None
if ip4_address is None
else Ip4Host(ip4_address, gateway=ip4_gateway)
)
if ip6_host:
ip6_host.gateway = ip6_gateway

if ip4_host:
ip4_host.gateway = ip4_gateway

stack = TcpIpStack(
fd=fd,
Expand Down
21 changes: 9 additions & 12 deletions examples/tcp_echo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def __thread__client(self) -> None:
)
@click.option(
"--ip6-address",
"ip6_host",
type=ClickTypeIp6Host(),
default=None,
help="IPv6 address/mask to be assigned to the interface.",
Expand All @@ -206,6 +207,7 @@ def __thread__client(self) -> None:
)
@click.option(
"--ip4-address",
"ip4_host",
type=ClickTypeIp4Host(),
default=None,
help="IPv4 address/mask to be assigned to the interface.",
Expand All @@ -226,9 +228,9 @@ def cli(
*,
interface: str,
mac_address: MacAddress | None,
ip6_address: Ip6Host | None,
ip6_host: Ip6Host | None,
ip6_gateway: Ip6Address | None,
ip4_address: Ip4Host | None,
ip4_host: Ip4Host | None,
ip4_gateway: Ip4Address | None,
remote_ip_address: IpAddress,
) -> None:
Expand All @@ -239,16 +241,11 @@ def cli(

fd, mtu = initialize_interface(interface)

ip6_host = (
None
if ip6_address is None
else Ip6Host(ip6_address, gateway=ip6_gateway)
)
ip4_host = (
None
if ip4_address is None
else Ip4Host(ip4_address, gateway=ip4_gateway)
)
if ip6_host:
ip6_host.gateway = ip6_gateway

if ip4_host:
ip4_host.gateway = ip4_gateway

stack = TcpIpStack(
fd=fd,
Expand Down
21 changes: 9 additions & 12 deletions examples/tcp_echo_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def _service(self, *, connected_socket: Socket) -> None:
)
@click.option(
"--ip6-address",
"ip6_host",
type=ClickTypeIp6Host(),
default=None,
help="IPv6 address/mask to be assigned to the interface.",
Expand All @@ -167,6 +168,7 @@ def _service(self, *, connected_socket: Socket) -> None:
)
@click.option(
"--ip4-address",
"ip4_host",
type=ClickTypeIp4Host(),
default=None,
help="IPv4 address/mask to be assigned to the interface.",
Expand All @@ -187,9 +189,9 @@ def cli(
*,
interface: str,
mac_address: MacAddress | None,
ip6_address: Ip6Host | None,
ip6_host: Ip6Host | None,
ip6_gateway: Ip6Address | None,
ip4_address: Ip4Host | None,
ip4_host: Ip4Host | None,
ip4_gateway: Ip4Address | None,
local_port: int,
) -> None:
Expand All @@ -200,16 +202,11 @@ def cli(

fd, mtu = initialize_interface(interface)

ip6_host = (
None
if ip6_address is None
else Ip6Host(ip6_address, gateway=ip6_gateway)
)
ip4_host = (
None
if ip4_address is None
else Ip4Host(ip4_address, gateway=ip4_gateway)
)
if ip6_host:
ip6_host.gateway = ip6_gateway

if ip4_host:
ip4_host.gateway = ip4_gateway

stack = TcpIpStack(
fd=fd,
Expand Down
21 changes: 9 additions & 12 deletions examples/udp_daytime_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def _service(self, *, listening_socket: Socket) -> None:
)
@click.option(
"--ip6-address",
"ip6_host",
type=ClickTypeIp6Host(),
default=None,
help="IPv6 address/mask to be assigned to the interface.",
Expand All @@ -119,6 +120,7 @@ def _service(self, *, listening_socket: Socket) -> None:
)
@click.option(
"--ip4-address",
"ip4_host",
type=ClickTypeIp4Host(),
default=None,
help="IPv4 address/mask to be assigned to the interface.",
Expand All @@ -139,9 +141,9 @@ def cli(
*,
interface: str,
mac_address: MacAddress | None,
ip6_address: Ip6Host | None,
ip6_host: Ip6Host | None,
ip6_gateway: Ip6Address | None,
ip4_address: Ip4Host | None,
ip4_host: Ip4Host | None,
ip4_gateway: Ip4Address | None,
local_port: int,
) -> None:
Expand All @@ -152,16 +154,11 @@ def cli(

fd, mtu = initialize_interface(interface)

ip6_host = (
None
if ip6_address is None
else Ip6Host(ip6_address, gateway=ip6_gateway)
)
ip4_host = (
None
if ip4_address is None
else Ip4Host(ip4_address, gateway=ip4_gateway)
)
if ip6_host:
ip6_host.gateway = ip6_gateway

if ip4_host:
ip4_host.gateway = ip4_gateway

stack = TcpIpStack(
fd=fd,
Expand Down
Loading

0 comments on commit 1536d13

Please sign in to comment.