Sha256: a8436d8cb243902b339a88bd0d5f6fccd060b79d2b9419a965c7facb18c4d000

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

require "json"
require "securerandom"
require "websocket/driver"

require "tamashii/manager/client"
require "tamashii/manager/stream"
require "tamashii/manager/stream_event_loop"

module Tamashii
  module Manager
    class Server
      class << self
        attr_reader :instance

        LOCK = Mutex.new

        def compile
          @instance ||= new
        end

        def call(env)
          LOCK.synchronize { compile } unless instance
          call!(env)
        end

        def call!(env)
          instance.call(env)
        end
      end

      def initialize
        @event_loop = StreamEventLoop.new
        setup_heartbeat_timer

        Manager.logger.info("Server is created, read for accept connection")
      end

      def setup_heartbeat_timer
        @heartbeat_timer = @event_loop.timer(Config.heartbeat_interval) do
          @event_loop.post { Connection.instance.map(&:beat) }
        end
      end

      def call(env)
        if WebSocket::Driver.websocket?(env)
          Client.new(env, @event_loop)
          # A dummy rack response
          body = {
            message: "WS connected",
            version: Tamashii::Manager::VERSION
          }.to_json

          [
            200,
            {
              "Content-Type" => "application/json",
              "Content-Length" => body.bytesize
            },
            [body]
          ]
        else

          # TODO: Handle HTTP API
          body = {
            message: "Invalid protocol or api request",
            version: Tamashii::Manager::VERSION
          }.to_json

          [
            404,
            {
              "Content-Type" => "application/json",
              "Content-Length" => body.bytesize
            },
            [body]
          ]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tamashii-manager-0.1.5 lib/tamashii/manager/server.rb
tamashii-manager-0.1.4 lib/tamashii/manager/server.rb