From: Erik Andresen Date: Sat, 3 Jan 2015 11:31:56 +0000 (+0100) Subject: json_client now in pyshared X-Git-Url: https://defiant.homedns.org/gitweb/?a=commitdiff_plain;h=6936d35b50b2ecc540d8437d02d0894a093c8130;p=arm_ros_conn.git json_client now in pyshared --- diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..cd8f6f2 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "scripts/pyshared"] + path = scripts/pyshared + url = ssh://erik@vontaene/home/erik_alt/git/pyshared.git diff --git a/scripts/arm_ros_conn.py b/scripts/arm_ros_conn.py index 29462c8..67561b3 100755 --- a/scripts/arm_ros_conn.py +++ b/scripts/arm_ros_conn.py @@ -6,7 +6,7 @@ import tf import prctl #import cProfile import threading -from json_client import JsonClient +from pyshared.json_client import JsonClient from geometry_msgs.msg import Twist from nav_msgs.msg import Odometry diff --git a/scripts/json_client.py b/scripts/json_client.py deleted file mode 100755 index 4e2208e..0000000 --- a/scripts/json_client.py +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env python -# -*- coding: iso-8859-15 -*- - -import sys -import socket -import json - -def set_keepalive(sock, after_idle_sec=1, interval_sec=3, max_fails=5): - """Set TCP keepalive on an open socket. - - It activates after 1 second (after_idle_sec) of idleness, - then sends a keepalive ping once every 3 seconds (interval_sec), - and closes the connection after 5 failed ping (max_fails), or 15 seconds - """ - sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) - sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, after_idle_sec) - sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, interval_sec) - sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, max_fails) -if not hasattr(socket, "set_keepalive"): - socket.set_keepalive = set_keepalive - -class JsonClient: - def __init__(self, addr = ("arm", 10002)): - self.pSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.pSocket.settimeout(1) - socket.set_keepalive(self.pSocket) - self.pSocket.connect(addr) - self.lMsgs = [] - - def write(self, cmd): - data = {'command': cmd} - num = self.pSocket.sendall(json.dumps(data)) - while True: - msg = json.loads(self.pSocket.recv(4096)) - if msg.has_key("return"): return msg["return"] - elif msg.has_key("error"): return msg["error"] - self.lMsgs.insert(0, msg) - - def read(self): - if len(self.lMsgs) > 0: - return self.lMsgs.pop() - self.pSocket.setblocking(False) - try: - return json.loads(self.pSocket.recv(4096)) - except socket.error: - pass - finally: - self.pSocket.setblocking(True) - -if __name__ == "__main__": - print JsonClient().write(sys.argv[1]) - - #from datetime import datetime - #from time import sleep - #pClient = JsonClient() - #while True: - # msg = pClient.read() - # if msg: - # print "Got async", msg - # print datetime.now(), float(pClient.write("get distance forward lower")) - # sleep(0.1) diff --git a/scripts/pyshared b/scripts/pyshared new file mode 160000 index 0000000..d73e753 --- /dev/null +++ b/scripts/pyshared @@ -0,0 +1 @@ +Subproject commit d73e753a4c2c8a9bb79ada177d1cb122a08cabeb