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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
/* Gearman server and library
* Copyright (C) 2008 Brian Aker, Eric Day
* All rights reserved.
*
* Use and distribution licensed under the BSD license. See
* the COPYING file in the parent directory for full text.
*/
/**
* @file
* @brief Example Client
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgearman/gearman.h>
static void usage(char *name);
int main(int argc, char *argv[])
{
int c;
char *host= NULL;
in_port_t port= 0;
int timeout= -1;
gearman_return_t ret;
gearman_client_st client;
char *result;
size_t result_size;
uint32_t numerator;
uint32_t denominator;
while ((c = getopt(argc, argv, "h:p:t:")) != -1)
{
switch(c)
{
case 'h':
host= optarg;
break;
case 'p':
port= (in_port_t)atoi(optarg);
break;
case 't':
timeout= atoi(optarg);
break;
default:
usage(argv[0]);
exit(1);
}
}
if (argc != (optind + 1))
{
usage(argv[0]);
exit(1);
}
if (gearman_client_create(&client) == NULL)
{
fprintf(stderr, "Memory allocation failure on client creation\n");
exit(1);
}
if (timeout >= 0)
gearman_client_set_timeout(&client, timeout);
ret= gearman_client_add_server(&client, host, port);
if (ret != GEARMAN_SUCCESS)
{
fprintf(stderr, "%s\n", gearman_client_error(&client));
exit(1);
}
while (1)
{
result= (char *)gearman_client_do(&client, "reverse", NULL,
(void *)argv[optind],
(size_t)strlen(argv[optind]),
&result_size, &ret);
if (ret == GEARMAN_WORK_DATA)
{
printf("Data=%.*s\n", (int)result_size, result);
free(result);
continue;
}
else if (ret == GEARMAN_WORK_STATUS)
{
gearman_client_do_status(&client, &numerator, &denominator);
printf("Status: %u/%u\n", numerator, denominator);
continue;
}
else if (ret == GEARMAN_SUCCESS)
{
printf("Result=%.*s\n", (int)result_size, result);
free(result);
}
else if (ret == GEARMAN_WORK_FAIL)
fprintf(stderr, "Work failed\n");
else
fprintf(stderr, "%s\n", gearman_client_error(&client));
break;
}
gearman_client_free(&client);
return 0;
}
static void usage(char *name)
{
printf("\nusage: %s [-h <host>] [-p <port>] <string>\n", name);
printf("\t-h <host> - job server host\n");
printf("\t-p <port> - job server port\n");
printf("\t-t <timeout> - timeout in milliseconds\n");
}
|