36 from rosauth.srv
import Authentication
41 from functools
import partial, wraps
43 from tornado
import version_info
as tornado_version_info
44 from tornado.ioloop
import IOLoop
45 from tornado.iostream
import StreamClosedError
46 from tornado.websocket
import WebSocketHandler, WebSocketClosedError
47 from tornado.gen
import coroutine, BadYieldError
54 """Log the most recent exception to ROS.""" 55 exc = traceback.format_exception(*sys.exc_info())
56 rospy.logerr(
''.join(exc))
60 """Decorator for logging exceptions to ROS.""" 62 def wrapper(*args, **kwargs):
64 return f(*args, **kwargs)
75 use_compression =
False 79 fragment_timeout = 600
81 delay_between_messages = 0
82 max_message_size =
None 83 unregister_timeout = 10.0
84 bson_only_mode =
False 88 rospy.logwarn_once(
'The Tornado Rosbridge WebSocket implementation is deprecated.' 89 ' See rosbridge_server.autobahn_websocket' 90 ' and rosbridge_websocket.py')
102 self.set_nodelay(
True)
108 if cls.client_manager:
109 cls.client_manager.add_client(self.
client_id, self.request.remote_ip)
110 except Exception
as exc:
111 rospy.logerr(
"Unable to accept incoming connection. Reason: %s", str(exc))
114 rospy.loginfo(
"Awaiting proper authentication...")
123 msg = bson.BSON(message).decode()
125 msg = json.loads(message)
127 if msg[
'op'] ==
'auth':
129 auth_srv = rospy.ServiceProxy(
'authenticate', Authentication)
130 resp = auth_srv(msg[
'mac'], msg[
'client'], msg[
'dest'],
131 msg[
'rand'], rospy.Time(msg[
't']), msg[
'level'],
132 rospy.Time(msg[
'end']))
135 rospy.loginfo(
"Client %d has authenticated.", self.protocol.client_id)
138 rospy.logwarn(
"Client %d did not authenticate. Closing connection.",
139 self.protocol.client_id)
143 self.protocol.incoming(message)
146 self.protocol.incoming(message)
152 self.protocol.finish()
153 if cls.client_manager:
154 cls.client_manager.remove_client(self.
client_id, self.request.remote_ip)
158 if type(message) == bson.BSON:
160 elif type(message) == bytearray:
162 message = bytes(message)
167 IOLoop.instance().add_callback(partial(self.
prewrite_message, message, binary))
174 future = self.write_message(message, binary)
179 if future
is None and tornado_version_info >= (4,3,0,0):
180 raise WebSocketClosedError
183 except WebSocketClosedError:
184 rospy.logwarn_throttle(1,
'WebSocketClosedError: Tried to write to a closed websocket')
186 except StreamClosedError:
187 rospy.logwarn_throttle(1,
'StreamClosedError: Tried to write to a closed stream')
189 except BadYieldError:
192 if tornado_version_info < (4, 5, 0, 0):
def prewrite_message(self, message, binary)
def check_origin(self, origin)
int delay_between_messages
def get_compression_options(self)
def send_message(self, message)
def on_message(self, message)