Class: Zas::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/zas/service.rb

Overview

Public: Authentication service class. Construct a new instance of this class and call the instance method #run to start it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = ServiceConfiguration.new) ⇒ Service

Public: Initialize the service. The service will not be running yet. Invoke the #run method on the service instantce to run the service.

config - Configuration spec for the service.



29
30
31
32
33
34
# File 'lib/zas/service.rb', line 29

def initialize(config=ServiceConfiguration.new)
  self.host = config.host
  self.port = config.port
  self.context = ZMQ::Context.new
  self.logger = Syslogger.new(config.name, Syslog::LOG_PID, Syslog::LOG_LOCAL0)
end

Instance Attribute Details

#loggerObject

Public: A syslog logger



16
17
18
# File 'lib/zas/service.rb', line 16

def logger
  @logger
end

Instance Method Details

#authenticatorsObject

Public: A collection authenticators mapped to keys.

Returns a Hash of authenticators.



21
22
23
# File 'lib/zas/service.rb', line 21

def authenticators 
  @authenticators ||= {}
end

#runObject

Public: Run the service. This method will block while awaiting incoming requests. The service may be stopped by sending the INT signal.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/zas/service.rb', line 38

def run
  socket = context.socket ZMQ::REP
  socket.bind "tcp://#{host}:#{port}"

  trap("INT") { socket.close }

  begin
    while req = socket.recv
      socket.send(encode(authenticate(req)))
    end
  rescue ZMQ::Error => e
    logger.error "Shutting down: #{e.message}" 
    e
  end      
end

#shutdownObject

Shutdown the service cleanly (closes the 0mq context)



55
56
57
# File 'lib/zas/service.rb', line 55

def shutdown
  context.close if context
end