ionian 0.6.4 → 0.6.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,7 +14,7 @@ module Ionian
14
14
  module Socket
15
15
 
16
16
  # Called automaticallly when the object is extended with #extend.
17
- def self.extended(obj)
17
+ def self.extended obj
18
18
  obj.extend Ionian::Extension::IO
19
19
  obj.initialize_ionian_socket
20
20
  end
@@ -24,6 +24,44 @@ module Ionian
24
24
  def initialize_ionian_socket
25
25
  end
26
26
 
27
+ # Returns true if sending broadcast datagrams is permitted.
28
+ # ( SO_BROADCAST )
29
+ def broadcast
30
+ param = self.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST)
31
+ .data.unpack('i').first
32
+ param > 0 ? true : false
33
+ end
34
+
35
+ # Permit sending broadcast datagrams if true.
36
+ # ( SO_BROADCAST )
37
+ def broadcast= value
38
+ param = value ? 1 : 0
39
+ self.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_BROADCAST, [param].pack('i')
40
+ end
41
+
42
+ alias_method :broadcast?, :broadcast
43
+
44
+ # For connection-oriented protocols, prevent #close from returning
45
+ # immediately and try to deliver any data in the send buffer if value
46
+ # is true.
47
+ # ( SO_LINGER )
48
+ def linger
49
+ param = self.getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_LINGER)
50
+ .data.unpack('i').first
51
+ param > 0 ? true : false
52
+ end
53
+
54
+ # For connection-oriented protocols, prevent #close from returning
55
+ # immediately and try to deliver any data in the send buffer if value
56
+ # is true.
57
+ # ( SO_LINGER )
58
+ def linger= value
59
+ param = value ? 1 : 0
60
+ self.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_LINGER, [param].pack('i')
61
+ end
62
+
63
+ alias_method :linger?, :linger
64
+
27
65
  # Returns true if local address reuse is allowed.
28
66
  # ( SO_REUSEADDR )
29
67
  def reuse_addr
@@ -36,7 +74,7 @@ module Ionian
36
74
 
37
75
  # Allows local address reuse if true.
38
76
  # ( SO_REUSEADDR )
39
- def reuse_addr=(value)
77
+ def reuse_addr= value
40
78
  param = value ? 1 : 0
41
79
  self.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, [param].pack('i')
42
80
  end
@@ -52,7 +90,7 @@ module Ionian
52
90
 
53
91
  # Sets the time to live (hop limit).
54
92
  # ( IP_TTL )
55
- def ttl=(value)
93
+ def ttl= value
56
94
  self.setsockopt ::Socket::IPPROTO_IP, ::Socket::IP_TTL, [value].pack('i')
57
95
  end
58
96
 
@@ -68,7 +106,7 @@ module Ionian
68
106
 
69
107
  # Disables the Nagle algorithm if true.
70
108
  # ( TCP_NODELAY )
71
- def no_delay=(value)
109
+ def no_delay= value
72
110
  param = value ? 1 : 0
73
111
  self.setsockopt ::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, [param].pack('i')
74
112
  end
@@ -93,7 +131,7 @@ module Ionian
93
131
  # See #recork.
94
132
  # Linux only.
95
133
  # ( TCP_CORK )
96
- def cork=(value)
134
+ def cork= value
97
135
  param = value ? 1 : 0
98
136
  self.setsockopt ::Socket::IPPROTO_TCP, ::Socket::TCP_CORK, [param].pack('i')
99
137
  end
@@ -111,7 +149,7 @@ module Ionian
111
149
  # Interface is the local network interface to receive the
112
150
  # multicast traffic on (all interfaces if not specified).
113
151
  # ( IP_ADD_MEMBERSHIP )
114
- def ip_add_membership(address = nil, interface = nil)
152
+ def ip_add_membership address = nil, interface = nil
115
153
  address ||= self.remote_address.ip_address
116
154
  interface ||= '0.0.0.0'
117
155
 
@@ -127,7 +165,7 @@ module Ionian
127
165
  # Interface is the local network interface the multicast
128
166
  # traffic is received on (all interfaces if not specified).
129
167
  # ( IP_DROP_MEMBERSHIP )
130
- def ip_drop_membership(address = nil, interface = nil)
168
+ def ip_drop_membership address = nil, interface = nil
131
169
  address ||= self.remote_address.ip_address
132
170
  interface ||= '0.0.0.0'
133
171
 
@@ -146,7 +184,7 @@ module Ionian
146
184
 
147
185
  # Specify default interface for outgoing multicasts.
148
186
  # ( IP_MULTICAST_IF )
149
- def ip_multicast_if=(interface = nil)
187
+ def ip_multicast_if= interface = nil
150
188
  interface ||= '0.0.0.0'
151
189
 
152
190
  self.setsockopt \
@@ -164,7 +202,7 @@ module Ionian
164
202
 
165
203
  # Set the time to live (hop limit) for outgoing multicasts.
166
204
  # ( IP_MULTICAST_TTL )
167
- def ip_multicast_ttl=(value)
205
+ def ip_multicast_ttl= value
168
206
  self.setsockopt ::Socket::IPPROTO_IP, ::Socket::IP_MULTICAST_TTL, [value].pack('C')
169
207
  end
170
208
 
@@ -180,7 +218,7 @@ module Ionian
180
218
 
181
219
  # Enables loopback of outgoing multicasts if true.
182
220
  # ( IP_MULTICAST_LOOP )
183
- def ip_multicast_loop=(value)
221
+ def ip_multicast_loop= value
184
222
  param = value ? 1 : 0
185
223
  self.setsockopt ::Socket::IPPROTO_IP, ::Socket::IP_MULTICAST_LOOP, [param].pack('C')
186
224
  end
@@ -204,7 +242,7 @@ module Ionian
204
242
  end
205
243
 
206
244
  # Not yet implemented.
207
- def ipv6_multicast_if=(value)
245
+ def ipv6_multicast_if= value
208
246
  # TODO: Implement
209
247
  end
210
248
 
@@ -215,7 +253,7 @@ module Ionian
215
253
  end
216
254
 
217
255
  # Not yet implemented.
218
- def ipv6_multicast_hops=(value)
256
+ def ipv6_multicast_hops= value
219
257
  # TODO: Implement
220
258
  end
221
259
 
@@ -228,14 +266,14 @@ module Ionian
228
266
  alias_method :ipv6_multicast_loop?, :ipv6_multicast_loop
229
267
 
230
268
  # Not yet implemented.
231
- def ipv6_multicast_loop=(value)
269
+ def ipv6_multicast_loop= value
232
270
  # TODO: Implement
233
271
  end
234
272
 
235
273
 
236
274
  class << self
237
275
  # Returns true if the given address is within the multicast range.
238
- def multicast(address)
276
+ def multicast address
239
277
  address >= '224.0.0.0' and address <= '239.255.255.255' ? true : false
240
278
  end
241
279
 
data/lib/ionian/socket.rb CHANGED
@@ -5,6 +5,15 @@ module Ionian
5
5
  # A convenient wrapper for TCP, UDP, and Unix client sockets.
6
6
  class Socket
7
7
 
8
+ # IP address or URL of server.
9
+ attr_reader :host
10
+
11
+ # Remote port number.
12
+ attr_reader :port
13
+
14
+ # Local port number.
15
+ attr_reader :bind_port
16
+
8
17
  # Creates a new socket or wraps an existing socket.
9
18
  #
10
19
  # Args:
@@ -22,7 +31,7 @@ module Ionian
22
31
  # cork: Set true to enable the TCP_CORK flag. Buffers multiple writes
23
32
  # into one segment.
24
33
  # expression: Overrides the #read_match regular expression for received data.
25
- def initialize(existing_socket = nil, **kwargs)
34
+ def initialize existing_socket = nil, **kwargs
26
35
  @socket = existing_socket
27
36
 
28
37
  @ionian_listeners = []
@@ -55,16 +64,17 @@ module Ionian
55
64
  @socket.expression = @expression if @expression
56
65
 
57
66
  initialize_socket_methods
67
+
58
68
  else
59
69
  # Initialize new socket.
60
70
 
61
- # TODO: Should be able to parse the port out of host.
62
- # :port should override this parsed value.
71
+ # Parse host out of "host:port" if specified.
72
+ host_port_ary = kwargs.fetch(:host).to_s.split ':'
63
73
 
64
- @host = kwargs.fetch :host
65
- @port = kwargs.fetch :port, 23
74
+ @host = host_port_ary[0]
75
+ @port = kwargs.fetch :port, host_port_ary[1].to_i || 23
66
76
  @bind_port = kwargs.fetch :bind_port, @port
67
-
77
+
68
78
  # Automatically select UDP for the multicast range. Otherwise default to TCP.
69
79
  default_protocol = :tcp
70
80
  default_protocol = :udp if Ionian::Extension::Socket.multicast? @host
@@ -94,7 +104,7 @@ module Ionian
94
104
  end
95
105
 
96
106
  # Set the regular expression used to match incoming data.
97
- def expression=(exp)
107
+ def expression= exp
98
108
  @expression = exp
99
109
  @socket.expression = exp if @socket
100
110
  end
@@ -108,7 +118,7 @@ module Ionian
108
118
  # Returns an array of received matches.
109
119
  # Block yields received match.
110
120
  # See Ionian::Extension::IO#read_match.
111
- def cmd(string, **kwargs, &block)
121
+ def cmd string, **kwargs, &block
112
122
  create_socket unless @persistent
113
123
 
114
124
  if @protocol == :udp
@@ -119,7 +129,7 @@ module Ionian
119
129
 
120
130
  @socket.flush
121
131
 
122
- matches = @socket.read_match(kwargs) {|match| yield match if block_given?}
132
+ matches = @socket.read_match(kwargs) { |match| yield match if block_given? }
123
133
  @socket.close unless @persistent
124
134
 
125
135
  matches
@@ -138,8 +148,8 @@ module Ionian
138
148
  alias_method :on_match, :register_observer
139
149
 
140
150
  # Unregister a block from being called when matched data is received.
141
- def unregister_observer(&block)
142
- @ionian_listeners.delete_if {|o| o == block}
151
+ def unregister_observer &block
152
+ @ionian_listeners.delete_if { |o| o == block }
143
153
  @socket.unregister_observer &block if @socket
144
154
  block
145
155
  end
@@ -150,7 +160,7 @@ module Ionian
150
160
  # Args:
151
161
  # Timeout: Number of seconds to wait for data until
152
162
  # giving up. Set to nil for blocking.
153
- def has_data?(**kwargs)
163
+ def has_data? **kwargs
154
164
  return false unless @socket
155
165
  @socket.has_data? kwargs
156
166
  end
@@ -169,13 +179,13 @@ module Ionian
169
179
 
170
180
  # Writes the given string(s) to the socket and appends a
171
181
  # newline character to any string not already ending with one.
172
- def puts(*string)
173
- self.write string.map{|s| s.chomp}.join("\n") + "\n"
182
+ def puts *string
183
+ self.write string.map{ |s| s.chomp }.join("\n") + "\n"
174
184
  end
175
185
 
176
186
  # Writes the given string to the socket. Returns the number of
177
187
  # bytes written.
178
- def write(string)
188
+ def write string
179
189
  create_socket unless @persistent
180
190
 
181
191
  num_bytes = 0
@@ -255,8 +265,8 @@ module Ionian
255
265
  # This was chosen over method_missing to avoid traversing the object
256
266
  # hierarchy on every method call, like transmitting data.
257
267
  @socket.methods
258
- .select {|m| @socket.respond_to? m}
259
- .select {|m| not self.respond_to? m}
268
+ .select { |m| @socket.respond_to? m }
269
+ .select { |m| not self.respond_to? m }
260
270
  .each do |m|
261
271
  self.singleton_class.send :define_method, m do |*args, &block|
262
272
  @socket.__send__ m, *args, &block
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ionian
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.6.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex McLain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-15 00:00:00.000000000 Z
11
+ date: 2014-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake