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
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
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
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
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
|