Skip to content

Commit

Permalink
Add bson encoding to the server side
Browse files Browse the repository at this point in the history
  • Loading branch information
DLu committed Jun 24, 2015
1 parent 8e871cb commit d7138dc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import roslib
import rospy

import bson
from rosbridge_library.internal import ros_loader

import math
Expand Down Expand Up @@ -100,7 +100,7 @@ def _from_inst(inst, rostype):
# Special case for uint8[], we base64 encode the string
for binary_type, expression in ros_binary_types_list_braces:
if expression.sub(binary_type, rostype) in ros_binary_types:
return standard_b64encode(inst)
return bson.Binary(inst)

# Check for time or duration
if rostype in ros_time_types:
Expand Down
16 changes: 14 additions & 2 deletions rosbridge_library/src/rosbridge_library/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import rospy
import time

import bson
from rosbridge_library.internal.exceptions import InvalidArgumentException
from rosbridge_library.internal.exceptions import MissingArgumentException

Expand All @@ -47,6 +47,15 @@ def is_number(s):
return True
except ValueError:
return False

def has_binary(d):
if type(d)==bson.Binary:
return True
if type(d)==dict:
for k,v in d.iteritems():
if has_binary(v):
return True
return False

class Protocol:
""" The interface for a single client to interact with ROS.
Expand Down Expand Up @@ -260,7 +269,10 @@ def serialize(self, msg, cid=None):
Returns a JSON string representing the dictionary
"""
try:
return json.dumps(msg)
if has_binary(msg):
return bson.BSON.encode(msg)
else:
return json.dumps(msg)
except:
if cid is not None:
# Only bother sending the log message if there's an id
Expand Down
5 changes: 3 additions & 2 deletions rosbridge_server/src/rosbridge_server/websocket_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

from rosbridge_library.rosbridge_protocol import RosbridgeProtocol
from rosbridge_library.util import json

import bson

class RosbridgeWebSocket(WebSocketHandler):
client_id_seed = 0
Expand Down Expand Up @@ -97,7 +97,8 @@ def on_close(self):
rospy.loginfo("Client disconnected. %d clients total.", cls.clients_connected)

def send_message(self, message):
IOLoop.instance().add_callback(partial(self.write_message, message))
binary = type(message)==bson.BSON
IOLoop.instance().add_callback(partial(self.write_message, message, binary))

def check_origin(self, origin):
return True

0 comments on commit d7138dc

Please sign in to comment.