Sha256: 661fd5967b04fa7135fd17aa406c7b3834764127249309b588dfd6005a230a91

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module UState
  class Server
    # A server binds together Backends, an Index, and Sinks.
    # - Backends spawn Connections with EM.
    # - Connections receive messages from clients, and pass States to the Index.
    # - The Index aggregates states together and informs Sinks.
 
    class Error < RuntimeError; end

    require 'eventmachine' 
    require 'ustate/server/connection'
    require 'ustate/server/index'
    require 'ustate/server/backends'
    require 'treetop'
    require 'ustate/query_string'
    require 'ustate/query/ast'

    attr_accessor :backends
    attr_accessor :index
   
    def initialize(opts = {})
      # Backends
      @backends = []
      b = Backends::TCP.new opts
      b.server = self
      @backends << b

      @index = Index.new

      setup_signals
    end

    def emailer(opts = {})
      require 'ustate/emailer'
      @emailer ||= UState::Emailer.new(@index, opts)
    end

    def start
      @index.start
     
      # Right now b.start blocks... should look into EM 
      @backends.map do |b|
        b.start
      end
    end

    def stop
      @backends.map do |b|
        if b.running?
          b.stop
        else
          stop!
        end
      end

      @index.stop
    end

    def stop!
      @backends.map do |b|
        b.stop!
      end

      @index.stop!
    end

    def setup_signals
      trap('INT') { stop! }
      trap('TERM') { stop }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ustate-client-0.0.3 lib/ustate/server.rb