Skip to content

Commit

Permalink
This might be harder than I imagined
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Wallar committed Sep 17, 2015
1 parent 19890a9 commit dcd3472
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/jammi/nameserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(self, host, port, interval):
self.interval = interval
self.masters = dict()
self.times = dict()
self.new_connections = list()
self.running = False
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind((host, port))
Expand Down Expand Up @@ -99,6 +100,24 @@ def get_time(self, name):

return self.times[name]

def discover(self):
"""
Discover new masters
Returns
-------
nc: list of strings
The new masters on the network
Notes
-----
This method resets the `new_connections` variable to an empty list
"""

nc = self.new_connections[:]
self.new_connections = list()
return nc

def start(self):
"""
Starts the name server thread
Expand All @@ -117,6 +136,8 @@ def __thread():
data_zip, _ = self.sock.recvfrom(1024)
data_str = zlib.decompress(data_zip)
data = json.loads(data_str)
if not data["name"] in self.masters:
self.new_connections.append(data["name"])
self.masters[data["name"]] = data
self.times[data["name"]] = time.time()
self.running = True
Expand Down

0 comments on commit dcd3472

Please sign in to comment.