Sha256: a6741a8c1dcd3c9a0aaac446e026a58c1c872f38609702db0103be9cb2a95449
Contents?: true
Size: 1.86 KB
Versions: 1
Compression:
Stored size: 1.86 KB
Contents
require 'socket' module Moneta # Moneta server # @api public class Server # @param [Hash] options # @option options [Integer] :port (9000) TCP port # @option options [String] :file Alternative Unix socket file name def initialize(store, options = {}) @store = store @server = if @file = options[:file] UNIXServer.open(@file) else TCPServer.open(options[:port] || DEFAULT_PORT) end @clients = [@server] @running = false end def running? @running end def run raise 'Already running' if @running @stop = false @running = true begin until @stop mainloop end ensure File.unlink(@file) if @file end end def stop raise 'Not running' unless @running @stop = true @server.close @server = nil end private include Net TIMEOUT = 1 def mainloop client = accept handle(client) if client rescue Exception => ex puts ex.message write(client, Error.new(ex.message)) if client end def accept ios = IO.select(@clients, nil, @clients, TIMEOUT) return nil unless ios ios[2].each do |io| io.close @clients.delete(io) end ios[0].each do |io| if io == @server client = @server.accept @clients << client if client else return io unless io.eof? @clients.delete(io) end end nil end def handle(client) method, *args = read(client) case method when :key?, :load, :delete, :increment write(client, @store.send(method, *args)) when :store, :clear @store.send(method, *args) client.write(@nil ||= pack(nil)) else raise 'Invalid method call' end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
moneta-0.7.2 | lib/moneta/server.rb |