Skip to content

Commit

Permalink
Add tasks to service runloop feature; resolved #26
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-crowhurst committed Aug 21, 2015
1 parent d42a7ec commit 351de17
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
3 changes: 3 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ target_link_libraries( session_data ${CMAKE_PROJECT_NAME} )
add_executable( rules_engine rules_engine/source/example.cpp )
target_link_libraries( rules_engine ${CMAKE_PROJECT_NAME} )

add_executable( schedule_work_on_service_runloop schedule_work_on_service_runloop/source/example.cpp )
target_link_libraries( schedule_work_on_service_runloop ${CMAKE_PROJECT_NAME} )

if ( PAM_FOUND )
add_executable( pam_authentication pam_authentication/source/example.cpp )
target_link_libraries( pam_authentication ${CMAKE_PROJECT_NAME} pam )
Expand Down
27 changes: 13 additions & 14 deletions example/schedule_work_on_service_runloop/source/example.cpp
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
/*
* Example illustrating how to schedule work on service runloop.
* Example illustrating how to scheduled work on the service runloop.
*
* Server Usage:
* ./distribution/example/schedule_work_on_service_runloop
*
* Client Usage:
* curl -w'\n' -v -X GET 'http://localhost:1984/api/create/task1'
* Watch service console output to ensure tasks run.
*/

#include <chrono>
#include <memory>
#include <string>
#include <vector>
#include <cstdlib>
#include <restbed>
#include <functional>

using namespace std;
using namespace restbed;

vector< string > tasks;
void multi_run_task( void )
{
fprintf( stderr, "multi run task executed.\n" );
}

void worker( const shared_ptr< Service >& service )
void single_run_task( void )
{
if ( not tasks.empty( ) )
{
fprintf( stderr, "processed task '%s'\n", tasks.front( ).data( ) );
}
fprintf( stderr, "single run task executed.\n" );
}

void create( const shared_ptr< Session >& session )
{
auto request = session->get_request( );
auto name = request->get_path_parameter( "name" );
tasks.push_back( name );
session->close( 200 );
}

int main( const int, const char** )
{
auto resource = make_shared< Resource >( );
resource->set_path( "/api/create/{name: [a-z]*}" );
resource->set_path( "/api" );
resource->set_method_handler( "GET", create );

auto settings = make_shared< Settings >( );
settings->set_port( 1984 );

auto service = make_shared< Service >( );
service->publish( resource );
service->schedule( bind( worker, service ) );
service->schedule( single_run_task );
service->schedule( multi_run_task, chrono::milliseconds( 1000 ) );
service->start( settings );

return EXIT_SUCCESS;
Expand Down
2 changes: 1 addition & 1 deletion source/corvusoft/restbed/detail/service_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace restbed
logger( nullptr ),
supported_methods( ),
settings( nullptr ),
io_service( nullptr ),
io_service( new ::io_service ),
session_manager( nullptr ),
rules( ),
#ifdef BUILD_SSL
Expand Down
2 changes: 1 addition & 1 deletion source/corvusoft/restbed/detail/socket_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using asio::error_code;
using asio::steady_timer;

#ifdef BUILD_SSL
using asio::ssl::stream;
using asio::ssl::stream;
#endif

namespace restbed
Expand Down
23 changes: 20 additions & 3 deletions source/corvusoft/restbed/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//External Includes
#include <asio.hpp>
#ifdef BUILD_SSL
#include <asio/ssl.hpp>
#include <asio/ssl.hpp>
#endif

//System Namespaces
Expand All @@ -39,6 +39,7 @@ using std::make_shared;
using std::stable_sort;
using std::runtime_error;
using std::invalid_argument;
using std::chrono::milliseconds;

//Project Namespaces
using restbed::detail::ServiceImpl;
Expand Down Expand Up @@ -119,8 +120,6 @@ namespace restbed
return lhs->get_priority( ) < rhs->get_priority( );
} );

m_pimpl->io_service = make_shared< io_service >( );

m_pimpl->http_start( );
#ifdef BUILD_SSL
m_pimpl->https_start( );
Expand Down Expand Up @@ -234,6 +233,24 @@ namespace restbed
}
}

void Service::schedule( const function< void ( void ) >& task, const milliseconds& interval )
{
if ( interval == milliseconds::zero( ) )
{
m_pimpl->io_service->post( task );
return;
}

auto timer = new asio::steady_timer( *m_pimpl->io_service );
timer->expires_from_now( interval );
timer->async_wait( [ this, task, interval, timer ]( const asio::error_code& )
{
task( );
schedule( task, interval );
delete timer;
} );
}

void Service::set_logger( const shared_ptr< Logger >& value )
{
if ( m_pimpl->is_running )
Expand Down
3 changes: 3 additions & 0 deletions source/corvusoft/restbed/service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

//System Includes
#include <map>
#include <chrono>
#include <memory>
#include <string>
#include <stdexcept>
Expand Down Expand Up @@ -63,6 +64,8 @@ namespace restbed

void suppress( const std::shared_ptr< const Resource >& resource );

void schedule( const std::function< void ( void ) >& task, const std::chrono::milliseconds& interval = std::chrono::milliseconds::zero( ) );

//Getters

//Setters
Expand Down

0 comments on commit 351de17

Please sign in to comment.