Class: Mosq::Client::Bucket Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mosq/client/bucket.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr) ⇒ Bucket

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Bucket.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mosq/client/bucket.rb', line 7

def initialize(ptr)
  @events    = []
  @callbacks = {}
  
  FFI.mosquitto_connect_callback_set     ptr, new_callback(:on_connect)
  # FFI.mosquitto_disconnect_callback_set  ptr, new_callback(:on_disconnect)
  FFI.mosquitto_publish_callback_set     ptr, new_callback(:on_publish)
  FFI.mosquitto_message_callback_set     ptr, new_callback(:on_message)
  FFI.mosquitto_subscribe_callback_set   ptr, new_callback(:on_subscribe)
  FFI.mosquitto_unsubscribe_callback_set ptr, new_callback(:on_unsubscribe)
  # FFI.mosquitto_log_callback_set         ptr, new_callback(:on_log)
end

Instance Attribute Details

#eventsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
# File 'lib/mosq/client/bucket.rb', line 27

def events
  @events
end

Instance Method Details

#new_callback(symbol) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



20
21
22
23
24
25
# File 'lib/mosq/client/bucket.rb', line 20

def new_callback(symbol)
  # This ensures that callback Procs are retained in the Bucket,
  # and are not garbage-collected for the entire life of the Client.
  # If the callback Procs are garbage-collected then invoked, SIGSEGV!
  @callbacks[symbol] = method(symbol).to_proc
end

#on_connect(ptr, _, status) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mosq/client/bucket.rb', line 29

def on_connect(ptr, _, status)
  @events << {
    type:    :connect,
    status:  status,
    message: case status
             when 0; "connection accepted"
             when 1; "connection refused (unacceptable protocol version)"
             when 2; "connection refused (identifier rejected)"
             when 3; "connection refused (broker unavailable)"
             when 4; "connection refused (bad user name or password)"
             when 5; "connection refused (not authorised)"
             when 6; "connection refused (unknown reason)"
             else    "unknown connection failure"
             end,
  }
end

#on_message(ptr, _, message) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



57
58
59
60
61
62
63
64
65
# File 'lib/mosq/client/bucket.rb', line 57

def on_message(ptr, _, message)
  @events << {
    type:     :message,
    topic:    message[:topic].read_string,
    payload:  message[:payload].read_bytes(message[:payloadlen]),
    retained: message[:retain],
    qos:      message[:qos],
  }
end

#on_publish(ptr, _, packet_id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

end



50
51
52
53
54
55
# File 'lib/mosq/client/bucket.rb', line 50

def on_publish(ptr, _, packet_id)
  @events << {
    type:      :publish,
    packet_id: packet_id,
  }
end

#on_subscribe(ptr, _, packet_id, _, _) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



67
68
69
70
71
72
# File 'lib/mosq/client/bucket.rb', line 67

def on_subscribe(ptr, _, packet_id, _, _)
  @events << {
    type:      :subscribe,
    packet_id: packet_id,
  }
end

#on_unsubscribe(ptr, _, packet_id) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



74
75
76
77
78
79
# File 'lib/mosq/client/bucket.rb', line 74

def on_unsubscribe(ptr, _, packet_id)
  @events << {
    type:      :unsubscribe,
    packet_id: packet_id,
  }
end