From e73ad46348bae50686bbb08b93aaa5c7071261dd Mon Sep 17 00:00:00 2001 From: Alex Wallar Date: Wed, 16 Sep 2015 15:58:09 -0400 Subject: [PATCH] Multicast heartbeat working --- .gitignore | 75 +++++++++++++++++++++++++++++++++++++++++++ src/jammi/__init__.py | 4 +++ src/jammi/heart.py | 45 ++++++++++++++++++++++++++ tests/heart_test.py | 33 +++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100644 .gitignore create mode 100644 src/jammi/heart.py create mode 100644 tests/heart_test.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..54207e5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,75 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] + +tags + +# C extensions +*.so +*.c + +# Distribution / packaging +.Python +env/ +bin/ +build/ +develop-eggs/ +dist/ +eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.cache +nosetests.xml +coverage.xml + +# Translations +*.mo + +# Mr Developer +.mr.developer.cfg +.project +.pydevproject + +# Rope +.ropeproject + +# Django stuff: +*.log +*.pot + +# Sphinx documentation +docs/_build/ + +# Tex +*.bak +*.log +*.aux + +# Ctags +tags + +#Chutsu stuff +run.sh + +scenes/*.out +*.out +scenes/ +octave-workspace +sandbox/ +videos +bags/ diff --git a/src/jammi/__init__.py b/src/jammi/__init__.py index e69de29..3456c0b 100644 --- a/src/jammi/__init__.py +++ b/src/jammi/__init__.py @@ -0,0 +1,4 @@ + +__all__ = ["Heart"] + +from heart import Heart diff --git a/src/jammi/heart.py b/src/jammi/heart.py new file mode 100644 index 0000000..21fb15b --- /dev/null +++ b/src/jammi/heart.py @@ -0,0 +1,45 @@ + +import socket +import json +import zlib +import rospy +import threading + + +class Heart(object): + + def __init__(self, host, port, rate): + self.host = host + self.port = port + self.rate = rate + self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + self.data = {} + self.running = False + + def set_data(self, data): + self.data = data + return self + + def beat(self): + 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): + self.running = False + return self + + def start(self): + if self.running: + raise RuntimeWarning("Heart already running") + else: + def __thread(): + rate = rospy.Rate(self.rate) + while self.running: + self.beat() + rate.sleep() + self.running = True + self.thread = threading.Thread(target=__thread) + self.thread.daemon = True + self.thread.start() diff --git a/tests/heart_test.py b/tests/heart_test.py new file mode 100644 index 0000000..c2c0580 --- /dev/null +++ b/tests/heart_test.py @@ -0,0 +1,33 @@ + +import os +import sys + +sys.path.append(os.path.join(os.path.dirname(__file__), "../src")) + +import jammi +import socket +import rospy +import zlib + + +def test_start(): + rospy.init_node("jammi_test", anonymous=False) + host, port = "localhost", 5005 + rate = 20 + heart = jammi.Heart(host, port, rate) + heart.data["name"] = "test" + heart.start() + + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.bind((host, port)) + i = 0 + while i < 20: + i += 1 + data_zip, addr = sock.recvfrom(1024) + data = zlib.decompress(data_zip) + print i, data, addr + heart.kill() + + +if __name__ == "__main__": + test_start()