Class: Wee::Application
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
Instance Attribute Summary collapse
-
#id_generator ⇒ Object
The generator used for creating unique request handler id’s.
-
#max_request_handlers ⇒ Object
The maximum number of request handlers.
Instance Method Summary collapse
-
#default_request_handler(&block) ⇒ Object
Get or set the default request handler.
-
#get_property(prop, klass) ⇒ Object
Returns an “owned” property for the given
klass. - #handle_request(context) ⇒ Object
-
#initialize(&block) ⇒ Application
constructor
Creates a new application.
- #insert_new_request_handler(request_handler) ⇒ Object
-
#properties ⇒ Object
-
:section: Properties - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -.
-
- #properties=(props) ⇒ Object
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_generator ⇒ Object
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_handlers ⇒ Object
The maximum number of request handlers
13 14 15 |
# File 'lib/wee/application.rb', line 13 def max_request_handlers @max_request_handlers 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.
117 118 119 120 121 122 123 |
# File 'lib/wee/application.rb', line 117 def get_property(prop, klass) if self.properties self.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 |
# 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 request_handler = @default_request_handler.call insert_new_request_handler(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 |
#insert_new_request_handler(request_handler) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/wee/application.rb', line 89 def insert_new_request_handler(request_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.application = self @request_handlers[request_handler.id] = request_handler } end |
#properties ⇒ Object
-
-
-
:section: Properties
-
-
-
112 |
# File 'lib/wee/application.rb', line 112 def properties() @__properties end |
#properties=(props) ⇒ Object
113 |
# File 'lib/wee/application.rb', line 113 def properties=(props) @__properties = props end |