~ubuntu-branches/ubuntu/utopic/libdbus-ruby/utopic

« back to all changes in this revision

Viewing changes to lib/dbus/matchrule.rb

  • Committer: Bazaar Package Importer
  • Author(s): Paul van Tilburg
  • Date: 2007-04-25 23:16:04 UTC
  • Revision ID: james.westby@ubuntu.com-20070425231604-yda2ta1tq35s898n
Tags: upstream-0.2.0
ImportĀ upstreamĀ versionĀ 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# This file is part of the ruby-dbus project
 
2
# Copyright (C) 2007 Arnaud Cornet and Paul van Tilburg
 
3
#
 
4
# This library is free software; you can redistribute it and/or
 
5
# modify it under the terms of the GNU Lesser General Public
 
6
# License, version 2.1 as published by the Free Software Foundation.
 
7
# See the file "COPYING" for the exact licensing terms.
 
8
 
 
9
module DBus
 
10
  # Exception raised when an erroneous match rule type is encountered.
 
11
  class MatchRuleException < Exception
 
12
  end
 
13
 
 
14
  # = D-Bus match rule class
 
15
  #
 
16
  # FIXME
 
17
  class MatchRule
 
18
    # The list of possible match filters.
 
19
    FILTERS = [:sender, :interface, :member, :path, :destination, :type]
 
20
    # The sender filter.
 
21
    attr_accessor :sender
 
22
    # The interface filter.
 
23
    attr_accessor :interface
 
24
    # The member filter.
 
25
    attr_accessor :member
 
26
    # The path filter.
 
27
    attr_accessor :path
 
28
    # The destination filter.
 
29
    attr_accessor :destination
 
30
    # The type type that is matched.
 
31
    attr_reader :type
 
32
 
 
33
    # Set the message types to filter to type _t_.
 
34
    # Possible message types are: signal, method_call, method_return, and error.
 
35
    def type=(t)
 
36
      if not ['signal', 'method_call', 'method_return', 'error'].member?(t)
 
37
        raise MatchRuleException 
 
38
      end
 
39
      @type = t
 
40
    end
 
41
 
 
42
    # Returns a match rule string version of the object.
 
43
    # E.g.:  "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='Foo',path='/bar/foo',destination=':452345.34',arg2='bar'"
 
44
    def to_s
 
45
      FILTERS.select do |sym|
 
46
        not method(sym).call.nil?
 
47
      end.collect do |sym|
 
48
        "#{sym.to_s}='#{method(sym).call}'"
 
49
      end.join(",")
 
50
    end
 
51
 
 
52
    # Parses a match rule string _s_ and sets the filters on the object.
 
53
    def from_s(str)
 
54
      s.split(",").each do |eq|
 
55
        if eq =~ /^(.*)='([^']*)'$/
 
56
          name = $1
 
57
          val = $1
 
58
          if FILTERS.member?(name.to_sym)
 
59
            method(name + "=").call(val)
 
60
          else
 
61
            raise MatchRuleException 
 
62
          end
 
63
        end
 
64
      end
 
65
    end
 
66
 
 
67
    # Sets the match rule to filter for the given _signal_ and the
 
68
    # given interface _intf_.
 
69
    def from_signal(intf, signal)
 
70
      signal = signal.name unless signal.is_a?(String)
 
71
      self.type = "signal"
 
72
      self.interface = intf.name
 
73
      self.member = signal
 
74
      self.path = intf.object.path
 
75
      self
 
76
    end
 
77
 
 
78
    # Determines whether a message _msg_ matches the match rule.
 
79
    def match(msg)
 
80
      if @type
 
81
        if {Message::SIGNAL => "signal", Message::METHOD_CALL => "method_call",
 
82
          Message::METHOD_RETURN => "method_return",
 
83
          Message::ERROR => "error"}[msg.message_type] != @type
 
84
          return false
 
85
        end
 
86
      end
 
87
      return false if @interface and @interface != msg.interface
 
88
      return false if @member and @member != msg.member
 
89
      return false if @path and @path != msg.path
 
90
      true
 
91
    end
 
92
  end # class MatchRule
 
93
end # module D-Bus