Module: Mosq::Util Private

Defined in:
lib/mosq/util.rb

Overview

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

Helper functions for this library.

Class Method Summary collapse

Class Method Details

.connection_info(uri = nil, **overrides) ⇒ 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.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/mosq/util.rb', line 45

def connection_info(uri=nil, **overrides)
  info = {
    ssl:  false,
    host: "localhost",
    port: 1883,
  }
  if uri
    # TODO: support IPv6
    pattern = %r{\A(?<schema>mqtts?)://((?<username>[^:@]+)(:(?<password>[^@]+))?@)?(?<host>[^:]+)(:(?<port>\d+))?\Z}
    match = pattern.match(uri)
    if match
      info[:ssl]  = ("mqtts" == match[:schema])
      info[:host] = match[:host]
      info[:port] = match[:port] ? Integer(match[:port]) : (info[:ssl] ? 8883 : 1883)
      info[:username] = match[:username] if match[:username]
      info[:password] = match[:password] if match[:password]
    else
      info[:host] = uri
    end
  end
  info.merge(overrides)
end

.const_name(lowercase_name) ⇒ 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.



9
10
11
# File 'lib/mosq/util.rb', line 9

def const_name(lowercase_name)
  lowercase_name.to_s.gsub(/((?:\A\w)|(?:_\w))/) { |x| x[-1].upcase }
end

.error_check(action, 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.

Raises:



13
14
15
16
# File 'lib/mosq/util.rb', line 13

def error_check(action, status)
  return if status == :success
  raise Mosq::FFI::Error.lookup(status), "while #{action}"
end

.mem_ptr(size, count: 1, clear: true, release: true) ⇒ 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.



23
24
25
26
27
# File 'lib/mosq/util.rb', line 23

def mem_ptr(size, count: 1, clear: true, release: true)
  ptr = ::FFI::MemoryPointer.new(size, count, clear)
  ptr.autorelease = false unless release
  ptr
end

.null_check(action, obj) ⇒ 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.

Raises:



18
19
20
21
# File 'lib/mosq/util.rb', line 18

def null_check(action, obj)
  return unless obj.nil?
  raise Mosq::FFI::Error, "while #{action} - got unexpected null"
end

.strdup_ary_ptr(ary, **kwargs) ⇒ 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.



36
37
38
39
40
41
42
43
# File 'lib/mosq/util.rb', line 36

def strdup_ary_ptr(ary, **kwargs)
  ptr = mem_ptr(:pointer, count: ary.size)
  ary.each_with_index do |str, i|
    cursor = (ptr + i * ::FFI::TypeDefs[:pointer].size)
    cursor.write_pointer(strdup_ptr(str, **kwargs))
  end
  ptr
end

.strdup_ptr(str, **kwargs) ⇒ 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
# File 'lib/mosq/util.rb', line 29

def strdup_ptr(str, **kwargs)
  str = str + "\x00"
  ptr = mem_ptr(str.bytesize, **kwargs)
  ptr.write_string(str)
  ptr
end