Skip to content

Commit

Permalink
Updated example services
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Jul 7, 2024
1 parent bd1889e commit 10de61a
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 21 deletions.
18 changes: 14 additions & 4 deletions examples/tcp_daytime_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ class TcpDaytimeService(TcpService):
def __init__(
self,
*,
local_ip_address: str = "0.0.0.0",
local_port: int = 13,
local_ip_address: str,
local_port: int,
message_count: int = -1,
message_delay: int = 1,
):
Expand Down Expand Up @@ -113,14 +113,24 @@ def service(self, *, connected_socket: Socket) -> None:

@click.command()
@click.option("--interface", default="tap7")
def cli(*, interface: str) -> None:
@click.option("--local-ip-address", default="0.0.0.0")
@click.option("--local-port", default=13, type=int)
def cli(
*,
interface: str,
local_ip_address: str,
local_port: int,
) -> None:
"""
Start PyTCP stack and stop it when user presses Ctrl-C.
Run the TCP Daytime service.
"""

stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
service = TcpDaytimeService()
service = TcpDaytimeService(
local_ip_address=local_ip_address,
local_port=local_port,
)

try:
stack.start()
Expand Down
19 changes: 16 additions & 3 deletions examples/tcp_discard_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ class TcpDiscardService(TcpService):
"""

def __init__(
self, *, local_ip_address: str = "0.0.0.0", local_port: int = 9
self,
*,
local_ip_address: str,
local_port: int,
):
"""
Class constructor.
Expand Down Expand Up @@ -120,14 +123,24 @@ def service(self, *, connected_socket: Socket) -> None:

@click.command()
@click.option("--interface", default="tap7")
def cli(*, interface: str) -> None:
@click.option("--local-ip-address", default="0.0.0.0")
@click.option("--local-port", default=9, type=int)
def cli(
*,
interface: str,
local_ip_address: str,
local_port: int,
) -> None:
"""
Start PyTCP stack and stop it when user presses Ctrl-C.
Run the TCP Discard service.
"""

stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
service = TcpDiscardService()
service = TcpDiscardService(
local_ip_address=local_ip_address,
local_port=local_port,
)

try:
stack.start()
Expand Down
19 changes: 16 additions & 3 deletions examples/tcp_echo_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class TcpEchoService(TcpService):
"""

def __init__(
self, *, local_ip_address: str = "0.0.0.0", local_port: int = 7
self,
*,
local_ip_address: str,
local_port: int,
):
"""
Class constructor.
Expand Down Expand Up @@ -131,14 +134,24 @@ def service(self, *, connected_socket: Socket) -> None:

@click.command()
@click.option("--interface", default="tap7")
def cli(*, interface: str) -> None:
@click.option("--local-ip-address", default="0.0.0.0")
@click.option("--local-port", default=7, type=int)
def cli(
*,
interface: str,
local_ip_address: str,
local_port: int,
) -> None:
"""
Start PyTCP stack and stop it when user presses Ctrl-C.
Run the TCP Echo service.
"""

stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
service = TcpEchoService()
service = TcpEchoService(
local_ip_address=local_ip_address,
local_port=local_port,
)

try:
stack.start()
Expand Down
19 changes: 16 additions & 3 deletions examples/udp_daytime_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ class UdpDaytimeService(UdpService):
"""

def __init__(
self, *, local_ip_address: str = "0.0.0.0", local_port: int = 13
self,
*,
local_ip_address: str,
local_port: int,
):
"""
Class constructor.
Expand Down Expand Up @@ -83,14 +86,24 @@ def service(self, *, listening_socket: Socket) -> None:

@click.command()
@click.option("--interface", default="tap7")
def cli(*, interface: str) -> None:
@click.option("--local-ip-address", default="0.0.0.0")
@click.option("--local-port", default=13, type=int)
def cli(
*,
interface: str,
local_ip_address: str,
local_port: int,
) -> None:
"""
Start PyTCP stack and stop it when user presses Ctrl-C.
Run the UDP Daytime service.
"""

stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
service = UdpDaytimeService()
service = UdpDaytimeService(
local_ip_address=local_ip_address,
local_port=local_port,
)

try:
stack.start()
Expand Down
19 changes: 16 additions & 3 deletions examples/udp_discard_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ class UdpDiscardService(UdpService):
"""

def __init__(
self, *, local_ip_address: str = "0.0.0.0", local_port: int = 9
self,
*,
local_ip_address: str,
local_port: int,
):
"""
Class constructor.
Expand All @@ -80,14 +83,24 @@ def service(self, *, listening_socket: Socket) -> None:

@click.command()
@click.option("--interface", default="tap7")
def cli(*, interface: str) -> None:
@click.option("--local-ip-address", default="0.0.0.0")
@click.option("--local-port", default=9, type=int)
def cli(
*,
interface: str,
local_ip_address: str,
local_port: int,
) -> None:
"""
Start PyTCP stack and stop it when user presses Ctrl-C.
Run the UDP Discard service.
"""

stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
service = UdpDiscardService()
service = UdpDiscardService(
local_ip_address=local_ip_address,
local_port=local_port,
)

try:
stack.start()
Expand Down
13 changes: 8 additions & 5 deletions examples/udp_echo_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ class UdpEchoService(UdpService):
UDP Echo service support class.
"""

def __init__(
self, *, local_ip_address: str = "0.0.0.0", local_port: int = 7
):
def __init__(self, *, local_ip_address: str = "::", local_port: int = 7):
"""
Class constructor.
"""
Expand Down Expand Up @@ -96,14 +94,19 @@ def service(self, *, listening_socket: Socket) -> None:

@click.command()
@click.option("--interface", default="tap7")
def cli(*, interface: str) -> None:
@click.option("--local-ip-address", default="0.0.0.0")
@click.option("--local-port", default=7, type=int)
def cli(*, interface: str, local_ip_address: str, local_port: int) -> None:
"""
Start PyTCP stack and stop it when user presses Ctrl-C.
Run the UDP Echo service.
"""

stack = TcpIpStack(fd=initialize_tap(tap_name=interface))
service = UdpEchoService()
service = UdpEchoService(
local_ip_address=local_ip_address,
local_port=local_port,
)

try:
stack.start()
Expand Down
1 change: 1 addition & 0 deletions pytcp/protocols/udp/fpa.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def __init__(
self._plen = UDP_HEADER_LEN + len(self._data)
self._hlen = UDP_HEADER_LEN
self._dlen = len(self._data)
self._cksum = 0

def __len__(self) -> int:
"""
Expand Down

0 comments on commit 10de61a

Please sign in to comment.