| 1 |
-------------------------------------------------------------------------------- |
|---|
| 2 |
-- Title: LWApplication.lua |
|---|
| 3 |
-- Description: Like a square peg in a round hole |
|---|
| 4 |
-- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ |
|---|
| 5 |
-- Creation Date: February 1, 2005 |
|---|
| 6 |
-- Legal: Copyright (C) 2005 Raphaël Szwarc |
|---|
| 7 |
-------------------------------------------------------------------------------- |
|---|
| 8 |
|
|---|
| 9 |
-- import dependencies |
|---|
| 10 |
local LUObject = require( "LUObject" ) |
|---|
| 11 |
local LULog = require( "LULog" ) |
|---|
| 12 |
local SocketRunLoop = require( "SocketRunLoop" ) |
|---|
| 13 |
local SocketServer = require( "SocketServer" ) |
|---|
| 14 |
local LWContext = require( "LWContext" ) |
|---|
| 15 |
local LWDispatcher = require( "LWDispatcher" ) |
|---|
| 16 |
local LWRequest = require( "LWRequest" ) |
|---|
| 17 |
local LWResponse = require( "LWResponse" ) |
|---|
| 18 |
local LWSession = require( "LWSession" ) |
|---|
| 19 |
|
|---|
| 20 |
-- define the class |
|---|
| 21 |
local super = LUObject |
|---|
| 22 |
local self = super() |
|---|
| 23 |
|
|---|
| 24 |
-- constants |
|---|
| 25 |
local MaximumUriSize = 1000 |
|---|
| 26 |
|
|---|
| 27 |
-- initialization method |
|---|
| 28 |
function self:init( aPort, anAddress ) |
|---|
| 29 |
self = super.init( self ) |
|---|
| 30 |
|
|---|
| 31 |
self._port = aPort |
|---|
| 32 |
self._address = anAddress |
|---|
| 33 |
|
|---|
| 34 |
return self |
|---|
| 35 |
end |
|---|
| 36 |
|
|---|
| 37 |
-- method to access this application address |
|---|
| 38 |
function self:address() |
|---|
| 39 |
if self._server ~= nil then |
|---|
| 40 |
return self:server():address() |
|---|
| 41 |
end |
|---|
| 42 |
|
|---|
| 43 |
if self._address == nil then |
|---|
| 44 |
self._address = "*" |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
return self._address |
|---|
| 48 |
end |
|---|
| 49 |
|
|---|
| 50 |
-- method to access this application port |
|---|
| 51 |
function self:port() |
|---|
| 52 |
if self._server ~= nil then |
|---|
| 53 |
return self:server():port() |
|---|
| 54 |
end |
|---|
| 55 |
|
|---|
| 56 |
if self._port == nil then |
|---|
| 57 |
self._port = 0 |
|---|
| 58 |
end |
|---|
| 59 |
|
|---|
| 60 |
return self._port |
|---|
| 61 |
end |
|---|
| 62 |
|
|---|
| 63 |
-- method to access this application server |
|---|
| 64 |
function self:server() |
|---|
| 65 |
if self._server == nil then |
|---|
| 66 |
self._server = SocketServer( self:address(), self:port(), self ) |
|---|
| 67 |
end |
|---|
| 68 |
|
|---|
| 69 |
return self._server |
|---|
| 70 |
end |
|---|
| 71 |
|
|---|
| 72 |
-- method to start this application |
|---|
| 73 |
function self:start( shouldStartRunLoop ) |
|---|
| 74 |
self:server():start() |
|---|
| 75 |
|
|---|
| 76 |
if shouldStartRunLoop == nil or shouldStartRunLoop == true then |
|---|
| 77 |
SocketRunLoop:default():start() |
|---|
| 78 |
end |
|---|
| 79 |
|
|---|
| 80 |
return self |
|---|
| 81 |
end |
|---|
| 82 |
|
|---|
| 83 |
-- method to access this application dispatcher |
|---|
| 84 |
function self:dispatcher() |
|---|
| 85 |
if self._dispatcher == nil then |
|---|
| 86 |
self._dispatcher = LWDispatcher() |
|---|
| 87 |
end |
|---|
| 88 |
|
|---|
| 89 |
return self._dispatcher |
|---|
| 90 |
end |
|---|
| 91 |
|
|---|
| 92 |
-- method to access the default service |
|---|
| 93 |
function self:defaultService() |
|---|
| 94 |
return self:dispatcher():defaultService() |
|---|
| 95 |
end |
|---|
| 96 |
|
|---|
| 97 |
-- method to register a service with this application |
|---|
| 98 |
function self:registerService( aService ) |
|---|
| 99 |
self:dispatcher():registerService( aService ) |
|---|
| 100 |
|
|---|
| 101 |
return self |
|---|
| 102 |
end |
|---|
| 103 |
|
|---|
| 104 |
-- method to access this application services |
|---|
| 105 |
function self:services() |
|---|
| 106 |
return self:dispatcher():services() |
|---|
| 107 |
end |
|---|
| 108 |
|
|---|
| 109 |
-- method to find a service for a given context |
|---|
| 110 |
function self:serviceWithContext( aContext ) |
|---|
| 111 |
return self:dispatcher():serviceWithContext( aContext ) |
|---|
| 112 |
end |
|---|
| 113 |
|
|---|
| 114 |
-- method to access this application session class |
|---|
| 115 |
function self:sessionClass() |
|---|
| 116 |
if self._sessionClass == nil then |
|---|
| 117 |
self._sessionClass = LWSession |
|---|
| 118 |
end |
|---|
| 119 |
|
|---|
| 120 |
return self._sessionClass |
|---|
| 121 |
end |
|---|
| 122 |
|
|---|
| 123 |
-- method to set this application session class |
|---|
| 124 |
function self:setSessionClass( aValue ) |
|---|
| 125 |
self._sessionClass = aValue |
|---|
| 126 |
|
|---|
| 127 |
return self |
|---|
| 128 |
end |
|---|
| 129 |
|
|---|
| 130 |
local function catch( anException ) |
|---|
| 131 |
LULog:warning( anException ) |
|---|
| 132 |
|
|---|
| 133 |
return nil, anException |
|---|
| 134 |
end |
|---|
| 135 |
|
|---|
| 136 |
-- method to run this application |
|---|
| 137 |
function self:run( aServer, aReader, aWriter ) |
|---|
| 138 |
while aReader:status() ~= "closed" and aWriter:status() ~= "closed" do |
|---|
| 139 |
local aRequest = LWRequest( aReader ) |
|---|
| 140 |
|
|---|
| 141 |
if aRequest ~= nil then |
|---|
| 142 |
local aResponse = LWResponse( aWriter, aRequest ) |
|---|
| 143 |
|
|---|
| 144 |
if aRequest:version() ~= nil then |
|---|
| 145 |
if aRequest:uri() ~= nil and aRequest:uri():path() ~= nil then |
|---|
| 146 |
if aRequest:uri():path():len() < MaximumUriSize then |
|---|
| 147 |
local aContext = LWContext( self, aRequest, aResponse ) |
|---|
| 148 |
local aService = self:serviceWithContext( aContext ) |
|---|
| 149 |
|
|---|
| 150 |
if aService:try( "run", aContext )( catch ) == nil then |
|---|
| 151 |
aResponse:writeStatus( 500 ) |
|---|
| 152 |
end |
|---|
| 153 |
else |
|---|
| 154 |
aResponse:writeStatus( 414 ) |
|---|
| 155 |
end |
|---|
| 156 |
else |
|---|
| 157 |
aResponse:writeStatus( 400 ) |
|---|
| 158 |
end |
|---|
| 159 |
else |
|---|
| 160 |
aResponse:writeStatus( 505 ) |
|---|
| 161 |
end |
|---|
| 162 |
else |
|---|
| 163 |
aReader:close() |
|---|
| 164 |
end |
|---|
| 165 |
end |
|---|
| 166 |
|
|---|
| 167 |
return self |
|---|
| 168 |
end |
|---|
| 169 |
|
|---|
| 170 |
-- method for string representation |
|---|
| 171 |
function self:toString() |
|---|
| 172 |
return ( "LWApplication@%s:%s" ):format( self:address(), self:port() ) |
|---|
| 173 |
end |
|---|
| 174 |
|
|---|
| 175 |
return self |
|---|