Class: MqttRails::ConnectionHelper
- Inherits:
-
Object
- Object
- MqttRails::ConnectionHelper
- Defined in:
- lib/mqtt_rails/connection_helper.rb
Instance Attribute Summary collapse
-
#sender ⇒ Object
Returns the value of attribute sender.
Instance Method Summary collapse
- #check_keep_alive(persistent, keep_alive) ⇒ Object
- #clean_start(host, port) ⇒ Object
- #config_socket ⇒ Object
- #do_connect(reconnection = false) ⇒ Object
- #do_disconnect(publisher, explicit, mqtt_thread) ⇒ Object
- #encrypted_socket(tcp_socket, ssl_context) ⇒ Object
- #explicit_disconnect(publisher, mqtt_thread) ⇒ Object
- #handler=(handler) ⇒ Object
- #host=(host) ⇒ Object
-
#initialize(host, port, ssl, ssl_context, ack_timeout) ⇒ ConnectionHelper
constructor
A new instance of ConnectionHelper.
- #is_connected? ⇒ Boolean
- #port=(port) ⇒ Object
- #send_connect(session_params) ⇒ Object
- #send_disconnect ⇒ Object
- #setup_connection ⇒ Object
-
#should_send_ping?(now, keep_alive, last_packet_received_at) ⇒ Boolean
Would return ‘true’ if ping requset should be sent and ‘nil’ if not.
Constructor Details
#initialize(host, port, ssl, ssl_context, ack_timeout) ⇒ ConnectionHelper
Returns a new instance of ConnectionHelper.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/mqtt_rails/connection_helper.rb', line 22 def initialize(host, port, ssl, ssl_context, ack_timeout) @cs = MQTT_CS_DISCONNECT @socket = nil @host = host @port = port @ssl = ssl @ssl_context = ssl_context @ack_timeout = ack_timeout @sender = Sender.new(ack_timeout) end |
Instance Attribute Details
#sender ⇒ Object
Returns the value of attribute sender.
20 21 22 |
# File 'lib/mqtt_rails/connection_helper.rb', line 20 def sender @sender end |
Instance Method Details
#check_keep_alive(persistent, keep_alive) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/mqtt_rails/connection_helper.rb', line 156 def check_keep_alive(persistent, keep_alive) now = Time.now last_packet_received_at = @handler.last_packet_received_at # send a PINGREQ only if we don't already wait for a PINGRESP if persistent && should_send_ping?(now, keep_alive, last_packet_received_at) Rails.logger.info("Checking if server is still alive...") @sender.send_pingreq end disconnect_timeout_at = last_packet_received_at + (keep_alive * 1.1).ceil if disconnect_timeout_at <= now Rails.logger.info("No activity is over timeout, disconnecting from #{@host}.") @cs = MQTT_CS_DISCONNECT end @cs end |
#clean_start(host, port) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/mqtt_rails/connection_helper.rb', line 105 def clean_start(host, port) self.host = host self.port = port unless @socket.nil? @socket.close unless @socket.closed? @socket = nil end end |
#config_socket ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/mqtt_rails/connection_helper.rb', line 80 def config_socket Rails.logger.info("Attempt to connect to host: #{@host}...") begin tcp_socket = TCPSocket.new(@host, @port) if @ssl encrypted_socket(tcp_socket, @ssl_context) else @socket = tcp_socket end rescue StandardError Rails.logger.warn("Could not open a socket with #{@host} on port #{@port}.") end end |
#do_connect(reconnection = false) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/mqtt_rails/connection_helper.rb', line 37 def do_connect(reconnection=false) @cs = MQTT_CS_NEW @handler.socket = @socket # Waiting a Connack packet for "ack_timeout" second from the remote connect_timeout = Time.now + @ack_timeout while (Time.now <= connect_timeout) && !is_connected? do @cs = @handler.receive_packet end unless is_connected? Rails.logger.warn("Connection failed. Couldn't recieve a Connack packet from: #{@host}.") raise Exception.new("Connection failed. Check log for more details.") unless reconnection end @cs end |
#do_disconnect(publisher, explicit, mqtt_thread) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/mqtt_rails/connection_helper.rb', line 56 def do_disconnect(publisher, explicit, mqtt_thread) Rails.logger.info("Disconnecting from #{@host}.") if explicit explicit_disconnect(publisher, mqtt_thread) end @socket.close unless @socket.nil? || @socket.closed? @socket = nil end |
#encrypted_socket(tcp_socket, ssl_context) ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/mqtt_rails/connection_helper.rb', line 94 def encrypted_socket(tcp_socket, ssl_context) unless ssl_context.nil? @socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ssl_context) @socket.sync_close = true @socket.connect else Rails.logger.error("The SSL context was found as nil while the socket's opening.") raise Exception end end |
#explicit_disconnect(publisher, mqtt_thread) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/mqtt_rails/connection_helper.rb', line 65 def explicit_disconnect(publisher, mqtt_thread) @sender.flush_waiting_packet(false) send_disconnect mqtt_thread.kill if mqtt_thread && mqtt_thread.alive? publisher.flush_publisher unless publisher.nil? end |
#handler=(handler) ⇒ Object
33 34 35 |
# File 'lib/mqtt_rails/connection_helper.rb', line 33 def handler=(handler) @handler = handler end |
#host=(host) ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/mqtt_rails/connection_helper.rb', line 114 def host=(host) if host.nil? || host == "" Rails.logger.error("The host was found as nil while the connection setup.") raise ArgumentError else @host = host end end |
#is_connected? ⇒ Boolean
52 53 54 |
# File 'lib/mqtt_rails/connection_helper.rb', line 52 def is_connected? @cs == MQTT_CS_CONNECTED end |
#port=(port) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/mqtt_rails/connection_helper.rb', line 123 def port=(port) if port.to_i <= 0 Rails.logger.error("The port value is invalid (<= 0). Could not setup the connection.") raise ArgumentError else @port = port end end |
#send_connect(session_params) ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/mqtt_rails/connection_helper.rb', line 132 def send_connect(session_params) setup_connection packet = MqttRails::Packet::Connect.new(session_params) @handler.clean_session = session_params[:clean_session] @sender.send_packet(packet) MQTT_ERR_SUCCESS end |
#send_disconnect ⇒ Object
140 141 142 143 144 |
# File 'lib/mqtt_rails/connection_helper.rb', line 140 def send_disconnect packet = MqttRails::Packet::Disconnect.new @sender.send_packet(packet) MQTT_ERR_SUCCESS end |
#setup_connection ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/mqtt_rails/connection_helper.rb', line 72 def setup_connection clean_start(@host, @port) config_socket unless @socket.nil? @sender.socket = @socket end end |
#should_send_ping?(now, keep_alive, last_packet_received_at) ⇒ Boolean
Would return ‘true’ if ping requset should be sent and ‘nil’ if not
147 148 149 150 151 152 153 154 |
# File 'lib/mqtt_rails/connection_helper.rb', line 147 def should_send_ping?(now, keep_alive, last_packet_received_at) last_pingreq_sent_at = @sender.last_pingreq_sent_at last_pingresp_received_at = @handler.last_pingresp_received_at if !last_pingreq_sent_at || (last_pingresp_received_at && (last_pingreq_sent_at <= last_pingresp_received_at)) next_pingreq_at = [@sender.last_packet_sent_at, last_packet_received_at].min + (keep_alive * 0.7).ceil return next_pingreq_at <= now end end |