Refactor main to improve readability and maintainability.
This commit is contained in:
@@ -1,29 +1,26 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import threading
|
||||
import time
|
||||
|
||||
import RNS
|
||||
|
||||
# Determine base directory for tests
|
||||
dir_path = os.path.abspath(os.path.dirname(__file__))
|
||||
config_dir = os.path.join(dir_path, 'config')
|
||||
identity_dir = os.path.join(dir_path, 'node-config')
|
||||
config_dir = os.path.join(dir_path, "config")
|
||||
identity_dir = os.path.join(dir_path, "node-config")
|
||||
|
||||
# Initialize Reticulum with shared config
|
||||
RNS.Reticulum(config_dir)
|
||||
|
||||
# Load server identity (created by the page node)
|
||||
identity_file = os.path.join(identity_dir, 'identity')
|
||||
identity_file = os.path.join(identity_dir, "identity")
|
||||
server_identity = RNS.Identity.from_file(identity_file)
|
||||
|
||||
# Create a destination to the server node
|
||||
destination = RNS.Destination(
|
||||
server_identity,
|
||||
RNS.Destination.OUT,
|
||||
RNS.Destination.SINGLE,
|
||||
'nomadnetwork',
|
||||
'node'
|
||||
server_identity, RNS.Destination.OUT, RNS.Destination.SINGLE, "nomadnetwork", "node"
|
||||
)
|
||||
|
||||
# Ensure we know a path to the destination
|
||||
@@ -39,53 +36,57 @@ global_link = RNS.Link(destination)
|
||||
responses = {}
|
||||
done_event = threading.Event()
|
||||
|
||||
|
||||
# Callback for page response
|
||||
def on_page(response):
|
||||
data = response.response
|
||||
if isinstance(data, bytes):
|
||||
text = data.decode('utf-8')
|
||||
text = data.decode("utf-8")
|
||||
else:
|
||||
text = str(data)
|
||||
print('Received page:')
|
||||
print("Received page:")
|
||||
print(text)
|
||||
responses['page'] = text
|
||||
if 'file' in responses:
|
||||
responses["page"] = text
|
||||
if "file" in responses:
|
||||
done_event.set()
|
||||
|
||||
|
||||
# Callback for file response
|
||||
def on_file(response):
|
||||
data = response.response
|
||||
# Handle response as [fileobj, headers]
|
||||
if isinstance(data, list) and len(data) == 2 and hasattr(data[0], 'read'):
|
||||
if isinstance(data, list) and len(data) == 2 and hasattr(data[0], "read"):
|
||||
fileobj, headers = data
|
||||
file_data = fileobj.read()
|
||||
filename = headers.get(b'name', b'').decode('utf-8')
|
||||
print(f'Received file ({filename}):')
|
||||
print(file_data.decode('utf-8'))
|
||||
responses['file'] = file_data.decode('utf-8')
|
||||
filename = headers.get(b"name", b"").decode("utf-8")
|
||||
print(f"Received file ({filename}):")
|
||||
print(file_data.decode("utf-8"))
|
||||
responses["file"] = file_data.decode("utf-8")
|
||||
# Handle response as a raw file object
|
||||
elif hasattr(data, 'read'):
|
||||
elif hasattr(data, "read"):
|
||||
file_data = data.read()
|
||||
filename = os.path.basename('text.txt')
|
||||
print(f'Received file ({filename}):')
|
||||
print(file_data.decode('utf-8'))
|
||||
responses['file'] = file_data.decode('utf-8')
|
||||
filename = os.path.basename("text.txt")
|
||||
print(f"Received file ({filename}):")
|
||||
print(file_data.decode("utf-8"))
|
||||
responses["file"] = file_data.decode("utf-8")
|
||||
# Handle response as raw bytes
|
||||
elif isinstance(data, bytes):
|
||||
text = data.decode('utf-8')
|
||||
print('Received file:')
|
||||
text = data.decode("utf-8")
|
||||
print("Received file:")
|
||||
print(text)
|
||||
responses['file'] = text
|
||||
responses["file"] = text
|
||||
else:
|
||||
print('Received file (unhandled format):', data)
|
||||
responses['file'] = str(data)
|
||||
if 'page' in responses:
|
||||
print("Received file (unhandled format):", data)
|
||||
responses["file"] = str(data)
|
||||
if "page" in responses:
|
||||
done_event.set()
|
||||
|
||||
|
||||
# Request the page and file once the link is established
|
||||
def on_link_established(link):
|
||||
link.request('/page/index.mu', None, response_callback=on_page)
|
||||
link.request('/file/text.txt', None, response_callback=on_file)
|
||||
link.request("/page/index.mu", None, response_callback=on_page)
|
||||
link.request("/file/text.txt", None, response_callback=on_file)
|
||||
|
||||
|
||||
# Register callbacks
|
||||
global_link.set_link_established_callback(on_link_established)
|
||||
@@ -93,12 +94,12 @@ global_link.set_link_closed_callback(lambda l: done_event.set())
|
||||
|
||||
# Wait for responses or timeout
|
||||
if not done_event.wait(timeout=30):
|
||||
print('Test timed out.', file=sys.stderr)
|
||||
print("Test timed out.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
if responses.get('page') and responses.get('file'):
|
||||
print('Tests passed!')
|
||||
if responses.get("page") and responses.get("file"):
|
||||
print("Tests passed!")
|
||||
sys.exit(0)
|
||||
else:
|
||||
print('Tests failed.', file=sys.stderr)
|
||||
print("Tests failed.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
@@ -1,20 +1,26 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import threading
|
||||
import time
|
||||
|
||||
import RNS
|
||||
|
||||
dir_path = os.path.abspath(os.path.dirname(__file__))
|
||||
config_dir = os.path.join(dir_path, 'config')
|
||||
config_dir = os.path.join(dir_path, "config")
|
||||
|
||||
RNS.Reticulum(config_dir)
|
||||
|
||||
DESTINATION_HEX = '49b2d959db8528347d0a38083aec1042' # Ivans Node that runs rns-page-node
|
||||
DESTINATION_HEX = (
|
||||
"49b2d959db8528347d0a38083aec1042" # Ivans Node that runs rns-page-node
|
||||
)
|
||||
|
||||
dest_len = (RNS.Reticulum.TRUNCATED_HASHLENGTH // 8) * 2
|
||||
if len(DESTINATION_HEX) != dest_len:
|
||||
print(f"Invalid destination length (got {len(DESTINATION_HEX)}, expected {dest_len})", file=sys.stderr)
|
||||
print(
|
||||
f"Invalid destination length (got {len(DESTINATION_HEX)}, expected {dest_len})",
|
||||
file=sys.stderr,
|
||||
)
|
||||
sys.exit(1)
|
||||
destination_hash = bytes.fromhex(DESTINATION_HEX)
|
||||
|
||||
@@ -28,32 +34,32 @@ server_identity = RNS.Identity.recall(destination_hash)
|
||||
print(f"Recalled server identity for {DESTINATION_HEX}")
|
||||
|
||||
destination = RNS.Destination(
|
||||
server_identity,
|
||||
RNS.Destination.OUT,
|
||||
RNS.Destination.SINGLE,
|
||||
'nomadnetwork',
|
||||
'node'
|
||||
server_identity, RNS.Destination.OUT, RNS.Destination.SINGLE, "nomadnetwork", "node"
|
||||
)
|
||||
link = RNS.Link(destination)
|
||||
|
||||
done_event = threading.Event()
|
||||
|
||||
|
||||
def on_page(response):
|
||||
data = response.response
|
||||
if isinstance(data, bytes):
|
||||
text = data.decode('utf-8')
|
||||
text = data.decode("utf-8")
|
||||
else:
|
||||
text = str(data)
|
||||
print('Fetched page content:')
|
||||
print("Fetched page content:")
|
||||
print(text)
|
||||
done_event.set()
|
||||
|
||||
link.set_link_established_callback(lambda l: l.request('/page/index.mu', None, response_callback=on_page))
|
||||
|
||||
link.set_link_established_callback(
|
||||
lambda l: l.request("/page/index.mu", None, response_callback=on_page)
|
||||
)
|
||||
link.set_link_closed_callback(lambda l: done_event.set())
|
||||
|
||||
if not done_event.wait(timeout=30):
|
||||
print('Timed out waiting for page', file=sys.stderr)
|
||||
print("Timed out waiting for page", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
print('Done fetching page.')
|
||||
print("Done fetching page.")
|
||||
sys.exit(0)
|
||||
|
||||
Reference in New Issue
Block a user