Compare commits
No commits in common. "4fa614776d0592ca0b9204aaf4a66f27debe5b01" and "c0e7fea3bbba3dd3473f3689b2cfdbf4e95f00cf" have entirely different histories.
4fa614776d
...
c0e7fea3bb
21
main.py
21
main.py
@ -1,21 +0,0 @@
|
|||||||
import argparse
|
|
||||||
import threading
|
|
||||||
from src.communication import ServiceExplorationModule
|
|
||||||
from src.parser import get_args
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
args = get_args()
|
|
||||||
|
|
||||||
socket_type = args.type
|
|
||||||
host, port = args.host, args.port
|
|
||||||
|
|
||||||
# start Service Exploration Module
|
|
||||||
# let all client can know which IP address has our service so that it can link to.
|
|
||||||
service_explore = ServiceExplorationModule(host, port+1)
|
|
||||||
explore_service_thread = threading.Thread(target=service_explore.listen)
|
|
||||||
explore_service_thread.start()
|
|
||||||
|
|
||||||
if socket_type == 'client':
|
|
||||||
hosts = service_explore.explore()
|
|
||||||
print(hosts)
|
|
||||||
|
|
||||||
@ -1,42 +0,0 @@
|
|||||||
import socket
|
|
||||||
|
|
||||||
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.IP = socket.gethostbyname(socket.gethostname())
|
|
||||||
self.is_available = True
|
|
||||||
|
|
||||||
def listen(self):
|
|
||||||
self.sock.bind((self.host, self.port))
|
|
||||||
print(f"Exploration Server(UDP) listening on {self.host}:{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.IP.encode(), addr)
|
|
||||||
|
|
||||||
def explore(self):
|
|
||||||
available_host = []
|
|
||||||
|
|
||||||
client_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
||||||
client_sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
|
|
||||||
|
|
||||||
client_sock.settimeout(3)
|
|
||||||
client_sock.sendto('[EXPLORE]'.encode(), ('255.255.255.255', self.port))
|
|
||||||
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
data, addr = client_sock.recvfrom(1024)
|
|
||||||
if addr[0] != self.IP:
|
|
||||||
available_host.append(addr)
|
|
||||||
except:
|
|
||||||
# if socket timeout
|
|
||||||
break
|
|
||||||
return available_host
|
|
||||||
@ -1,10 +0,0 @@
|
|||||||
import argparse
|
|
||||||
|
|
||||||
def get_args():
|
|
||||||
parser = argparse.ArgumentParser()
|
|
||||||
parser.add_argument('type', type=str, help="'client' or 'server'", default='server')
|
|
||||||
parser.add_argument('--host', type=str, help="server's IPv4 address", default='0.0.0.0')
|
|
||||||
parser.add_argument('--port', type=int, help="server's listening port", default=8888)
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
|
||||||
return args
|
|
||||||
Loading…
Reference in New Issue
Block a user