Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Commit

Permalink
Retrieve string message from C worker, and return into JobResult
Browse files Browse the repository at this point in the history
  • Loading branch information
valnoel committed Dec 13, 2019
1 parent 4bb5733 commit ec2564a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 21 deletions.
18 changes: 9 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ impl MessageEvent for CWorkerEvent {

let job_id = job.job_id;
debug!("Process job: {:?}", job_id);
let return_code = call_worker_process(job);
debug!("Returned code: {:?}", return_code);
match return_code {
0 => Ok(JobResult::new(job_id, JobStatus::Completed, vec![])),
_ => {
let result = JobResult::new(job_id, JobStatus::Error, vec![]).with_message(format!(
"Worker process returned error code: {:?}",
return_code
));
let process_return = call_worker_process(job);
debug!("Returned code: {:?}", process_return);
match process_return {
ProcessReturn { code: 0, message } => {
Ok(JobResult::new(job_id, JobStatus::Completed, vec![]).with_message(message))
}
ProcessReturn { code, message } => {
let result = JobResult::new(job_id, JobStatus::Error, vec![])
.with_message(format!("{} (code: {:?})", message, code));
Err(MessageError::ProcessingError(result))
}
}
Expand Down
45 changes: 34 additions & 11 deletions src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type ProcessFunc = unsafe fn(
callback: GetParameterValueCallback,
check_error: CheckLastError,
logger: LogCallback,
output_message: *mut c_char,
) -> c_int;

type CheckLastError = extern "C" fn() -> c_int;
Expand All @@ -66,7 +67,7 @@ pub static PROCESS_FUNCTION: &str = "process";
extern "C" fn check_error() -> c_int {
let last_error = LAST_ERROR.with(|last_error| last_error.replace(None));
if let Some(error_message) = last_error {
return_with_error(error_message)
return_with_error(error_message).code
} else {
0
}
Expand Down Expand Up @@ -102,9 +103,14 @@ extern "C" fn log(value: *const c_char) {
* Utility functions
************************/

fn return_with_error(message: String) -> i32 {
error!("{}", message);
1
#[derive(Debug)]
pub struct ProcessReturn {
pub code: i32,
pub message: String,
}

fn return_with_error(message: String) -> ProcessReturn {
ProcessReturn { code: 1, message }
}

fn get_parameter_type_from_c_str(c_str: &CStr) -> ParameterType {
Expand Down Expand Up @@ -210,8 +216,10 @@ pub fn get_worker_parameters() -> Vec<Parameter> {
parameters
}

pub fn call_worker_process(job: Job) -> i32 {
match libloading::Library::new(get_library_file_path()) {
pub fn call_worker_process(job: Job) -> ProcessReturn {
let library = get_library_file_path();
debug!("Call worker process from library: {}", library);
match libloading::Library::new(library) {
Ok(worker_lib) => unsafe {
match get_library_function(&worker_lib, PROCESS_FUNCTION)
as Result<libloading::Symbol<ProcessFunc>, String>
Expand All @@ -233,16 +241,29 @@ pub fn call_worker_process(job: Job) -> i32 {
let boxed_job_params_ptrs = Box::new(job_params_ptrs);
let job_params_ptrs_ptr = Box::into_raw(boxed_job_params_ptrs);

// Get output message pointer
let message_ptr = libc::malloc(2048) as *mut c_char;

// Call C worker process function
process_func(
let return_code = process_func(
job_params_ptrs_ptr as *mut c_void,
get_parameter_value,
check_error,
log,
)
message_ptr,
);

// Retrieve message as string and free pointer
let message = get_c_string!(message_ptr);
libc::free(message_ptr as *mut libc::c_void);

ProcessReturn {
code: return_code,
message,
}
}
Err(error) => return_with_error(format!(
"Could access {:?} fonction from worker library: {:?}",
"Could not access {:?} function from worker library: {:?}",
PROCESS_FUNCTION, error
)),
}
Expand All @@ -269,7 +290,8 @@ pub fn test_c_binding_process() {

let job = Job::new(message).unwrap();
let returned_code = call_worker_process(job);
assert_eq!(0, returned_code);
assert_eq!(0, returned_code.code);
assert_eq!("Everything worked well!", returned_code.message);
}

#[test]
Expand All @@ -287,5 +309,6 @@ pub fn test_c_binding_failing_process() {

let job = Job::new(message).unwrap();
let returned_code = call_worker_process(job);
assert_eq!(1, returned_code);
assert_eq!(1, returned_code.code);
assert_eq!("Something went wrong...", returned_code.message);
}
11 changes: 10 additions & 1 deletion worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ typedef void* (*Logger)(const char*);
* Check error callback
*/
typedef int* (*CheckError)();
/**
* Message to return as a response
*/
typedef char* OutputMessage;


/**
Expand Down Expand Up @@ -97,7 +101,7 @@ void get_parameters(Parameter* parameters) {
* @param checkError Check error callback
* @param logger Rust logger callback
*/
int process(JobParameters job, GetParameterValueCallback parametersValueGetter, CheckError checkError, Logger logger) {
int process(JobParameters job, GetParameterValueCallback parametersValueGetter, CheckError checkError, Logger logger, OutputMessage message) {
// Print message through the Rust internal logger
logger("Start C Worker process...");

Expand All @@ -106,11 +110,16 @@ int process(JobParameters job, GetParameterValueCallback parametersValueGetter,

// Check whether an error occurred parsing job parameters
if(checkError() != 0) {
const char* message_str = "Something went wrong...";
memcpy(message, message_str, strlen(message_str));
return 1;
}

// Print value through the Rust internal logger
logger(value);

const char* message_str = "Everything worked well!";
memcpy(message, message_str, strlen(message_str));
return 0;
}

Expand Down

0 comments on commit ec2564a

Please sign in to comment.