Class: PahoMqtt::Sender

Inherits:
Object
  • Object
show all
Defined in:
lib/paho_mqtt/sender.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ack_timeout) ⇒ Sender

Returns a new instance of Sender.



20
21
22
23
24
25
26
27
28
# File 'lib/paho_mqtt/sender.rb', line 20

def initialize(ack_timeout)
  @socket          = nil
  @writing_queue   = []
  @publish_queue   = []
  @publish_mutex   = Mutex.new
  @writing_mutex   = Mutex.new
  @last_ping_req   = -1
  @ack_timeout     = ack_timeout
end

Instance Attribute Details

#last_ping_reqObject

Returns the value of attribute last_ping_req.



18
19
20
# File 'lib/paho_mqtt/sender.rb', line 18

def last_ping_req
  @last_ping_req
end

Instance Method Details

#append_to_writing(packet) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/paho_mqtt/sender.rb', line 62

def append_to_writing(packet)
  begin
    if packet.is_a?(PahoMqtt::Packet::Publish)
      prepare_sending(@publish_queue, @publish_mutex, MAX_PUBLISH, packet)
    else
      prepare_sending(@writing_queue, @writing_mutex, MAX_QUEUE, packet)
    end
  rescue FullWritingException
    sleep SELECT_TIMEOUT
    retry
  end
  MQTT_ERR_SUCCESS
end

#check_ack_alive(queue, mutex) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/paho_mqtt/sender.rb', line 111

def check_ack_alive(queue, mutex)
  mutex.synchronize do
    now = Time.now
    queue.each do |pck|
      if now >= pck[:timestamp] + @ack_timeout
        pck[:packet].dup ||= true unless pck[:packet].class == PahoMqtt::Packet::Subscribe || pck[:packet].class == PahoMqtt::Packet::Unsubscribe
        PahoMqtt.logger.info("Acknowledgement timeout is over, resending #{pck[:packet].inspect}") if PahoMqtt.logger?
        send_packet(pck[:packet])
        pck[:timestamp] = now
      end
    end
  end
end

#flush_waiting_packet(sending = true) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/paho_mqtt/sender.rb', line 94

def flush_waiting_packet(sending=true)
  if sending
    @writing_mutex.synchronize do
      @writing_queue.each do |packet|
        send_packet(packet)
      end
    end
    @publish_mutex.synchronize do
      @publish_queue.each do |packet|
        send_packet(packet)
      end
    end
  end
  @writing_queue = []
  @publish_queue = []
end

#prepare_sending(queue, mutex, max_packet, packet) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/paho_mqtt/sender.rb', line 51

def prepare_sending(queue, mutex, max_packet, packet)
  if queue.length < max_packet
    mutex.synchronize do
      queue.push(packet)
    end
  else
    PahoMqtt.logger.error('Writing queue is full, slowing down') if PahoMqtt.logger?
    raise FullWritingException
  end
end

#send_packet(packet) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/paho_mqtt/sender.rb', line 34

def send_packet(packet)
  begin
    @socket.write(packet.to_s) unless @socket.nil? || @socket.closed?
    @last_ping_req = Time.now
    MQTT_ERR_SUCCESS
  end
rescue StandardError
  raise WritingException
rescue IO::WaitWritable
  IO.select(nil, [@socket], nil, SELECT_TIMEOUT)
  retry
end

#send_pingreqObject



47
48
49
# File 'lib/paho_mqtt/sender.rb', line 47

def send_pingreq
  send_packet(PahoMqtt::Packet::Pingreq.new)
end

#socket=(socket) ⇒ Object



30
31
32
# File 'lib/paho_mqtt/sender.rb', line 30

def socket=(socket)
  @socket = socket
end

#writing_loopObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/paho_mqtt/sender.rb', line 76

def writing_loop
  @writing_mutex.synchronize do
    MAX_QUEUE.times do
      break if @writing_queue.empty?
      packet = @writing_queue.shift
      send_packet(packet)
    end
  end
  @publish_mutex.synchronize do
    MAX_PUBLISH.times do
      break if @publish_queue.empty?
      packet = @publish_queue.shift
      send_packet(packet)
    end
  end
  MQTT_ERR_SUCCESS
end