Class: Wee::Application

Inherits:
Object show all
Defined in:
lib/wee/application.rb

Overview

A Wee::Application manages all Wee::RequestHandler’s of a single application, where most of the time the request handlers are Wee::Session objects. It dispatches the request to the correct handler by examining the request.

Direct Known Subclasses

OgApplication, PagelessApplication

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Application

Creates a new application. The block is used to initialize the attributes:

Wee::Application.new {|app|
  app.default_request_handler { MySession.new } 
  app.id_generator = Wee::SimpleIdGenerator.new
  app.max_request_handlers = 1000 
}


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/wee/application.rb', line 34

def initialize(&block)
  @request_handlers = Hash.new
  block.call(self)
  @id_generator ||= Wee::SimpleIdGenerator.new(rand(1_000_000))
  if @default_request_handler.nil?
    raise ArgumentError, "No default request handler specified"
  end
  @mutex = Mutex.new

  # start request-handler collecting thread
  # run once every minute
  @gc_thread = Thread.new {
    sleep 60
    @mutex.synchronize { garbage_collect_handlers }
  }
end

Instance Attribute Details

#id_generatorObject

The generator used for creating unique request handler id’s.



9
10
11
# File 'lib/wee/application.rb', line 9

def id_generator
  @id_generator
end

#max_request_handlersObject

The maximum number of request handlers



13
14
15
# File 'lib/wee/application.rb', line 13

def max_request_handlers
  @max_request_handlers
end

#propertiesObject

                                                                        • -

:section: Properties

                                                                        • -



110
111
112
# File 'lib/wee/application.rb', line 110

def properties
  @properties
end

Instance Method Details

#default_request_handler(&block) ⇒ Object

Get or set the default request handler. The default request handler is used if no request handler id is given in a request.



18
19
20
21
22
23
24
# File 'lib/wee/application.rb', line 18

def default_request_handler(&block)
  if block.nil?
    @default_request_handler
  else
    @default_request_handler = block
  end
end

#get_property(prop, klass) ⇒ Object

Returns an “owned” property for the given klass.



114
115
116
117
118
119
120
# File 'lib/wee/application.rb', line 114

def get_property(prop, klass)
  if @properties
    @properties.fetch(klass, {})[prop]
  else
    nil
  end
end

#handle_request(context) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/wee/application.rb', line 51

def handle_request(context)
  request_handler_id = context.request.request_handler_id
  request_handler = @mutex.synchronize { @request_handlers[request_handler_id] }

  if request_handler_id.nil?

    # No id was given -> check whether the maximum number of request-handlers
    # limit is reached. if not, create new id and handler

    @mutex.synchronize {
      if @max_request_handlers != nil and @request_handlers.size >= @max_request_handlers
        # limit reached -> remove non-alive handlers...
        garbage_collect_handlers()

        # ...and test again
        if @request_handlers.size >= @max_request_handlers
          # TODO: show a custom error message
          raise "maximum number of request-handlers reached" 
        end
      end

      request_handler_id =  unique_request_handler_id()
      request_handler = @default_request_handler.call  
      request_handler.id = request_handler_id
      request_handler.application = self
      @request_handlers[request_handler_id] = request_handler
    }

    context.request.request_handler_id = request_handler_id 
    handle_request(context)
    return

  elsif request_handler.nil?

    # A false request handler id was given. This might indicate that a
    # request handler has expired. 

    request_handler_expired(context)
    return

  elsif !request_handler.alive?

    # The request_handler is not alive anymore.

    request_handler_expired(context)
    return

  end

  request_handler.handle_request(context)

rescue => exn
  context.response = Wee::ErrorResponse.new(exn) 
end