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

Raises:

  • (ArgumentError)


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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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)
      uri.host or raise ArgumentError, 'need for a tcp socket uri host.'
      uri.port or raise ArgumentError, 'need for a tcp socket uri port.'
      return TCPSocketAddress.new(unsquare.call(uri.host), uri.port)
    when /\A unix:/x
      uri = URI(config)
      uri.path or raise ArgumentError, 'need for a unix socket uri path.'
      uri.path.empty? and raise ArgumentError, 'empty unix socket uri path.'
      return UNIXSocketAddress.new(uri.path)
    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'] or raise ArgumentError, 'need for a tcp socket host.'
        (host.is_a? String) or raise TypeError, 'not a string tcp scoket host.'
        host.empty? and raise ArgumentError, 'empty tcp socket host.'

        port = config[:port] || config['port'] or raise ArgumentError, 'need for a tcp socket port.'
        case (port)
        when Integer
          # OK
        when String
          service_name = port
          port = Socket.getservbyname(service_name, 'tcp')
        else
          raise TypeError, 'port number is neither an integer nor a service name.'
        end

        if (backlog = config[:backlog] || config['backlog']) then
          (backlog.is_a? Integer) or raise TypeError, 'not a integer tcp socket backlog.'
        end

        return TCPSocketAddress.new(unsquare.call(host), port, backlog)
      when 'unix'
        path = config[:path] || config['path'] or raise ArgumentError, 'need for a unix socket path.'
        (path.is_a? String) or raise TypeError, 'not a string unix socket path.'
        path.empty? and raise ArgumentError, 'empty unix socket path.'

        if (backlog = config[:backlog] || config['backlog']) then
          (backlog.is_a? Integer) or raise TypeError, 'not a integer unix socket backlog.'
        end

        if (mode = config[:mode] || config['mode']) then
          (mode.is_a? Integer) or raise TypeError, 'not a integer socket mode.'
        end

        if (owner = config[:owner] || config['owner']) then
          case (owner)
          when Integer
            # OK
          when String
            owner.empty? and raise ArgumentError, 'empty unix socket owner.'
          else
            raise TypeError, 'unix socket owner is neither an integer nor a string.'
          end
        end

        if (group = config[:group] || config['group']) then
          case (group)
          when Integer
            # OK
          when String
            group.empty? and raise ArgumentError, 'empty unix socket group.'
          else
            raise TypeError, 'unix socket group is neither an integer nor a string.'
          end
        end

        return UNIXSocketAddress.new(path, backlog, mode, owner, group)
      end
    end
  end

  raise ArgumentError, 'invalid socket address.'
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