Class: Dizby::TCProtocol::Server

Inherits:
BasicServer show all
Includes:
PolymorphicDelegated
Defined in:
lib/dizby/protocols/tcp.rb

Instance Attribute Summary collapse

Attributes inherited from BasicServer

#exported_uri, #front, #shutdown_pipe, #stream, #uri

Attributes inherited from AbstractServer

#log

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PolymorphicDelegated

included

Methods inherited from BasicServer

#add_uri_alias, #alive?, #close, #close_shutdown_pipe, #here?, #shutdown, #to_id, #to_obj

Methods included from ClassicAttributeAccess

#attr_accessor, #attr_reader, #attr_writer

Methods inherited from AbstractServer

#alive?, #connect_to, #make_distributed, #shutdown, #spawn_on

Methods included from Configurable

#config_accessor, #config_reader, #config_writer

Constructor Details

#initialize(front, config, host, port) ⇒ Server

Returns a new instance of Server.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/dizby/protocols/tcp.rb', line 58

def initialize(front, config, host, port)
  port ||= 0

  if host.empty?
    host = TCProtocol.getservername
    socket = self.class.open_socket_inaddr_any(host, port)
  else
    socket = TCPServer.open(host, port)
  end

  port = socket.addr[1] if port == 0

  super("drb://#{host}:#{port}", front, socket, config)

  TCProtocol.apply_sockopt(socket)

  @port = port
end

Instance Attribute Details

#port (readonly)

Returns the value of attribute port.



77
78
79
# File 'lib/dizby/protocols/tcp.rb', line 77

def port
  @port
end

Class Method Details

.open_socket_inaddr_any(host, port)



93
94
95
96
97
98
99
100
# File 'lib/dizby/protocols/tcp.rb', line 93

def self.open_socket_inaddr_any(host, port)
  infos = Socket.getaddrinfo(host, nil, Socket::AF_UNSPEC,
                             Socket::SOCK_STREAM, 0, Socket::AI_PASSIVE)
  families = Hash[*infos.map { |af, *_| af }.uniq.zip([]).flatten]
  return TCPServer.open('0.0.0.0', port) if families.key?('AF_INET')
  return TCPServer.open('::', port) if families.key?('AF_INET6')
  TCPServer.open(port)
end

Instance Method Details

#accept



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dizby/protocols/tcp.rb', line 79

def accept
  socket = nil
  loop do
    socket = super
    break if !tcp_acl || tcp_acl.allow_socket?(socket) # TODO: not tested
    socket.close
  end

  TCProtocol.apply_sockopt(socket)
  BasicConnection.new(self, socket)
end