experimental distributed hashtable implementations with 2D topology
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB

from node import Node
from keyspace import Keyspace
import gevent
from gevent import socket
from sys import stdin, argv
def start_first_node():
node = Node(own_port=60000, keyspace=Keyspace())
print ("Started new DHT with %s" % node)
return node
def start_node(entry_port):
node = Node()
print ("Started %s." % node)
node.join_network(entry_port) # FIXME: should exit if we don't get a response
return node
entry_port = int(argv[1])
node = start_node(entry_port)
except IndexError:
node = start_first_node()
def await_query(node):
data = stdin.readline().rstrip("\n")
return data
def await_request(node):
return node.socket.recvfrom(1024) # Buffer size is 1024 bytes
query = gevent.spawn(await_query, node)
request = gevent.spawn(await_request, node)
while True:
if query.successful():
query = gevent.spawn(await_query, node)
if request.successful():
queryType, sender = request.value
node.query(queryType.decode('utf-8'), sender)
request = gevent.spawn(await_request, node)
gevent.sleep(0.05) # to reduce cpu usage
except KeyboardInterrupt:
# TODO: handle network leave