Class: Received::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/received/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Server

Returns a new instance of Server.



10
11
12
13
14
15
16
17
# File 'lib/received/server.rb', line 10

def initialize(options)
  @options = options
  Received.logger ||= (options[:logger] || Logger.new(STDERR))
  @connections = []
  # For how long the server will wait for connections to finish
  @grace_period = options[:grace_period] || 10
  create_backend
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/received/server.rb', line 8

def options
  @options
end

Instance Method Details

#idle?Boolean

Checks if the server is processing any connections

Returns:

  • (Boolean)


57
58
59
# File 'lib/received/server.rb', line 57

def idle?
  @connections.empty?
end

#loggerObject



66
67
68
# File 'lib/received/server.rb', line 66

def logger
  Received.logger
end

#remove_connection(conn) ⇒ Object



61
62
63
64
# File 'lib/received/server.rb', line 61

def remove_connection(conn)
  @connections.delete(conn)
  set_title
end

#serve!Object



19
20
21
# File 'lib/received/server.rb', line 19

def serve!
  EventMachine.run { start }
end

#startObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/received/server.rb', line 23

def start
  unless options[:unix_socket] or options[:port]
    raise "No port or UNIX socket path were provided"
  end
  set_title
  if host = options[:unix_socket]
    port = nil
  else
    host = options[:host] || '127.0.0.1'
    port = options[:port]
  end
  logger.info "Starting server on #{host}#{port ? ":" + port.to_s : ''}"
  @signature = EventMachine.start_server(host, port, Received::Connection, self, @backend) do |conn|
    add_connection(conn)
  end
end

#stopObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/received/server.rb', line 40

def stop
  return if stopping?
  logger.info "Stopping server"
  EventMachine.stop_server(@signature)
  @stopped_at = Time.now
  unless wait_for_connections_and_stop
    # Still some connections running, schedule a check later
    EventMachine.add_periodic_timer(1) { wait_for_connections_and_stop }
  end
end

#stopping?Boolean

Checks whether the server is in stopping mode

Returns:

  • (Boolean)


52
53
54
# File 'lib/received/server.rb', line 52

def stopping?
  !!@stopped_at
end