Class: Moneta::Server

Inherits:
Object
  • Object
show all
Includes:
Net
Defined in:
lib/moneta/server.rb

Overview

Moneta server

Constant Summary

Constants included from Net

Net::DEFAULT_PORT

Instance Method Summary collapse

Methods included from Net

#pack, #read, #write

Constructor Details

#initialize(store, options = {}) ⇒ Server

Returns a new instance of Server.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :port (Integer) — default: 9000

    TCP port

  • :file (String)

    Alternative Unix socket file name



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/moneta/server.rb', line 10

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

Instance Method Details

#runObject

Note:

This method blocks!

Run the server



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/moneta/server.rb', line 32

def run
  raise 'Already running' if @running
  @stop = false
  @running = true
  begin
    until @stop
      mainloop
    end
  ensure
    File.unlink(@file) if @file
  end
end

#running?Boolean

Is the server running

Returns:

  • (Boolean)

    true if the server is running



25
26
27
# File 'lib/moneta/server.rb', line 25

def running?
  @running
end

#stopObject

Stop the server



46
47
48
49
50
51
# File 'lib/moneta/server.rb', line 46

def stop
  raise 'Not running' unless @running
  @stop = true
  @server.close
  @server = nil
end