def deserialize(self, msg, cid=None): """ Turns the wire-level representation into a dictionary of values Default behaviour assumes JSON. Override to use a different container. Keyword arguments: msg -- the wire-level message to deserialize cid -- (optional) an ID associated with this. Is logged on error Returns a dictionary of values """ try: if self.bson_only_mode: bson_message = bson.BSON(msg) return bson_message.decode() else: return json.loads(msg) except Exception as e: # if we did try to deserialize whole buffer .. first try to let self.incoming check for multiple/partial json-decodes before logging error # .. this means, if buffer is not == msg --> we tried to decode part of buffer # TODO: implement a way to have a final Exception when nothing works out to decode (multiple/broken/partial JSON..) # supressed logging of exception on json-decode to keep rosbridge-logs "clean", otherwise console logs would get spammed for every failed json-decode try # if msg != self.buffer: # error_msg = "Unable to deserialize message from client: %s" % msg # error_msg += "\nException was: " +str(e) # # self.log("error", error_msg, cid) # re-raise Exception to allow handling outside of deserialize function instead of returning None raise
def on_message(self, message): cls = self.__class__ # check if we need to authenticate if cls.authenticate and not self.authenticated: try: if cls.bson_only_mode: msg = bson.BSON(message).decode() else: msg = json.loads(message) if msg['op'] == 'auth': # check the authorization information auth_srv = rospy.ServiceProxy('authenticate', Authentication) resp = auth_srv(msg['mac'], msg['client'], msg['dest'], msg['rand'], rospy.Time(msg['t']), msg['level'], rospy.Time(msg['end'])) self.authenticated = resp.authenticated if self.authenticated: rospy.loginfo("Client %d has authenticated.", self.protocol.client_id) return # if we are here, no valid authentication was given rospy.logwarn("Client %d did not authenticate. Closing connection.", self.protocol.client_id) self.close() except: # proper error will be handled in the protocol class self.protocol.incoming(message) else: # no authentication required self.protocol.incoming(message)
def on_message(self, message): cls = self.__class__ # check if we need to authenticate if cls.authenticate and not self.authenticated: try: if cls.bson_only_mode: msg = bson.BSON(message).decode() else: msg = json.loads(message) if msg['op'] == 'auth': # check the authorization information auth_srv_client = cls.node_handle.create_client( Authentication, 'authenticate') auth_srv_req = Authentication.Request() auth_srv_req.mac = msg['mac'] auth_srv_req.client = msg['client'] auth_srv_req.dest = msg['dest'] auth_srv_req.rand = msg['rand'] auth_srv_req.t = Time(seconds=msg['t']).to_msg() auth_srv_req.level = msg['level'] auth_srv_req.end = Time(seconds=msg['end']).to_msg() while not auth_srv_client.wait_for_service( timeout_sec=1.0): cls.node_handle.get_logger().info( 'Authenticate service not available, waiting again...' ) future = auth_srv_client.call_async(auth_srv_req) rclpy.spin_until_future_complete(cls.node_handle, future) # Log error if service could not be called. if future.result() is not None: self.authenticated = future.result().authenticated else: self.authenticated = False cls.node_handle.get_logger.error( 'Authenticate service call failed') if self.authenticated: cls.node_handle.get_logger().info( "Client {} has authenticated.".format( self.protocol.client_id)) return # if we are here, no valid authentication was given cls.node_handle.get_logger().warn( "Client {} did not authenticate. Closing connection.". format(self.protocol.client_id)) self.close() except: # proper error will be handled in the protocol class self.protocol.incoming(message) else: # no authentication required self.protocol.incoming(message)