Class: Wamp::Router::Server

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

Overview

Connection Handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(router, options = {}) ⇒ Server

Returns a new instance of Server.



14
15
16
17
18
19
# File 'lib/wamp/router/server.rb', line 14

def initialize(router, options = {})
  @options = options
  @selector = NIO::Selector.new
  @router = router
  # @router.add_realm(options.fetch(:realm, "realm1"))
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/wamp/router/server.rb', line 12

def options
  @options
end

#selectorObject (readonly)

Returns the value of attribute selector.



12
13
14
# File 'lib/wamp/router/server.rb', line 12

def selector
  @selector
end

Instance Method Details

#accept_connectionObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/wamp/router/server.rb', line 45

def accept_connection
  selector.select do |monitor|
    case monitor.io
    when TCPServer
      create_connection(monitor.io.accept_nonblock)
    when TCPSocket
      monitor.value.call
    end
  end
end

#create_connection(client) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wamp/router/server.rb', line 56

def create_connection(client)
  monitor = selector.register(client, :r)
  connection = Connection.new(client) do |conn|
    selector.deregister(monitor)
    @router.detach_client(conn)
  end
  connection.router = @router
  monitor.value = proc do
    connection.listen
  end
end

#create_tcp_serverObject



40
41
42
43
# File 'lib/wamp/router/server.rb', line 40

def create_tcp_server
  server = TCPServer.new(options.fetch(:host, "127.0.0.1"), options.fetch(:port, 8080))
  selector.register(server, :r)
end

#options_messageObject



33
34
35
36
37
38
# File 'lib/wamp/router/server.rb', line 33

def options_message
  host = options.fetch(:host, "127.0.0.1")
  port = options.fetch(:port, 8080)
  realm = options.fetch(:realm, "realm1")
  puts "Starting router on ws://#{host}:#{port}/ws and added Realm: #{realm}"
end

#runObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wamp/router/server.rb', line 21

def run
  trap("INT") { throw :ctrl_c }

  create_tcp_server
  options_message
  catch :ctrl_c do
    loop do
      accept_connection
    end
  end
end