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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
import asyncio
from kademlia.network import Server
import socket
import zstandard as zstd
from typing import List, Dict, Optional, Set
import time
import logging
from dataclasses import dataclass
from contextlib import asynccontextmanager
from aiohttp import web
import threading
@dataclass
class PeerState:
last_seen: float
failed_attempts: int = 0
is_active: bool = True
class MeshNetwork:
def __init__(self, base_port: int = 12345, max_retry_ports: int = 5):
self.base_port = base_port
self.max_retry_ports = max_retry_ports
self.port: Optional[int] = None
self.dht = Server()
self.peers: Dict[str, PeerState] = {}
self.message_buffer: List[Dict] = []
self.is_running = False
self.known_messages: Set[str] = set()
self.retry_interval = 30
self.peer_timeout = 300
self.logger = logging.getLogger('MeshNetwork')
self._http_runner = None
async def _start_http_server(self):
"""Start minimal HTTP server for health checks"""
if self._http_runner:
return True
app = web.Application()
app.router.add_get('/', lambda _: web.Response(text="Mesh network running"))
app.router.add_get('/health', lambda _: web.Response(text="OK"))
self._http_runner = web.AppRunner(app)
try:
await self._http_runner.setup()
site = web.TCPSite(self._http_runner, '0.0.0.0', self.base_port)
await site.start()
self.logger.info(f"✅ HTTP health check server started on port {self.base_port}")
return True
except Exception as e:
self.logger.error(f"❌ Failed to start HTTP server: {str(e)}")
if self._http_runner:
await self._http_runner.cleanup()
self._http_runner = None
return False
async def _verify_port_active(self, port: int, timeout: int = 5) -> bool:
"""Wait for port to become active with shorter timeout"""
end_time = time.time() + timeout
while time.time() < end_time:
try:
# Use the DHT's bootstrap functionality to verify
await self.dht.bootstrap([('127.0.0.1', port)])
self.logger.info(f"✅ Port {port} is active and responding")
return True
except Exception as e:
self.logger.debug(f"Port verification attempt failed: {str(e)}")
await asyncio.sleep(0.5)
self.logger.error(f"❌ Port {port} did not become active within {timeout} seconds")
return False
async def _cleanup_port(self, port: int):
"""Cleanup a port before trying to use it"""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('0.0.0.0', port))
sock.close()
except Exception as e:
self.logger.debug(f"Port cleanup failed: {str(e)}")
await asyncio.sleep(1)
async def start(self) -> None:
"""Start mesh network with robust port handling"""
# First try to start HTTP server on base port
self.logger.info(f"🚀 Starting HTTP health check server on port {self.base_port}")
if not await self._start_http_server():
self.logger.warning("⚠️ Could not start HTTP server on base port")
# Try ports for DHT
for port_offset in range(self.max_retry_ports):
test_port = self.base_port + port_offset + 1 # Start from base_port + 1
self.logger.info(f"🔍 Attempting to start DHT on port {test_port}")
# Cleanup port before use
await self._cleanup_port(test_port)
try:
await self.dht.listen(test_port)
self.port = test_port
self.is_running = True
# Start maintenance tasks
asyncio.create_task(self._peer_maintenance())
asyncio.create_task(self._handle_message_buffer())
asyncio.create_task(self._heartbeat())
# Short wait before verification
await asyncio.sleep(1)
# Verify DHT is listening
if await self._verify_port_active(test_port):
self.logger.info(f"✅ DHT started successfully on port {test_port}")
return
else:
raise RuntimeError(f"DHT failed to bind to port {test_port}")
except Exception as e:
self.logger.error(f"❌ Failed to start on port {test_port}: {str(e)}")
if self.dht:
try:
await self.dht.stop()
except:
pass
self.is_running = False
continue
raise RuntimeError(f"Could not find available port in range {self.base_port+1}-{self.base_port + self.max_retry_ports}")
async def stop(self) -> None:
"""Stop mesh network gracefully"""
self.logger.info("Stopping mesh network")
self.is_running = False
# Stop HTTP server
if self._http_runner:
await self._http_runner.cleanup()
self._http_runner = None
# Stop DHT
if self.dht:
await self.dht.stop()
await asyncio.sleep(1) # Allow tasks to complete
self.logger.info("Mesh network stopped")
async def broadcast_message(self, message: str) -> None:
"""Broadcast message to all peers with deduplication"""
msg_hash = hash(message)
if msg_hash in self.known_messages:
return
self.known_messages.add(msg_hash)
compressed = self._compress_message(message)
failed_peers = []
for peer, state in self.peers.items():
if not state.is_active:
continue
try:
async with self._peer_connection(peer) as conn:
await self._send_message(conn, compressed)
except Exception as e:
self.logger.warning(f"Failed to send to {peer}: {str(e)}")
state.failed_attempts += 1
if state.failed_attempts >= 3:
state.is_active = False
failed_peers.append((peer, compressed))
# Add failed deliveries to buffer
for peer, msg in failed_peers:
self.message_buffer.append({
"peer": peer,
"message": msg,
"attempts": 0,
"timestamp": time.time()
})
@asynccontextmanager
async def _peer_connection(self, peer: str):
"""Context manager for peer connections with timeout"""
reader, writer = await asyncio.wait_for(
asyncio.open_connection(peer, self.port),
timeout=10
)
try:
yield (reader, writer)
finally:
writer.close()
await writer.wait_closed()
async def _send_message(self, conn, data: bytes) -> None:
"""Send message with length prefix"""
reader, writer = conn
# Send length prefix
writer.write(len(data).to_bytes(4, 'big'))
writer.write(data)
await writer.drain()
async def _peer_maintenance(self) -> None:
"""Maintain peer list and handle failures"""
while self.is_running:
current_time = time.time()
# Remove stale peers
self.peers = {
peer: state
for peer, state in self.peers.items()
if current_time - state.last_seen < self.peer_timeout
}
# Reset failed attempts periodically
for state in self.peers.values():
if not state.is_active and state.failed_attempts > 0:
state.failed_attempts = max(0, state.failed_attempts - 1)
if state.failed_attempts == 0:
state.is_active = True
await asyncio.sleep(60)
async def _handle_message_buffer(self) -> None:
"""Process buffered messages with exponential backoff"""
while self.is_running:
current_time = time.time()
for msg in self.message_buffer[:]: # Copy to allow modification
if msg["attempts"] < 5: # Try 5 times with increasing delays
try:
await self._send_to_peer(msg["peer"], msg["message"])
self.message_buffer.remove(msg)
except Exception:
msg["attempts"] += 1
# Exponential backoff
await asyncio.sleep(2 ** msg["attempts"])
elif current_time - msg["timestamp"] > 3600: # Remove after 1 hour
self.message_buffer.remove(msg)
await asyncio.sleep(self.retry_interval)
async def _heartbeat(self) -> None:
"""Send periodic heartbeats to peers"""
while self.is_running:
for peer, state in self.peers.items():
if not state.is_active:
continue
try:
async with self._peer_connection(peer) as conn:
await self._send_message(conn, b"PING")
state.last_seen = time.time()
state.failed_attempts = 0
except Exception:
state.failed_attempts += 1
if state.failed_attempts >= 3:
state.is_active = False
await asyncio.sleep(30) # Heartbeat every 30 seconds
def _compress_message(self, message: str) -> bytes:
"""Compress message using zstd with error handling"""
try:
compressor = zstd.ZstdCompressor(level=3) # Balanced compression
return compressor.compress(message.encode())
except Exception as e:
self.logger.error(f"Compression failed: {str(e)}")
# Fallback to uncompressed
return message.encode()
def _decompress_message(self, data: bytes) -> str:
"""Decompress message with error handling"""
try:
decompressor = zstd.ZstdDecompressor()
return decompressor.decompress(data).decode()
except Exception as e:
self.logger.error(f"Decompression failed: {str(e)}")
# Try to return as-is if decompression fails
return data.decode(errors='replace')
async def _send_to_peer(self, peer: str, data: bytes):
async with self._peer_connection(peer) as conn:
await self._send_message(conn, data)
self.logger.info(f"Message sent to {peer}") #Added logging
async def _verify_port_available(self, port: int) -> bool:
"""Verify if a port is available"""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('0.0.0.0', port))
sock.close()
return True
except:
return False
async def _wait_for_port_active(self, port: int, timeout: int = 30) -> bool:
"""Wait for port to become active"""
end_time = time.time() + timeout
while time.time() < end_time:
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('127.0.0.1', port))
sock.close()
return True
except:
await asyncio.sleep(1)
return False
|