From 89cd5a3b4424eb7418761de52a65cb5e5bf38974 Mon Sep 17 00:00:00 2001 From: Jocelyn Fiat Date: Wed, 30 Nov 2011 19:05:16 +0100 Subject: [PATCH] Removed initial Thread for the HTTP_HANDLER, this is not needed here Removed HTTP_SERVER_SHARED_CONFIGURATION from the library ... not needed by the library. Added HTTP_SERVER_SHARED_CONFIGURATION to the example to show how to share the configuration, if needed --- example/SimpleWebServer/application.e | 3 ++ .../http_server_shared_configuration.e | 0 library/http_handler.e | 39 ++++++++++++------- library/http_server.e | 11 +----- 4 files changed, 30 insertions(+), 23 deletions(-) rename {library/configuration => example/SimpleWebServer}/http_server_shared_configuration.e (100%) diff --git a/example/SimpleWebServer/application.e b/example/SimpleWebServer/application.e index 96e80318..51666fd4 100644 --- a/example/SimpleWebServer/application.e +++ b/example/SimpleWebServer/application.e @@ -9,6 +9,8 @@ class inherit ARGUMENTS + HTTP_SERVER_SHARED_CONFIGURATION + create make @@ -24,6 +26,7 @@ feature {NONE} -- Initialization create l_cfg.make l_cfg.http_server_port := 9_000 l_cfg.document_root := default_document_root + set_server_configuration (l_cfg) debug ("nino") l_cfg.set_is_verbose (True) end diff --git a/library/configuration/http_server_shared_configuration.e b/example/SimpleWebServer/http_server_shared_configuration.e similarity index 100% rename from library/configuration/http_server_shared_configuration.e rename to example/SimpleWebServer/http_server_shared_configuration.e diff --git a/library/http_handler.e b/library/http_handler.e index 7cb47899..b1b902f9 100644 --- a/library/http_handler.e +++ b/library/http_handler.e @@ -8,7 +8,7 @@ deferred class HTTP_HANDLER inherit - THREAD + ANY HTTP_CONSTANTS @@ -27,6 +27,13 @@ feature {NONE} -- Initialization main_server_set: a_main_server ~ main_server end +feature -- Output + + log (a_message: READABLE_STRING_8) + do + io.put_string (a_message) + end + feature -- Inherited Features execute @@ -43,14 +50,14 @@ feature -- Inherited Features create l_http_socket.make_server_by_port (l_http_port) if not l_http_socket.is_bound then if is_verbose then - print ("Socket could not be bound on port " + l_http_port.out ) + log ("Socket could not be bound on port " + l_http_port.out ) end else l_http_port := l_http_socket.port from l_http_socket.listen (main_server_configuration.max_tcp_clients) if is_verbose then - print ("%NHTTP Connection Server ready on port " + l_http_port.out +" : http://localhost:" + l_http_port.out + "/%N") + log ("%NHTTP Connection Server ready on port " + l_http_port.out +" : http://localhost:" + l_http_port.out + "/%N") end on_launched (l_http_port) until @@ -59,13 +66,7 @@ feature -- Inherited Features l_http_socket.accept if not is_stop_requested then if attached l_http_socket.accepted as l_thread_http_socket then - --| FIXME jfiat [2011/11/03] : should launch a new thread to handle this connection - --| also handle permanent connection...? - receive_message_and_send_reply (l_thread_http_socket) - l_thread_http_socket.cleanup - check - socket_closed: l_thread_http_socket.is_closed - end + process_connection (l_thread_http_socket) end end is_stop_requested := main_server.stop_requested @@ -79,10 +80,10 @@ feature -- Inherited Features on_stopped end if is_verbose then - print ("HTTP Connection Server ends.") + log ("HTTP Connection Server ends.") end rescue - print ("HTTP Connection Server shutdown due to exception. Please relaunch manually.") + log ("HTTP Connection Server shutdown due to exception. Please relaunch manually.") if attached l_http_socket as ll_http_socket then ll_http_socket.cleanup @@ -97,6 +98,18 @@ feature -- Inherited Features retry end + process_connection (a_socket: TCP_STREAM_SOCKET) + -- Process incoming connection + do + log ("Incoming connection...%N") + --| FIXME jfiat [2011/11/03] : should use a Pool of Threads/Handler to process this connection + --| also handle permanent connection...? + receive_message_and_send_reply (a_socket) + a_socket.cleanup + ensure + socket_closed: a_socket.is_closed + end + feature -- Event on_launched (a_port: INTEGER) @@ -121,7 +134,7 @@ feature -- Event end feature -- Access - + is_verbose: BOOLEAN -- Is verbose for output messages. do diff --git a/library/http_server.e b/library/http_server.e index 862eac4c..66f61089 100644 --- a/library/http_server.e +++ b/library/http_server.e @@ -7,9 +7,6 @@ note class HTTP_SERVER -inherit - HTTP_SERVER_SHARED_CONFIGURATION - create make @@ -18,7 +15,6 @@ feature -- Initialization make (cfg: like configuration) do configuration := cfg - set_server_configuration (configuration) end setup (a_http_handler: HTTP_HANDLER) @@ -30,12 +26,7 @@ feature -- Initialization print ("Starting Web Application Server (port="+ configuration.http_server_port.out +"):%N") end stop_requested := False - if configuration.force_single_threaded then - a_http_handler.execute - else - a_http_handler.launch - a_http_handler.join - end + a_http_handler.execute end shutdown_server