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

Commit

Permalink
Added register_signal bool. Added none blocking example.
Browse files Browse the repository at this point in the history
  • Loading branch information
luismartingil committed Nov 7, 2018
1 parent a12f529 commit 5c6edb1
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 4 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;
}
9 changes: 6 additions & 3 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 Down
4 changes: 3 additions & 1 deletion src/served/net/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ 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.
Expand Down

0 comments on commit 5c6edb1

Please sign in to comment.