Module: Ebb

Defined in:
lib/ebb.rb,
lib/ebb/version.rb,
ext/ebb_ffi.c

Defined Under Namespace

Modules: FFI Classes: Connection, Request, Response

Constant Summary collapse

VERSION =
"0.3.2"
VERSION_STRING =
"Ebb #{VERSION}"
@@log =
STDOUT

Class Method Summary collapse

Class Method Details

.logObject



277
278
279
# File 'lib/ebb.rb', line 277

def self.log
  @@log
end

.log=(output) ⇒ Object



273
274
275
# File 'lib/ebb.rb', line 273

def self.log=(output)
  @@log = output
end

.process(app, req) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ebb.rb', line 81

def self.process(app, req)
  res = req.response
  req.connection.responses << res

  status = headers = body = nil
  catch(:async) do 
    status, headers, body = app.call(req.env)
  end

  # James Tucker's async response scheme
  # check out
  # http://github.com/raggi/thin/tree/async_for_rack/example/async_app.ru
  res.call(status, headers, body) if status != 0 
  # if status == 0 then the application promises to call
  # env['async.callback'].call(status, headers, body) 
  # later on...

end

.running?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/ebb.rb', line 10

def self.running?
  FFI::server_open?
end

.set_options(options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ebb.rb', line 22

def self.set_options(options)
  if options.has_key?(:fileno)
    fd = options[:fileno].to_i
    FFI::server_listen_on_fd(fd)
    log.puts "Ebb is listening on file descriptor #{fd}"
# missing until libebb adds UNIX socket support
#    elsif options.has_key?(:unix_socket)
#      socketfile = options[:unix_socket]
#      FFI::server_listen_on_unix_socket(socketfile)
#      log.puts "Ebb is listening on unix socket #{socketfile}"
  else
    port = (options[:Port] || options[:port] || 4001).to_i
    FFI::server_listen_on_port(port)
    log.puts "Ebb is listening at http://0.0.0.0:#{port}/"
  end

  @@secure = false
  if options.has_key?(:ssl_cert) and options.has_key?(:ssl_key)
    unless FFI.respond_to?(:server_set_secure)
      log.puts "ebb compiled without ssl support. get gnutls" 
    else
      cert_file = options[:ssl_cert]
      key_file = options[:ssl_key]
      if FileTest.readable?(cert_file) and FileTest.readable?(cert_file)
        FFI::server_set_secure(cert_file, key_file);
        @@secure = true
      else 
        log.puts "error opening certificate or key file"
      end
    end
  end

  unless options[:docroot].nil?
    @@docroot = options[:docroot]
    raise "#{@@docroot} not a directory"unless File.directory?(@@docroot)
  end
end

.ssl?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/ebb.rb', line 14

def self.ssl?
  @@secure
end

.start_server(app, options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ebb.rb', line 60

def self.start_server(app, options={})
  set_options(options)
  log.puts "Ebb PID #{Process.pid}"
  
  Connection.reset_responses
  @running = true
  trap('INT')  { stop_server }

  while @running
    FFI::server_process_connections()
    while request = FFI::server_waiting_requests.shift
      if app.respond_to?(:deferred?) and !app.deferred?(request.env)
        process(app, request)
      else
        Thread.new(request) { |req| process(app, req) }
      end
    end
  end
  FFI::server_unlisten()
end

.stop_serverObject



18
19
20
# File 'lib/ebb.rb', line 18

def self.stop_server()
  @running = false
end