Class: Mqlight::Destination

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/mqlight/destination.rb

Overview

Internal class used to store a record of a destination a client is client is subscribed to. Should not be used externally.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

#initialize(service, topic_pattern, options = {}) ⇒ Destination

Returns a new instance of Destination.

Parameters:

  • service (Service)
  • topic_pattern (String)
  • options (Map) (defaults to: {})


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mqlight/destination.rb', line 42

def initialize(service, topic_pattern, options = {})
  logger.entry(@id) { self.class.to_s + '#' + __method__.to_s }
  parms = Hash[method(__method__).parameters.map do |parm|
    [parm[1], eval(parm[1].to_s)]
  end]
  logger.parms(@id, parms) { self.class.to_s + '#' + __method__.to_s }

  fail ArgumentError,
       'service must be a Service object.' unless service.is_a? Service
  @service = service

  fail ArgumentError,
       'topic_pattern must be a String.' unless topic_pattern.is_a? String
  @topic_pattern = topic_pattern

  if options.is_a? Hash
    @share = options.fetch(:share, nil)
    @qos = options.fetch(:qos, nil)
    @ttl = options.fetch(:ttl, nil)
    @auto_confirm = options.fetch(:auto_confirm, nil)
  else
    fail ArgumentError,
         'options must be a Hash or nil.' unless options.nil?
  end

  unless @ttl.nil?
    fail ArgumentError, 'ttl must be an unsigned Integer.' unless
      @ttl.is_a?(Integer) && @ttl >= 0
    @ttl = 4_294_967_295 if @ttl > 4_294_967_295
  end

  # Defaults
  @qos ||= QOS_AT_MOST_ONCE
  @ttl ||= 0
  @auto_confirm = true if @auto_confirm.nil?

  fail ArgumentError, 'auto_confirm must be a boolean.' unless
    @auto_confirm.class == TrueClass || @auto_confirm.class == FalseClass

  fail ArgumentError, "qos value #{@qos} " \
    ' is invalid must evaluate to 0 or 1.' unless
         @qos == QOS_AT_MOST_ONCE || @qos == QOS_AT_LEAST_ONCE

  @ttl = (@ttl / 1000).round

  # Validate share
  fail ArgumentError, 'share must be a String or nil.' unless
    @share.is_a?(String) || @share.nil?
  if @share.is_a? String
    fail ArgumentError,
         'share is invalid because it contains a colon (:) character' if
      @share.include? ':'
    uri_share = 'share:' + @share + ':'
  else
    uri_share = 'private:'
  end
  @address = @service.address + '/' + uri_share + @topic_pattern

  # Set defaults
  @unconfirmed = 0
  @confirmed = 0
rescue => e
  logger.throw(nil, e) { self.class.to_s + '#' + __method__.to_s }
  raise e
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



29
30
31
# File 'lib/mqlight/destination.rb', line 29

def address
  @address
end

#auto_confirmObject (readonly)

Returns the value of attribute auto_confirm.



35
36
37
# File 'lib/mqlight/destination.rb', line 35

def auto_confirm
  @auto_confirm
end

#qosObject (readonly)

Returns the value of attribute qos.



33
34
35
# File 'lib/mqlight/destination.rb', line 33

def qos
  @qos
end

#serviceObject (readonly)

Returns the value of attribute service.



30
31
32
# File 'lib/mqlight/destination.rb', line 30

def service
  @service
end

#shareObject (readonly)

Returns the value of attribute share.



32
33
34
# File 'lib/mqlight/destination.rb', line 32

def share
  @share
end

#topic_patternObject (readonly)

Returns the value of attribute topic_pattern.



31
32
33
# File 'lib/mqlight/destination.rb', line 31

def topic_pattern
  @topic_pattern
end

#ttlObject (readonly)

Returns the value of attribute ttl.



34
35
36
# File 'lib/mqlight/destination.rb', line 34

def ttl
  @ttl
end

Instance Method Details

#match?(topic_pattern, share) ⇒ Boolean

Returns true if the topic pattern and share match.

Returns:

  • (Boolean)

    true if the topic pattern and share match.



110
111
112
# File 'lib/mqlight/destination.rb', line 110

def match?(topic_pattern, share)
  (@topic_pattern.eql? topic_pattern) && (@share.eql? share)
end

#to_sObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mqlight/destination.rb', line 114

def to_s
  info = '{'
  info << 'address: ' +
    @address.to_s.force_encoding('UTF-8') unless @address.nil?
  info << ', service: ' +
    @service.to_s.force_encoding('UTF-8') unless @service.nil?
  info << ', topic_pattern: ' +
    @topic_pattern.to_s.force_encoding('UTF-8') unless @topic_pattern.nil?
  info << ', share: ' + @share.to_s unless @share.nil?
  info << ', qos: ' + @qos.to_s unless @qos.nil?
  info << ', ttl: ' + @ttl.to_s unless @ttl.nil?
  info << ', auto_confirm: ' + @auto_confirm.to_s unless @auto_confirm.nil?
  info << '}'
end