Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
Merge pull request #43 from luismartingil/42-configure-blocking-natur…
Browse files Browse the repository at this point in the history
…e-for-server-run

Added a block boolean param to the server.run function
  • Loading branch information
Jeffail authored Nov 7, 2018
2 parents 430d712 + 5c6edb1 commit 193b141
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ ADD_SUBDIRECTORY (binary_data)
ADD_SUBDIRECTORY (form_data)
ADD_SUBDIRECTORY (handlers)
ADD_SUBDIRECTORY (hello_world)
ADD_SUBDIRECTORY (hello_world_no_blocking)
ADD_SUBDIRECTORY (query_params)
ADD_SUBDIRECTORY (list_endpoints)
ADD_SUBDIRECTORY (request_logger_plugin)
Expand Down
36 changes: 36 additions & 0 deletions src/examples/hello_world_no_blocking/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (C) 2014 MediaSift Ltd.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

#
# Locate project sources
#
FILE (GLOB_RECURSE eg_hello_world_no_blocking_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp)

#
# Configure common project settings
#
SET (eg_hello_world_no_blocking_LIBS ${PROJECT_NAME} ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})

#
# Executable build rules
#
ADD_EXECUTABLE (eg_hello_world_no_blocking ${eg_hello_world_no_blocking_SRCS})
TARGET_LINK_LIBRARIES (eg_hello_world_no_blocking ${eg_hello_world_no_blocking_LIBS})
INSTALL (TARGETS eg_hello_world_no_blocking DESTINATION bin)
57 changes: 57 additions & 0 deletions src/examples/hello_world_no_blocking/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2014 MediaSift Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <served/served.hpp>
#include <unistd.h>

/* hello_world_no_blocking example
*
* This example is a basic example of served run in non blocking way.
*/
int main(int, char const**)
{
served::multiplexer mux;

mux.handle("/hello")
.get([](served::response & res, const served::request &) {
res << "Hello world";
});

std::cout << "Try this example with:" << std::endl;
std::cout << " curl http://localhost:8123/hello" << std::endl;

served::net::server server("127.0.0.1", "8123", mux);
server.run(10, false); // Run with a pool of 10 threads (not blocking)

std::cout << "We receive the control and we can do other stuff! Sleeping..." << std::endl;
int j = 0;
while (j < 6) {
std::cout << "Sleeping: " << j << std::endl;
j += 1;
sleep(1);
}

std::cout << "Time to stop the server" << std::endl;
server.stop();

return 0;
}
22 changes: 16 additions & 6 deletions src/served/net/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ using namespace served::net;
server::server( const std::string & address
, const std::string & port
, multiplexer & mux
, bool register_signals /* = true */
)
: _io_service()
, _signals(_io_service)
Expand All @@ -48,11 +49,13 @@ server::server( const std::string & address
* It is safe to register for the same signal multiple times in a program,
* provided all registration for the specified signal is made through Asio.
*/
_signals.add(SIGINT);
_signals.add(SIGTERM);
if ( register_signals ) {
_signals.add(SIGINT);
_signals.add(SIGTERM);
#if defined(SIGQUIT)
_signals.add(SIGQUIT);
_signals.add(SIGQUIT);
#endif // defined(SIGQUIT)
}

do_await_stop();

Expand All @@ -69,7 +72,7 @@ server::server( const std::string & address
}

void
server::run(int n_threads /* = 1 */)
server::run(int n_threads /* = 1 */, bool block /* = true */)
{
/*
* The io_service::run() call will block until all asynchronous operations
Expand All @@ -88,9 +91,16 @@ server::run(int n_threads /* = 1 */)
}
for ( auto & thread : v_threads )
{
if ( thread.joinable() )
if ( block )
{
if ( thread.joinable() )
{
thread.join();
}
}
else
{
thread.join();
thread.detach();
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions src/served/net/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,25 @@ class server
* @param address the address to bind to for incoming connections
* @param port the port to bind to for incoming connections
* @param mux the multiplexer to be used for forwarding requests to handlers
* @param register_signals specifies whether POSIX signals are captured or not
* @param read_timeout optional parameter that specifies a timeout for reading
* @param write_timeout optional parameter that specifies a timeout for writing
*/
explicit server( const std::string & address
, const std::string & port
, multiplexer & mux );
, multiplexer & mux
, bool register_signals = true );

/*
* A call that prompts the server into listening for HTTP requests.
*
* This call blocks until the server is closed, it accepts a value for how large the thread
* pool should be for distributing requests.
* This call accepts a value for how large the thread pool should be for distributing requests
* and another param which defines the blocking nature.
*
* @param n_threads the number of threads to pool for request handling
* @param block if n_threads > 0, defines whether this operation is blocking or not
*/
void run(int n_threads = 1);
void run(int n_threads = 1, bool block = true);

/*
* Stops the server from accepting requests.
Expand Down

0 comments on commit 193b141

Please sign in to comment.