-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrserver.py
More file actions
95 lines (71 loc) · 2.94 KB
/
rserver.py
File metadata and controls
95 lines (71 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
import RNS
import sys
import time
import threading
import config
from content import ensure_public_directory
from reticulum import get_or_create_identity, create_destination, start_link_server
from http import http_handler
def main():
try:
print("RServer - Reticulum Web Server")
print("=" * 40)
print("Initializing Reticulum...")
# Initialize Reticulum with default config
reticulum = RNS.Reticulum()
print("✓ Reticulum initialized successfully")
# Display basic status using correct API
print(f"✓ Transport enabled: {RNS.Reticulum.transport_enabled()}")
print(f"✓ Instance ready: {RNS.Reticulum.get_instance() is not None}")
# Load or create server identity
identity, was_created = get_or_create_identity()
print(f"✓ {'Created' if was_created else 'Loaded'} server identity: {RNS.prettyhexrep(identity.hash)}")
# Create destination for this server
destination = create_destination(identity)
app_name, aspect = config.app_context()
print(f"✓ Server destination: {RNS.prettyhexrep(destination.hash)}")
print(f"✓ App context: {app_name}.{aspect}")
# Ensure public directory exists with default content
print(f"✓ Public directory: {config.public_dir()}")
created = ensure_public_directory()
if created:
print(f"✓ Created public directory: {config.public_dir()}")
print(f"✓ Created default file: {config.default_file()}")
# Start Link server with HTTP handler
start_link_server(destination, http_handler)
# Start periodic announcer
start_announcer(destination)
print("\nRServer is running. Press Ctrl+C to exit.")
print()
# Keep the program running (use timeout-based wait so Ctrl+C is handled)
stop_event = threading.Event()
while not stop_event.wait(timeout=1):
# loop wakes every second to allow KeyboardInterrupt handling
continue
except KeyboardInterrupt:
print("\nShutting down RServer...")
RNS.exit()
sys.exit(0)
except Exception as e:
print(f"✗ Error initializing Reticulum: {e}")
sys.exit(1)
def start_announcer(destination):
"""Start a daemon thread that periodically calls `destination.announce()`.
"""
interval = config.announce_interval()
app_data = config.server_name().encode("utf-8")
def _announcer():
# Announce immediately, then every `interval` seconds
while True:
try:
destination.announce(app_data=app_data)
print("✓ Server announced on Reticulum network")
except Exception as ae:
print(f"✗ Announcement failed: {ae}")
time.sleep(interval)
t = threading.Thread(target=_announcer, daemon=True)
t.start()
return t
if __name__ == "__main__":
main()