/
test-client.cpp
91 lines (80 loc) · 2.09 KB
/
test-client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//#include "gear_config.h"
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <libgearman/gearman.h>
//#include "util/string.hpp"
//#include "gearmand/error.hpp"
//#define std::cerr<< std::cerr<<
int main()
{
gearman_client_st client;
if (gearman_client_create(&client) == NULL)
{
std::cerr << "Memory allocation failure on client creation" << std::endl;
return EXIT_FAILURE;
}
//
int timeout=-1;
//
if (timeout >= 0)
{
gearman_client_set_timeout(&client, timeout);
}
gearman_return_t ret= gearman_client_add_server(&client, "localhost", GEARMAN_DEFAULT_TCP_PORT);
if (ret != GEARMAN_SUCCESS)
{
std::cerr<<(gearman_client_error(&client));
return EXIT_FAILURE;
}
if (gearman_failed(gearman_client_set_identifier(&client, "client1", strlen("client1"))))
{
std::cerr<<(gearman_client_error(&client));
return EXIT_FAILURE;
}
int exit_code= EXIT_SUCCESS;
int count=1; //if status is to be returned then don't change count on status receive
do
{
size_t result_size;
char *result;
result= (char *)gearman_client_do(&client, "make_call", NULL,
"scheme-string", strlen("scheme-string"),
&result_size, &ret);
if (ret == GEARMAN_WORK_DATA)
{
std::cout.write(result, result_size);
free(result);
continue;
}
else if (ret == GEARMAN_WORK_STATUS)
{
uint32_t numerator;
uint32_t denominator;
gearman_client_do_status(&client, &numerator, &denominator);
std::clog << "Status: " << numerator << "/" << denominator << std::endl;
continue;
}
else if (ret == GEARMAN_SUCCESS)
{
std::cout.write(result, result_size);
free(result);
}
else if (ret == GEARMAN_WORK_FAIL)
{
std::cerr<<("Work failed");
exit_code= EXIT_FAILURE;
break;
}
else
{
std::cerr<<(gearman_client_error(&client));
exit_code= EXIT_FAILURE;
break;
}
--count;
} while (count);
gearman_client_free(&client);
return exit_code;
}