Skip to content

Commit

Permalink
Commented up the heart class
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Wallar committed Sep 16, 2015
1 parent 8d260f9 commit 48124d9
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/jammi/heart.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,27 @@


class Heart(object):
"""
A heartbeat class that can be used to send messages over UDP to a group
with a given rate
"""

def __init__(self, host, port, rate):
"""
Initializes a multicast heart object
Parameters
----------
host: string
The host of the multicast group
port: integer
The port of the multicast group
rate: integer
The rate per second that the messages would be sent over UDP
"""

self.host = host
self.port = port
self.rate = rate
Expand All @@ -17,20 +36,47 @@ def __init__(self, host, port, rate):
self.running = False

def set_data(self, data):
"""
Sets the data being sent over UDP
Parameters
----------
data: dict
A dictionary to update the current data with
"""

self.data = data
return self

def beat(self):
"""
Compresses and sends the formatted data over UDP
Notes
-----
This is not to be called independently but instead gets called by
the timed thread in `start`
"""
json_str = json.dumps(self.data)
zip_str = zlib.compress(json_str)
self.sock.sendto(zip_str, (self.host, self.port))
return self

def kill(self):
"""
Kills the heartbeat thread
"""
self.running = False
return self

def start(self):
"""
Starts the hearbeat thread
Notes
-----
If the thread has already started, this will throw a RuntimeWarning
"""
if self.running:
raise RuntimeWarning("Heart already running")
else:
Expand Down

0 comments on commit 48124d9

Please sign in to comment.