Sha256: 17fdbf001046705ead9bac96b9d7a57c962744e0c6450e1fd3e5854097e1099e

Contents?: true

Size: 1.92 KB

Versions: 3

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require "nio4r"
require "socket"
require_relative "connection"

module Wamp
  # Router
  module Router
    # Connection Handler
    class Server
      attr_reader :selector, :options

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

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

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

      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

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

      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

      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
    end

    @session_ids = {}
    class << self
      def create_identifier
        id = rand(100_000..(2**53))
        if @session_ids.include?(id)
          create_identifier
        else
          @session_ids[id] = id
          id
        end
      end
    end
  end
end
# TOP Level Doc

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
xconn-0.1.2 lib/wamp/router/server.rb
xconn-0.1.1 lib/wamp/router/server.rb
xconn-0.1.0 lib/wamp/router/server.rb