2 #include <ail/file.hpp>
3 #include <ail/string.hpp>
4 #include <boost/foreach.hpp>
5 #include <theodwalha/module_manager.hpp>
6 #include <theodwalha/configuration.hpp>
8 bool module_manager::load_modules(std::string
const & directory
)
14 if(!ail::read_directory(directory
, files
, directories
))
17 BOOST_FOREACH(std::string
const & file
, files
)
19 std::string path
= ail::join_paths(directory
, file
);
21 ail::dynamic_module
* new_module
= new ail::dynamic_module();
22 if(!new_module
->load(path
))
24 std::cout
<< "Failed to load module " << path
<< std::endl
;
28 void * function_pointer
;
29 if(!new_module
->get_function("get_process_request_address", function_pointer
))
31 std::cout
<< "Failed to retrieve function pointer from module " << path
<< std::endl
;
35 std::cout
<< "Loaded " << path
<< std::endl
;
37 typedef void * (* get_process_request_address_type
)();
38 get_process_request_address_type get_process_request_address
= reinterpret_cast<get_process_request_address_type
>(function_pointer
);
39 void * void_pointer
= get_process_request_address();
41 //std::cout << "get_process_request_address returned: " << ail::hex_string_32(reinterpret_cast<uword>(void_pointer)) << std::endl;
43 module_entry current_entry
;
44 current_entry
.name
= path
;
45 current_entry
.module_pointer
= new_module
;
46 current_entry
.handler
= reinterpret_cast<request_handler_function_type
*>(void_pointer
);
47 module_entries
.push_back(current_entry
);
53 bool module_manager::process_request(http_request
& request
, module_result
& result
)
55 //std::cout << "module_manager::process_request" << std::endl;
57 BOOST_FOREACH(module_entry
& entry
, module_entries
)
59 //std::cout << entry.name << ": " << std::endl;
61 entry
.handler(request
, result
);
67 case request_handler_result::no_match
:
68 std::cout
<< "No match" << std::endl
;
71 case request_handler_result::success
:
72 //std::cout << "Success!" << std::endl;
76 case request_handler_result::error
:
77 std::cout
<< "Error: " << result
.error_message
<< std::endl
;
81 std::cout
<< "Invalid result specified: " << ail::hex_string_32(static_cast<uword
>(result
.result
)) << std::endl
;
85 if(result
.command
== request_handler_command::stop
)
89 std::cout
<< "None of the modules succeeded at processing the request" << std::endl
;