Class: Riser::SocketAddress

Inherits:
Object
  • Object
show all
Defined in:
lib/riser/sockaddr.rb

Direct Known Subclasses

TCPSocketAddress, UNIXSocketAddress

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, backlog = nil) ⇒ SocketAddress

Returns a new instance of SocketAddress.



8
9
10
11
# File 'lib/riser/sockaddr.rb', line 8

def initialize(type, backlog=nil)
  @type = type
  @backlog = backlog
end

Instance Attribute Details

#backlogObject (readonly)

Returns the value of attribute backlog.



14
15
16
# File 'lib/riser/sockaddr.rb', line 14

def backlog
  @backlog
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/riser/sockaddr.rb', line 13

def type
  @type
end

Class Method Details

.parse(config) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/riser/sockaddr.rb', line 42

def self.parse(config)
  unsquare = proc{|s| s.sub(/\A \[/x, '').sub(/\] \z/x, '') }
  case (config)
  when String
    case (config)
    when /\A tcp:/x
      uri = URI(config)
      if (uri.host && uri.port) then
        return TCPSocketAddress.new(unsquare.call(uri.host), uri.port)
      end
    when /\A unix:/x
      uri = URI(config)
      if (uri.path && ! uri.path.empty?) then
        return UNIXSocketAddress.new(uri.path)
      end
    when %r"\A [A-Za-z]+:/"x
      # unknown URI scheme
    when /\A (\S+):(\d+) \z/x
      host = $1
      port = $2.to_i
      return TCPSocketAddress.new(unsquare.call(host), port)
    end
  when Hash
    if (type = config[:type] || config['type']) then
      case (type.to_s)
      when 'tcp'
        host    = config[:host]    || config['host']
        port    = config[:port]    || config['port']
        backlog = config[:backlog] || config['backlog']
        if ((host && (host.is_a? String) && ! host.empty?) &&
            (port && (port.is_a? Integer)) &&
            (backlog.nil? || (backlog.is_a? Integer)))
        then
          return TCPSocketAddress.new(unsquare.call(host), port, backlog)
        end
      when 'unix'
        path    = config[:path]    || config['path']
        backlog = config[:backlog] || config['backlog']
        mode    = config[:mode]    || config['mode']
        owner   = config[:owner]   || config['owner']
        group   = config[:group]   || config['group']
        if ((path && (path.is_a? String) && ! path.empty?) &&
            (backlog.nil? || (backlog.is_a? Integer)) &&
            (mode.nil? || (mode.is_a? Integer)) &&
            (owner.nil? || (owner.is_a? Integer) || ((owner.is_a? String) && ! owner.empty?)) &&
            (group.nil? || (group.is_a? Integer) || ((group.is_a? String) && ! group.empty?)))
        then
          return UNIXSocketAddress.new(path, backlog, mode, owner, group)
        end
      end
    end
  end

  return
end

Instance Method Details

#==(other) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/riser/sockaddr.rb', line 26

def ==(other)
  if (other.is_a? SocketAddress) then
    [ self.to_address, self.to_option ] == [ other.to_address, other.to_option ]
  else
    false
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/riser/sockaddr.rb', line 34

def eql?(other)
  self == other
end

#hashObject



38
39
40
# File 'lib/riser/sockaddr.rb', line 38

def hash
  [ to_address, to_option ].hash ^ self.class.hash
end

#to_addressObject



16
17
18
# File 'lib/riser/sockaddr.rb', line 16

def to_address
  [ @type ]
end

#to_optionObject



20
21
22
23
24
# File 'lib/riser/sockaddr.rb', line 20

def to_option
  option = {}
  option[:backlog] = @backlog if @backlog
  option
end