feat: service exploration module
This commit is contained in:
parent
f253a0d9df
commit
b1de4dcacd
@ -15,6 +15,43 @@ def handle_client(conn):
|
|||||||
conn.close()
|
conn.close()
|
||||||
break
|
break
|
||||||
|
|
||||||
|
class ServiceExplorationModule():
|
||||||
|
def __init__(self, host, port):
|
||||||
|
# UDP server
|
||||||
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
||||||
|
self.host = host
|
||||||
|
self.port = port
|
||||||
|
|
||||||
|
self.is_available = False
|
||||||
|
|
||||||
|
def listen(self):
|
||||||
|
self.sock.bind((self.host, self.port))
|
||||||
|
print(f"Exploration Server(UDP) listening on port {self.port}...")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
data, addr = self.sock.recvfrom(1024)
|
||||||
|
|
||||||
|
if self.is_available:
|
||||||
|
if data.decode() == '[EXPLORE]':
|
||||||
|
print(f"Receive exploration broadcast from {addr}")
|
||||||
|
self.sock.sendto(self.host.encode(), addr)
|
||||||
|
|
||||||
|
def explore(self):
|
||||||
|
available_host = []
|
||||||
|
|
||||||
|
self.sock.settimeout(3)
|
||||||
|
self.sock.sendto('[EXPLORE]'.encode(), ('255.255.255.255', self.port))
|
||||||
|
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
data, addr = self.sock.recvfrom(1024)
|
||||||
|
available_host.append(addr)
|
||||||
|
print(data.decode(), addr)
|
||||||
|
except:
|
||||||
|
# if socket timeout
|
||||||
|
break
|
||||||
|
return available_host
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
@ -26,32 +63,11 @@ if __name__ == '__main__':
|
|||||||
socket_type = args.type
|
socket_type = args.type
|
||||||
host, port = args.host, args.port
|
host, port = args.host, args.port
|
||||||
|
|
||||||
if socket_type == 'server':
|
service_explore = ServiceExplorationModule(host, port)
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
explore_service_thread = threading.Thread(target=service_explore.listen)
|
||||||
sock.bind((host, port))
|
explore_service_thread.start()
|
||||||
|
|
||||||
print("Server listening...")
|
|
||||||
|
|
||||||
while True:
|
|
||||||
data, addr = sock.recvfrom(1024)
|
|
||||||
print(f"Accept connection from {addr}")
|
|
||||||
|
|
||||||
sock.sendto(host.encode(), addr)
|
|
||||||
|
|
||||||
# thread = threading.Thread(target=handle_client, args=(conn, ))
|
|
||||||
# thread.start()
|
|
||||||
|
|
||||||
else:
|
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
||||||
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
|
||||||
sock.settimeout(2)
|
|
||||||
|
|
||||||
sock.sendto('discover'.encode(), ('255.255.255.255', port))
|
|
||||||
|
|
||||||
while True:
|
|
||||||
data, addr = sock.recvfrom(1024)
|
|
||||||
print(data.decode(), addr)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if socket_type == 'client':
|
||||||
|
hosts = service_explore.explore()
|
||||||
|
print(hosts)
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user