Class: IORequest::SSLSockets::Server

Inherits:
Object
  • Object
show all
Includes:
Utility::MultiThread
Defined in:
lib/io_request/connection/ssl_sockets.rb

Overview

SSL socket server.

Instance Method Summary collapse

Constructor Details

#initialize(port: 8000, authorizer: Authorizer.empty, certificate: nil, key: nil, &requests_handler) ⇒ Server

Initalize new server.

Parameters:

  • port (Integer) (defaults to: 8000)

    port of server.

  • authorizer (Authorizer) (defaults to: Authorizer.empty)
  • certificate (String) (defaults to: nil)
  • key (String) (defaults to: nil)


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/io_request/connection/ssl_sockets.rb', line 20

def initialize(
  port: 8000,
  authorizer: Authorizer.empty,
  certificate: nil,
  key: nil,
  &requests_handler
)
  @port = port
  @authorizer = authorizer
  @requests_handler = requests_handler

  initialize_ssl_context(certificate, key)
end

Instance Method Details

#clientsArray<IORequest::Client>

Returns:



35
36
37
# File 'lib/io_request/connection/ssl_sockets.rb', line 35

def clients
  @clients_data.keys
end

#data(client) ⇒ Hash?

Returns you are free to store anything you want in hash. Only field you will find in it is ‘auth` with authenticator data.

Parameters:

Returns:

  • (Hash, nil)

    you are free to store anything you want in hash. Only field you will find in it is ‘auth` with authenticator data.



42
43
44
# File 'lib/io_request/connection/ssl_sockets.rb', line 42

def data(client)
  @clients_data[client]
end

#startObject

Start server.



47
48
49
50
51
52
53
# File 'lib/io_request/connection/ssl_sockets.rb', line 47

def start
  @clients_data = {}

  @server = TCPServer.new(@port)

  @accept_thread = in_thread(name: 'accept_thr') { accept_loop }
end

#stopObject

Fully stop server.



56
57
58
59
60
61
62
63
64
65
# File 'lib/io_request/connection/ssl_sockets.rb', line 56

def stop
  clients.each(&:close)
  @clients_data.clear

  @server.close
  @server = nil

  @accept_thread&.kill
  @accept_thread = nil
end