Skip to content

Commit

Permalink
Multicast heartbeat working
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Wallar committed Sep 16, 2015
1 parent 68e6a8b commit e73ad46
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/
4 changes: 4 additions & 0 deletions src/jammi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

__all__ = ["Heart"]

from heart import Heart
45 changes: 45 additions & 0 deletions src/jammi/heart.py
Original file line number Diff line number Diff line change
@@ -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()
33 changes: 33 additions & 0 deletions tests/heart_test.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit e73ad46

Please sign in to comment.