class Yast::TarballServer

a webrick server which provides the source tarballs

the server handles these URL paths:

- "/archive/current.tar.gz" - the generated source code tarball
- "/servers/index.json" - index of the tarball servers running on this machine
- "/" - just a simple index page

to stop the server press Ctrl+C

Constants

DEFAULT_HTTP_PORT

the default port number

Attributes

port[R]

Public Class Methods

new(port = nil) click to toggle source

constructor

@param port [Integer,nil] the port number, if nil the port will be found automatically

# File lib/yast/tarball_server.rb, line 61
def initialize(port = nil)
  @port = port || find_port
end

Public Instance Methods

addresses() click to toggle source

create all URLs valid for this machine, use all network interfaces (except the loop backs, the server will be used only from outside) @return [Array<String>] list of URLs

# File lib/yast/tarball_server.rb, line 46
def addresses
  # ignore the loopback addresses
  hosts = Socket.ip_address_list.reject { |a| a.ipv4_loopback? || a.ipv6_loopback? }
  # IPv6 addresses need to be closed in square brackets in URLs
  hosts.map! { |a| a.ipv6? ? "[#{a.ip_address}]" : a.ip_address.to_s }
  # include also the hostname to make it easier to write
  hostname = Socket.gethostname
  hosts << hostname if !hostname&.empty?
  hosts.map! { |h| "http://#{h}:#{port}" }
end
start() click to toggle source

start the webserver, it can be closed by pressing Ctrl+C or by sending SIGTERM signal

# File lib/yast/tarball_server.rb, line 66
def start
  dir = File.basename(Dir.pwd)
  # change the process title so we can find the running
  # servers and their ports just by simple grepping the running processes
  Process.setproctitle("rake server (#{port},#{dir})")

  # Use "*" to bind also the IPv6 addresses
  server = WEBrick::HTTPServer.new(Port: port, BindAddress: "*")
  server.mount("/archive/current.tar.gz", TarballServlet)
  server.mount("/servers/index.json", ServersServlet)
  server.mount("/", IndexServlet)

  # stop the server when receiving a signal like Ctrl+C
  # (inspired by the "un.rb" from the Ruby stdlib)
  signals = ["TERM", "QUIT"]
  signals.concat(["HUP", "INT"]) if $stdin.tty?
  signals.each do |s|
    trap(s) { server.shutdown }
  end

  server.start
end

Private Instance Methods

find_port() click to toggle source

find a free port starting from the default port number @return [Integer] the free port number

# File lib/yast/tarball_server.rb, line 105
def find_port
  DEFAULT_HTTP_PORT.step.find { |p| !port_taken?(p) }
end
port_taken?(port) click to toggle source

is the local port already taken by some other application? @param port [Integer] the port number @return [Boolean] true if the port is taken, false otherwise

# File lib/yast/tarball_server.rb, line 94
def port_taken?(port)
  # open the port and close it immediately, if that succeeds
  # some other application is already using it
  TCPSocket.new("localhost", port).close
  true
rescue Errno::ECONNREFUSED
  false
end