sourcecode

Saturday, April 13, 2013

C/C++ with gearman (as client)

gearman library (libgearman) can be directly called in C/C++ programs. (gearmand --version 1.1.5)

http://gearman.info/libgearman/examples.html

And my makefile:

 a.out:callrev.cpp
           clang++ -o $@ $^ -std=c++11 -lgearman

Together with the worker command from last post, here is a complete C++ program as a client:


#include <libgearman/gearman.h>
#include <iostream>
#include <cstring>
using namespace std;

int main(){
 gearman_client_st* gclient = gearman_client_create(NULL);
 gearman_return_t gsc = gearman_client_add_server(gclient, "127.0.0.1",4730);

 auto status_print = [](gearman_return_t gserver_code){
  cout<<gserver_code<< " --  "; 
  if (gearman_success(gserver_code)) cout<<"success";
  if (gearman_failed(gserver_code)) cout<<"failed";
  if (gearman_continue(gserver_code)) cout<<"continue";
  cout<<endl;
 };

 status_print(gsc);

 const char* function_name = "wwcc";

 const char* unique = "whatever unique";
 const char* workload = "aa bb cc";
 size_t workload_size = strlen(workload);
 size_t result_size;
 gearman_return_t return_code;
 void* value = gearman_client_do(gclient, function_name, unique, workload, workload_size, &result_size, &return_code);

 status_print(return_code);
 const char* result = static_cast<char*>(value);
 cout<<result<<endl;


 free(value);
 gearman_client_free(gclient);
 
 return 0;
}

No comments: