class YDIM::Server

Constants

CONFIG
SECONDS_IN_DAY

Attributes

serv[R]

Public Class Methods

config() click to toggle source
# File lib/ydim/server_config.rb, line 53
def Server.config
  CONFIG
end
new(config, logger) click to toggle source
# File lib/ydim/server.rb, line 23
          def initialize(config, logger)
                  @serv = Needle::Registry.new
                  @serv.register(:auth_server) { 
                          auth = RRBA::Server.new
                          root = RootUser.new(:root)
                          root.name = config.root_name
                          root.email = config.root_email
                          root_key = config.root_key
                          path = File.expand_path(root_key, config.conf_dir)
                          path_or_key = File.exist?(path) ? path : root_key
                          root.public_key = Util.load_key(path_or_key)
                          auth.root = root
                          auth
                  }
                  @serv.register(:clients) {
                          ClientHandler.new(@serv)
                  }
                  @serv.register(:config) {
                          config
                  }
                  @serv.register(:currency_converter) {
                          ODBA.cache.fetch_named('currency_converter', self) { 
                                  CurrencyConverter.new      
                          }
                  }
                  @serv.register(:factory) {
                          Factory.new(@serv)
                  }
                  @serv.register(:id_server) { 
                          ODBA.cache.fetch_named('id_server', self) {
                                  ODBA::IdServer.new
                          }
                  }
                  @serv.register(:logger) {
                          logger
                  }
                  if(hour = config.autoinvoice_hour)
                          @autoinvoicer = repeat_at(hour, 'AutoInvoicer') {
                                  AutoInvoicer.new(@serv).run
                          }
                  end
                  if(hour = config.currency_update_hour)
                          if(@serv.currency_converter.known_currencies \
                                   < @serv.config.currencies.size)
                                  CurrencyUpdater.new(@serv).run
                          end
                          @currency_updater = repeat_at(hour, 'CurrencyUpdater') {
                                  CurrencyUpdater.new(@serv).run
                          }
                  end
@status_updater = repeat_at(1, 'StatusUpdater') {
  Invoice.all { |inv| inv.save }
}
                  @sessions = []
          end

Public Instance Methods

login(client, name=nil, &block) click to toggle source
# File lib/ydim/server.rb, line 78
def login(client, name=nil, &block)
        @serv.logger.debug(client.__drburi) { "attempting login" }
        session = @serv.auth_server.authenticate(name, &block)
        session.serv = @serv
        session.client = client
        @serv.logger.info(session.whoami) { 'login' }
        @sessions.push(session)
        session
rescue Exception => error
        @serv.logger.error('unknown user') { 
                [error.class, error.message].join(' - ') }
        raise
end
logout(session) click to toggle source
# File lib/ydim/server.rb, line 91
def logout(session)
        @serv.logger.info(session.whoami) { 'logout' }
        @sessions.delete(session)
        nil
end
ping() click to toggle source
# File lib/ydim/server.rb, line 96
def ping
        true
end

Private Instance Methods

repeat_at(hour, thread_name) { || ... } click to toggle source
# File lib/ydim/server.rb, line 100
def repeat_at(hour, thread_name)
        Thread.new { 
                Thread.current.abort_on_exception = true
                loop {
                        now = Time.now
                        next_run = Time.local(now.year, now.month, now.day, hour)
                        sleepy_time = next_run - now
                        if(sleepy_time < 0)
                                sleepy_time += SECONDS_IN_DAY
                                next_run += SECONDS_IN_DAY
                        end
                        @serv.logger.info(thread_name) {
                                sprintf("next run %s, sleeping %i seconds", 
                                                                next_run.strftime("%c"), sleepy_time)
                        }
                        sleep(sleepy_time)
                        yield
                }
        }
end