Class: David::Server

Inherits:
Object
  • Object
show all
Includes:
Celluloid::IO, MidCache, Multicast, Respond, Utility
Defined in:
lib/david/server.rb,
lib/david/server/mapping.rb,
lib/david/server/respond.rb,
lib/david/server/utility.rb,
lib/david/server/constants.rb,
lib/david/server/multicast.rb

Defined Under Namespace

Modules: Constants, Mapping, Multicast, Respond, Utility

Constant Summary

Constants included from Mapping

Mapping::HTTP_TO_COAP_CODES, Mapping::HTTP_TO_COAP_CODES_MINIMAL

Constants included from Constants

Constants::ASCII_8BIT, Constants::COAP_CBOR, Constants::COAP_DTLS, Constants::COAP_DTLS_ID, Constants::COAP_DTLS_NOSEC, Constants::COAP_MULTICAST, Constants::COAP_VERSION, Constants::CONTENT_LENGTH, Constants::CONTENT_TYPE, Constants::CONTENT_TYPE_CBOR, Constants::CONTENT_TYPE_JSON, Constants::EMPTY_STRING, Constants::HTTP_ACCEPT, Constants::HTTP_CACHE_CONTROL, Constants::HTTP_CONTENT_LENGTH, Constants::HTTP_CONTENT_TYPE, Constants::HTTP_ETAG, Constants::HTTP_LOCATION, Constants::PATH_INFO, Constants::QUERY_STRING, Constants::RACK_ERRORS, Constants::RACK_INPUT, Constants::RACK_LOGGER, Constants::RACK_MULTIPROCESS, Constants::RACK_MULTITHREAD, Constants::RACK_RUN_ONCE, Constants::RACK_URL_SCHEME, Constants::RACK_URL_SCHEME_HTTP, Constants::RACK_VERSION, Constants::REMOTE_ADDR, Constants::REMOTE_PORT, Constants::REQUEST_METHOD, Constants::SCRIPT_NAME, Constants::SERVER_NAME, Constants::SERVER_PORT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Respond

#respond

Methods included from Multicast

#multicast_initialize!

Methods included from MidCache

#cache, #cache_add, #cache_clean!, #cache_delete, #cache_get, included

Constructor Details

#initialize(app, options) ⇒ Server

Returns a new instance of Server.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/david/server.rb', line 21

def initialize(app, options)
  @app        = app.respond_to?(:new) ? app.new : app
  @mid_cache  = {}
  @options    = AppConfig.new(options)
  @log        = @options[:Log]

  host, port  = @options.values_at(:Host, :Port)

  log.info "David #{David::VERSION} on #{RUBY_DESCRIPTION}"
  log.info "Starting on coap://[#{host}]:#{port}"

  af = ipv6? ? ::Socket::AF_INET6 : ::Socket::AF_INET

  # Actually Celluloid::IO::UDPSocket.
  @socket = UDPSocket.new(af)
  multicast_initialize! if @options[:Multicast]
  @socket.bind(host, port)
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



17
18
19
# File 'lib/david/server.rb', line 17

def log
  @log
end

#socketObject (readonly)

Returns the value of attribute socket.



17
18
19
# File 'lib/david/server.rb', line 17

def socket
  @socket
end

Instance Method Details

#answer(exchange, key = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/david/server.rb', line 55

def answer(exchange, key = nil)
  @socket.send(exchange.message.to_wire, 0, exchange.host, exchange.port)

  if log.info?
    log.info('-> ' + exchange.to_s)
    log.debug(exchange.message.inspect)
  end

  cache_add(exchange.key, exchange.message) if exchange.ack?
end

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/david/server.rb', line 40

def run
  loop do
    if jruby_or_rbx?
      dispatch(*@socket.recvfrom(1152))
    else
      begin
        dispatch(*@socket.to_io.recvmsg_nonblock)
      rescue ::IO::WaitReadable
        Celluloid::IO.wait_readable(@socket)
        retry
      end
    end
  end
end