Class: ZMQMachine::Address

Inherits:
Object
  • Object
show all
Defined in:
lib/zm/address.rb

Overview

A simple wrapper for creating transport strings for the 0mq library to use for bind and connect calls.

It is recommended to use Address.create instead of calling #new directly on the class. Using this factory method allows user code to avoid creating a begin/rescue/end structure and dealing with exceptions. Failed creation will return a nil value.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, type = :tcp) ⇒ Address

type : :tcp, :pgm or :inprocess



51
52
53
54
55
# File 'lib/zm/address.rb', line 51

def initialize host, port, type = :tcp
  @host = host
  @port = port
  @transport = determine_type type
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



48
49
50
# File 'lib/zm/address.rb', line 48

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



48
49
50
# File 'lib/zm/address.rb', line 48

def port
  @port
end

#transportObject (readonly)

Returns the value of attribute transport.



48
49
50
# File 'lib/zm/address.rb', line 48

def transport
  @transport
end

Class Method Details

.create(host, port, type = :tcp) ⇒ Object

Recommended to use this class method as a factory versus calling #new directly. Returns an Address object upon successful creation and nil if creation fails.



70
71
72
73
74
75
76
77
78
79
# File 'lib/zm/address.rb', line 70

def self.create host, port, type = :tcp
  address = nil
  begin
    address = Address.new host, port, type
  rescue UnknownAddressError
    address = nil
  end
  
  address
end

.from_string(string) ⇒ Object

Converts strings with the format “type://host:port” into an Address instance.



85
86
87
88
89
90
91
92
93
# File 'lib/zm/address.rb', line 85

def self.from_string string
  # should also return nil or some other error indication when parsing fails
  split = string.split(':')
  type = split[0]
  port = split[2] # nil for ipc/inproc and non-empty for tcp
  host = split[1].slice(2, split[1].length) #sub('//', '')

  Address.create host, port, type.downcase.to_sym
end

Instance Method Details

#to_sObject



57
58
59
60
61
62
63
64
# File 'lib/zm/address.rb', line 57

def to_s
  case @transport
  when :tcp
    "#{@transport}://#{@host}:#{@port}"
  else
    "#{@transport}://#{@host}"
  end
end