Skip to content

Commit

Permalink
Transport layer is wokring better now
Browse files Browse the repository at this point in the history
  • Loading branch information
a20r committed Jan 31, 2016
1 parent 572c104 commit 05a4ebb
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 249 deletions.
5 changes: 0 additions & 5 deletions src/client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@

__all__ = ["Heart", "NameServer"]

from heart import Heart
from nameserver import NameServer
91 changes: 0 additions & 91 deletions src/client/heart.py

This file was deleted.

146 changes: 0 additions & 146 deletions src/client/nameserver.py

This file was deleted.

68 changes: 68 additions & 0 deletions src/client/ws.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

import threading
import string
import sys
from twisted.internet import reactor
from autobahn.twisted.websocket import WebSocketClientProtocol
from autobahn.twisted.websocket import WebSocketClientFactory


test_msg = """
{"to": "$to",
"from": "$fr",
"topic": "/foo/state",
"type": "geometry_msgs/Point",
"msg": "{'x': 0, 'y': 0, 'z': 0}",
"stamp": $t}
"""


class MMClient(WebSocketClientProtocol):

client = None

def onConnect(self, reponse):
MMClient.client = self

def onMessage(self, payload, is_binary):
if not is_binary:
print payload

@staticmethod
def send_message(payload):
if not MMClient.client is None:
MMClient.client.sendMessage(payload)


class Connection(threading.Thread):
def __init__(self, host, port, name):
super(Connection, self).__init__()
self.host = host
self.port = port
self.name = name
self.url = "ws://{}:{}/{}".format(host, port, name)
self.factory = WebSocketClientFactory(self.url, debug=True)

def run(self):
self.factory.protocol = MMClient
reactor.connectTCP(self.host, self.port, self.factory)
reactor.run(installSignalHandlers=0)

def stop(self):
reactor.stop()

def send_message(self, payload):
return MMClient.send_message(payload)


if __name__ == "__main__":
import time
conn = Connection("localhost", 9000, sys.argv[1])
conn.daemon = True
conn.start()
msg_tmp = string.Template(test_msg)
while True:
msg = msg_tmp.substitute(
fr=sys.argv[1], to=sys.argv[2], t=time.time())
conn.send_message(msg)
time.sleep(0.1)
13 changes: 8 additions & 5 deletions src/server/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ def onOpen(self):

def onMessage(self, payload, is_binary):
if not is_binary:
msg = json.loads(payload)
if msg["to"] == "*":
for name in common.clients.keys():
try:
msg = json.loads(payload)
if msg["to"] == "*":
for name in common.clients.keys():
common.get_client(msg["to"]).sendMessage(payload)
else:
common.get_client(msg["to"]).sendMessage(payload)
else:
common.get_client(msg["to"]).sendMessage(payload)
except KeyError:
pass

def onClose(self, was_clean, code, reason):
common.remove_client(self.name_of_client)
4 changes: 2 additions & 2 deletions tests/ws_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def create_websocket():

def test_ws_server():
log.startLogging(sys.stdout)
create_websocket()
# create_websocket()
factory = WebSocketServerFactory("ws://localhost:9000", debug=True)
factory.protocol = server.MMServerProtocol
reactor.listenTCP(9000, factory)
task.LoopingCall(print_conns).start(1.0)
# task.LoopingCall(print_conns).start(1.0)
reactor.run()

0 comments on commit 05a4ebb

Please sign in to comment.