-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9477c85
commit cbce9c5
Showing
1 changed file
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,57 @@ | ||
# Placeholder content for peer_discovery.py | ||
# Placeholder content for peer_import socket | ||
import struct | ||
import threading | ||
import time | ||
|
||
MCAST_GRP = '224.1.1.1' | ||
MCAST_PORT = 5007 | ||
|
||
class PeerDiscovery: | ||
def __init__(self): | ||
self.peer_address = self.get_own_ip() | ||
self.peers = set() | ||
|
||
def get_own_ip(self): | ||
# Get the local machine's IP address | ||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | ||
try: | ||
# doesn't even have to be reachable | ||
s.connect(('10.255.255.255', 1)) | ||
IP = s.getsockname()[0] | ||
except Exception: | ||
IP = '127.0.0.1' | ||
finally: | ||
s.close() | ||
return IP | ||
|
||
def listen_for_peers(self): | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | ||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
sock.bind(('', MCAST_PORT)) | ||
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY) | ||
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) | ||
|
||
while True: | ||
data, addr = sock.recvfrom(1024) | ||
if addr[0] not in self.peers and addr[0] != self.peer_address: | ||
print(f"Discovered new peer: {addr[0]}") | ||
self.peers.add(addr[0]) | ||
|
||
def broadcast_presence(self): | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | ||
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2) | ||
while True: | ||
sock.sendto(b'Peer Discovery Ping', (MCAST_GRP, MCAST_PORT)) | ||
time.sleep(5) | ||
|
||
def start_discovery(self): | ||
listener_thread = threading.Thread(target=self.listen_for_peers) | ||
broadcaster_thread = threading.Thread(target=self.broadcast_presence) | ||
listener_thread.start() | ||
broadcaster_thread.start() | ||
|
||
# Example usage | ||
if __name__ == "__main__": | ||
discovery = PeerDiscovery() | ||
discovery.start_discovery() | ||
discovery.py |