lib/dbus/message.rb in ruby-dbus-0.17.0 vs lib/dbus/message.rb in ruby-dbus-0.18.0.beta1

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + # dbus.rb - Module containing the low-level D-Bus implementation # # This file is part of the ruby-dbus project # Copyright (C) 2007 Arnaud Cornet and Paul van Tilburg # @@ -25,11 +27,11 @@ # The serial number of the message. @@serial = 1 # Mutex that protects updates on the serial number. @@serial_mutex = Mutex.new # Type of a message (by specification). - MESSAGE_SIGNATURE = "yyyyuua(yv)".freeze + MESSAGE_SIGNATURE = "yyyyuua(yv)" # FIXME: following message type constants should be under Message::Type IMO # well, yeah sure # # Invalid message type. @@ -41,10 +43,13 @@ # Error message type. ERROR = 3 # Signal message type. SIGNAL = 4 + # Names used by signal match rules + TYPE_NAMES = ["invalid", "method_call", "method_return", "error", "signal"].freeze + # Message flag signyfing that no reply is expected. NO_REPLY_EXPECTED = 0x1 # Message flag signifying that no automatic start is required/must be # performed. NO_AUTO_START = 0x2 @@ -64,15 +69,17 @@ attr_accessor :destination # The sender of the message. attr_accessor :sender # The signature of the message contents. attr_accessor :signature - # The serial number of the message this message is a reply for. + # @return [Integer] (u32) + # The serial number of the message this message is a reply for. attr_accessor :reply_serial # The protocol. attr_reader :protocol - # The serial of the message. + # @return [Integer] (u32) + # The serial of the message. attr_reader :serial # The parameters of the message. attr_reader :params # Create a message with message type _mtype_ with default values and a @@ -103,26 +110,32 @@ "serial=#{serial} reply_serial=#{reply_serial} " \ "path=#{path}; interface=#{interface}; member=#{member} " \ "error_name=#{error_name}" end - # Create a regular reply to a message _m_. - def self.method_return(m) - MethodReturnMessage.new.reply_to(m) + # @return [String] name of message type, as used in match rules: + # "method_call", "method_return", "signal", "error" + def message_type_s + TYPE_NAMES[message_type] || "unknown_type_#{message_type}" end - # Create an error reply to a message _m_. - def self.error(m, error_name, description = nil) - ErrorMessage.new(error_name, description).reply_to(m) + # Create a regular reply to a message _msg_. + def self.method_return(msg) + MethodReturnMessage.new.reply_to(msg) end - # Mark this message as a reply to a another message _m_, taking - # the serial number of _m_ as reply serial and the sender of _m_ as + # Create an error reply to a message _msg_. + def self.error(msg, error_name, description = nil) + ErrorMessage.new(error_name, description).reply_to(msg) + end + + # Mark this message as a reply to a another message _msg_, taking + # the serial number of _msg_ as reply serial and the sender of _msg_ as # destination. - def reply_to(m) - @reply_serial = m.serial - @destination = m.sender + def reply_to(msg) + @reply_serial = msg.serial + @destination = msg.sender self end # Add a parameter _val_ of type _type_ to the message. def add_param(type, val) @@ -221,24 +234,24 @@ when SIGNATURE @signature = struct[1] end end pu.align(8) - if @body_length > 0 && @signature + if @body_length.positive? && @signature @params = pu.unmarshall(@signature, @body_length) end [self, pu.idx] end # Make a new exception from ex, mark it as being caused by this message # @api private - def annotate_exception(ex) - new_ex = ex.exception("#{ex}; caused by #{self}") - new_ex.set_backtrace(ex.backtrace) - new_ex + def annotate_exception(exc) + new_exc = exc.exception("#{exc}; caused by #{self}") + new_exc.set_backtrace(exc.backtrace) + new_exc end - end # class Message + end class MethodReturnMessage < Message def initialize super(METHOD_RETURN) end @@ -249,19 +262,19 @@ super(ERROR) @error_name = error_name add_param(Type::STRING, description) unless description.nil? end - def self.from_exception(ex) - name = if ex.is_a? DBus::Error - ex.name + def self.from_exception(exc) + name = if exc.is_a? DBus::Error + exc.name else "org.freedesktop.DBus.Error.Failed" - # ex.class.to_s # RuntimeError is not a valid name, has no dot + # exc.class.to_s # RuntimeError is not a valid name, has no dot end - description = ex.message + description = exc.message msg = new(name, description) - msg.add_param(DBus.type("as"), ex.backtrace) + msg.add_param(DBus.type("as"), exc.backtrace) msg end end -end # module DBus +end