Gearman Public API Documentation

examples/reverse_client.c
Go to the documentation of this file.
00001 /* Gearman server and library
00002  * Copyright (C) 2008 Brian Aker, Eric Day
00003  * All rights reserved.
00004  *
00005  * Use and distribution licensed under the BSD license.  See
00006  * the COPYING file in the parent directory for full text.
00007  */
00008 
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <string.h>
00017 #include <unistd.h>
00018 
00019 #include <libgearman/gearman.h>
00020 
00021 static void usage(char *name);
00022 
00023 int main(int argc, char *argv[])
00024 {
00025   int c;
00026   char *host= NULL;
00027   in_port_t port= 0;
00028   int timeout= -1;
00029   gearman_return_t ret;
00030   gearman_client_st client;
00031   char *result;
00032   size_t result_size;
00033   uint32_t numerator;
00034   uint32_t denominator;
00035 
00036   while ((c = getopt(argc, argv, "h:p:t:")) != -1)
00037   {
00038     switch(c)
00039     {
00040     case 'h':
00041       host= optarg;
00042       break;
00043 
00044     case 'p':
00045       port= (in_port_t)atoi(optarg);
00046       break;
00047 
00048     case 't':
00049       timeout= atoi(optarg);
00050       break;
00051 
00052     default:
00053       usage(argv[0]);
00054       exit(1);
00055     }
00056   }
00057 
00058   if (argc != (optind + 1))
00059   {
00060     usage(argv[0]);
00061     exit(1);
00062   }
00063 
00064   if (gearman_client_create(&client) == NULL)
00065   {
00066     fprintf(stderr, "Memory allocation failure on client creation\n");
00067     exit(1);
00068   }
00069 
00070   if (timeout >= 0)
00071     gearman_client_set_timeout(&client, timeout);
00072 
00073   ret= gearman_client_add_server(&client, host, port);
00074   if (ret != GEARMAN_SUCCESS)
00075   {
00076     fprintf(stderr, "%s\n", gearman_client_error(&client));
00077     exit(1);
00078   }
00079 
00080   while (1)
00081   {
00082     result= (char *)gearman_client_do(&client, "reverse", NULL,
00083                                       (void *)argv[optind],
00084                                       (size_t)strlen(argv[optind]),
00085                                       &result_size, &ret);
00086     if (ret == GEARMAN_WORK_DATA)
00087     {
00088       printf("Data=%.*s\n", (int)result_size, result);
00089       free(result);
00090       continue;
00091     }
00092     else if (ret == GEARMAN_WORK_STATUS)
00093     {
00094       gearman_client_do_status(&client, &numerator, &denominator);
00095       printf("Status: %u/%u\n", numerator, denominator);
00096       continue;
00097     }
00098     else if (ret == GEARMAN_SUCCESS)
00099     {
00100       printf("Result=%.*s\n", (int)result_size, result);
00101       free(result);
00102     }
00103     else if (ret == GEARMAN_WORK_FAIL)
00104       fprintf(stderr, "Work failed\n");
00105     else
00106       fprintf(stderr, "%s\n", gearman_client_error(&client));
00107 
00108     break;
00109   }
00110 
00111   gearman_client_free(&client);
00112 
00113   return 0;
00114 }
00115 
00116 static void usage(char *name)
00117 {
00118   printf("\nusage: %s [-h <host>] [-p <port>] <string>\n", name);
00119   printf("\t-h <host>    - job server host\n");
00120   printf("\t-p <port>    - job server port\n");
00121   printf("\t-t <timeout> - timeout in milliseconds\n");
00122 }