Gearman Public API Documentation

examples/wc_worker.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 <errno.h>
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <string.h>
00018 #include <unistd.h>
00019 
00020 #include <libgearman/gearman.h>
00021 
00022 static void *wc(gearman_job_st *job, void *context, size_t *result_size,
00023                 gearman_return_t *ret_ptr);
00024 
00025 static void usage(char *name);
00026 
00027 int main(int argc, char *argv[])
00028 {
00029   int c;
00030   uint32_t count= 0;
00031   char *host= NULL;
00032   in_port_t port= 0;
00033   gearman_return_t ret;
00034   gearman_worker_st worker;
00035 
00036   while ((c = getopt(argc, argv, "c:h:p:")) != -1)
00037   {
00038     switch(c)
00039     {
00040     case 'c':
00041       count= (uint32_t)atoi(optarg);
00042       break;
00043 
00044     case 'h':
00045       host= optarg;
00046       break;
00047 
00048     case 'p':
00049       port= (in_port_t)atoi(optarg);
00050       break;
00051 
00052     default:
00053       usage(argv[0]);
00054       exit(1);
00055     }
00056   }
00057 
00058   if (gearman_worker_create(&worker) == NULL)
00059   {
00060     fprintf(stderr, "Memory allocation failure on worker creation\n");
00061     exit(1);
00062   }
00063 
00064   ret= gearman_worker_add_server(&worker, host, port);
00065   if (ret != GEARMAN_SUCCESS)
00066   {
00067     fprintf(stderr, "%s\n", gearman_worker_error(&worker));
00068     exit(1);
00069   }
00070 
00071   ret= gearman_worker_add_function(&worker, "wc", 0, wc, NULL);
00072   if (ret != GEARMAN_SUCCESS)
00073   {
00074     fprintf(stderr, "%s\n", gearman_worker_error(&worker));
00075     exit(1);
00076   }
00077 
00078   while (1)
00079   {
00080     ret= gearman_worker_work(&worker);
00081     if (ret != GEARMAN_SUCCESS)
00082     {
00083       fprintf(stderr, "%s\n", gearman_worker_error(&worker));
00084       break;
00085     }
00086 
00087     if (count > 0)
00088     {
00089       count--;
00090       if (count == 0)
00091         break;
00092     }
00093   }
00094 
00095   gearman_worker_free(&worker);
00096 
00097   return 0;
00098 }
00099 
00100 static void *wc(gearman_job_st *job, void *context, size_t *result_size,
00101                 gearman_return_t *ret_ptr)
00102 {
00103   const uint8_t *workload;
00104   uint8_t *result;
00105   size_t x;
00106   uint64_t count= 0;
00107   (void)context;
00108 
00109   workload= gearman_job_workload(job);
00110   *result_size= gearman_job_workload_size(job);
00111 
00112   result= malloc(21); /* Max digits for a 64 bit int. */
00113   if (result == NULL)
00114   {
00115     fprintf(stderr, "malloc:%d\n", errno);
00116     *ret_ptr= GEARMAN_WORK_FAIL;
00117     return NULL;
00118   }
00119 
00120   if (workload != NULL)
00121   {
00122     if (workload[0] != ' ' && workload[0] != '\t' && workload[0] != '\n')
00123       count++;
00124 
00125     for (x= 0; x < *result_size; x++)
00126     {
00127       if (workload[x] != ' ' && workload[x] != '\t' && workload[x] != '\n')
00128         continue;
00129 
00130       count++;
00131 
00132       while (workload[x] == ' ' || workload[x] == '\t' || workload[x] == '\n')
00133       {
00134         x++;
00135         if (x == *result_size)
00136         {
00137           count--;
00138           break;
00139         }
00140       }
00141     }
00142   }
00143 
00144   snprintf((char *)result, 21, "%" PRIu64, count);
00145 
00146   printf("Job=%s Workload=%.*s Result=%s\n", gearman_job_handle(job),
00147          (int)*result_size, workload, result);
00148 
00149   *result_size= strlen((char *)result) + 1;
00150 
00151   *ret_ptr= GEARMAN_SUCCESS;
00152   return result;
00153 }
00154 
00155 static void usage(char *name)
00156 {
00157   printf("\nusage: %s [-h <host>] [-p <port>]\n", name);
00158   printf("\t-h <host> - job server host\n");
00159   printf("\t-p <port> - job server port\n");
00160 }