36 from rosauth.srv
import Authentication
41 from functools
import wraps
43 from autobahn.twisted.websocket
import WebSocketServerProtocol
44 from twisted.internet
import interfaces, reactor
45 from zope.interface
import implementer
52 """Log the most recent exception to ROS.""" 53 exc = traceback.format_exception(*sys.exc_info())
54 rospy.logerr(
''.join(exc))
58 """Decorator for logging exceptions to ROS.""" 60 def wrapper(*args, **kwargs):
62 return f(*args, **kwargs)
69 @implementer(interfaces.IPushProducer)
71 """Allows the Autobahn transport to pause outgoing messages from rosbridge. 73 The purpose of this valve is to connect backpressure from the WebSocket client 74 back to the rosbridge protocol, which depends on backpressure for queueing. 75 Without this flow control, rosbridge will happily keep writing messages to 76 the WebSocket until the system runs out of memory. 78 This valve is closed and opened automatically by the Twisted TCP server. 79 In practice, Twisted should only close the valve when its userspace write buffer 80 is full and it should only open the valve when that buffer is empty. 82 When the valve is closed, the rosbridge protocol instance's outgoing writes 83 must block until the valve is opened. 95 reactor.callFromThread(self._proto.outgoing, message)
111 clients_connected = 0
116 fragment_timeout = 600
118 delay_between_messages = 0
119 max_message_size =
None 120 unregister_timeout = 10.0
121 bson_only_mode =
False 135 self.transport.registerProducer(producer,
True)
136 producer.resumeProducing()
137 self.protocol.outgoing = producer.relay
142 self.
peer = self.transport.getPeer().host
143 if cls.client_manager:
146 except Exception
as exc:
147 rospy.logerr(
"Unable to accept incoming connection. Reason: %s", str(exc))
150 rospy.loginfo(
"Awaiting proper authentication...")
155 message = message.decode(
'utf-8')
160 msg = bson.BSON(message).decode()
162 msg = json.loads(message)
164 if msg[
'op'] ==
'auth':
166 auth_srv = rospy.ServiceProxy(
'authenticate', Authentication)
167 resp = auth_srv(msg[
'mac'], msg[
'client'], msg[
'dest'],
168 msg[
'rand'], rospy.Time(msg[
't']), msg[
'level'],
169 rospy.Time(msg[
'end']))
172 rospy.loginfo(
"Client %d has authenticated.", self.protocol.client_id)
175 rospy.logwarn(
"Client %d did not authenticate. Closing connection.",
176 self.protocol.client_id)
180 self.protocol.incoming(message)
183 self.protocol.incoming(message)
186 if type(message) == bson.BSON:
188 message = bytes(message)
189 elif type(message) == bytearray:
191 message = bytes(message)
194 message = message.encode(
'utf-8')
196 self.sendMessage(message, binary)
199 if not hasattr(self,
'protocol'):
203 self.protocol.finish()
204 if cls.client_manager:
def __init__(self, proto)
def outgoing(self, message)
def resumeProducing(self)
int delay_between_messages
def onMessage(self, message, binary)
def onClose(self, was_clean, code, reason)