Class: Minbox::Server
- Inherits:
-
Object
- Object
- Minbox::Server
- Defined in:
- lib/minbox/server.rb
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #handle(socket, &block) ⇒ Object
-
#initialize(host = 'localhost', port = 25, tls = false, logger = Minbox.logger) ⇒ Server
constructor
A new instance of Server.
- #listen!(&block) ⇒ Object
- #shutdown! ⇒ Object
- #ssl_context ⇒ Object
- #tls? ⇒ Boolean
Constructor Details
#initialize(host = 'localhost', port = 25, tls = false, logger = Minbox.logger) ⇒ Server
Returns a new instance of Server.
5 6 7 8 9 10 11 |
# File 'lib/minbox/server.rb', line 5 def initialize(host = 'localhost', port = 25, tls = false, logger = Minbox.logger) @host = host @port = port @logger = logger @tls = tls @key = OpenSSL::PKey::RSA.new(2048) end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
3 4 5 |
# File 'lib/minbox/server.rb', line 3 def host @host end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
3 4 5 |
# File 'lib/minbox/server.rb', line 3 def key @key end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
3 4 5 |
# File 'lib/minbox/server.rb', line 3 def logger @logger end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
3 4 5 |
# File 'lib/minbox/server.rb', line 3 def port @port end |
Instance Method Details
#handle(socket, &block) ⇒ Object
30 31 32 33 |
# File 'lib/minbox/server.rb', line 30 def handle(socket, &block) logger.debug("client connected: #{socket.inspect}") Client.new(self, socket, logger).handle(&block) end |
#listen!(&block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/minbox/server.rb', line 17 def listen!(&block) logger.debug("Starting server on port #{port}...") @server = TCPServer.new(port.to_i) @server = upgrade(@server) if tls? logger.debug("Server started!") loop do handle(@server.accept, &block) rescue StandardError => error logger.error(error) end end |
#shutdown! ⇒ Object
35 36 37 |
# File 'lib/minbox/server.rb', line 35 def shutdown! @server&.close end |
#ssl_context ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/minbox/server.rb', line 39 def ssl_context @ssl_context ||= begin ssl_context = OpenSSL::SSL::SSLContext.new ssl_context.cert = certificate_for(key) ssl_context.key = key ssl_context.ssl_version = :TLSv1_2 ssl_context end end |
#tls? ⇒ Boolean
13 14 15 |
# File 'lib/minbox/server.rb', line 13 def tls? @tls end |