lib/ionian/server.rb in ionian-0.6.5 vs lib/ionian/server.rb in ionian-0.6.6
- old
+ new
@@ -4,44 +4,76 @@
module Ionian
# A convenient wrapper for TCP, UDP, and Unix server sockets.
class Server
+ # Interface to listen for clients.
+ attr_reader :interface
+
+ # Port number to listen for clients.
+ attr_reader :port
+
+ # Returns a symbol of the type of protocol this socket uses:
+ # :tcp, :udp, :unix
+ attr_reader :protocol
+ alias_method :protocol?, :protocol
+
+
# A convenient wrapper for TCP and Unix server sockets (UDP doesn't use
# a server).
#
# Accepts an optional block that is passed to #register_accept_listener.
# Server opens listening socket on instantiation if this block is provided.
#
# Args:
+ # port: Port number to listen for clients.
# interface: The address of the network interface to bind to.
# Defaults to all.
# protocol: :tcp, :unix. Default is :tcp.
def initialize **kwargs, &block
@accept_listeners = []
register_accept_listener &block if block_given?
- @interface = kwargs.fetch :interface, ''
+ # @interface = kwargs.fetch :interface, nil
+ @interface = kwargs.fetch :interface, nil
@port = kwargs.fetch :port, nil
-
# Automatically select UDP for the multicast range. Otherwise default to TCP.
default_protocol = :tcp
- # TODO: This ivar may be incorrect for UDP -- bound interface is not destination.
- default_protocol = :udp if Ionian::Extension::Socket.multicast? @interface
- default_protocol = :unix if @interface.start_with? '/'
+ if @interface
+ # TODO: This ivar may be incorrect for UDP -- bound interface is not destination.
+ default_protocol = :udp if Ionian::Extension::Socket.multicast? @interface
+ default_protocol = :unix if @interface.start_with? '/'
+ end
+
@protocol = kwargs.fetch :protocol, default_protocol
+
# TODO: Move this to #listen.
case @protocol
when :tcp
+ @interface ||= '0.0.0.0' # All interfaces.
+
+ # Parse host out of "host:port" if specified.
+ host_port_ary = @interface.to_s.split ':'
+ @interface = host_port_ary[0]
+ @port ||= host_port_ary[1]
+
+ # TODO: Parse port from interface if TCP.
+ raise ArgumentError, "Port not specified." unless @port
+ @port = @port.to_i
+
@server = TCPServer.new @interface, @port
@server.setsockopt ::Socket::SOL_SOCKET, ::Socket::SO_REUSEADDR, [1].pack('i')
+
when :udp
raise ArgumentError, "UDP should be implemented with Ionian::Socket."
+
when :unix
+ raise ArgumentError, "Path not specified." unless @interface
+
@server = UNIXServer.new @interface
end
listen if block_given?
end
@@ -91,10 +123,10 @@
alias_method :on_accept, :register_accept_listener
# Unregisters a socket accept notifier block.
def unregister_accept_listener proc
- @accept_listeners.delete_if {|o| o == proc}
+ @accept_listeners.delete_if { |o| o == proc }
proc
end
end
\ No newline at end of file