Class: Wine::TestServer

Inherits:
Object
  • Object
show all
Defined in:
lib/wine/test_server.rb

Constant Summary collapse

Timeout =
100.0 / 1000.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port, logger) ⇒ TestServer

Returns a new instance of TestServer.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wine/test_server.rb', line 28

def initialize(port, logger)
  @port = port
  @log  = logger

  @stopped = false

  @config = {}

  @server_socket = nil
  @connections   = {}

  @log.progname = "wine-test-server"
  @log.formatter = Proc.new do |severity, time, progname, msg|
    "#{progname}: #{severity.downcase}: #{msg}\n"
  end
end

Class Method Details

.start(args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/wine/test_server.rb', line 12

def self.start(args)
  usage unless args.length == 1

  port = args[0].to_i
  usage if port == 0

  log = Logger.new(STDERR)

  server = new(port, log)
  server.run
end

.usageObject



24
25
26
# File 'lib/wine/test_server.rb', line 24

def self.usage
  abort "Usage: wine-test-server <port>"
end

Instance Method Details

#runObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wine/test_server.rb', line 45

def run
  listen

  @log.info("Listening on port #{@port}")

  until @stopped do
    readable, writable, erroneous = IO.select(sockets, [], [], Timeout)
    next unless readable

    readable.each do |socket|
      if socket == @server_socket
        accept
      else
        recv(@connections[socket])
      end
    end
  end

  sockets.each do |socket|
    socket.close
  end
end

#stopObject



68
69
70
# File 'lib/wine/test_server.rb', line 68

def stop
  @stopped = true
end