File: wc_worker.c

package info (click to toggle)
gearmand 0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,876 kB
  • ctags: 1,656
  • sloc: ansic: 17,412; sh: 10,894; makefile: 76; cpp: 9
file content (160 lines) | stat: -rw-r--r-- 3,224 bytes parent folder | download
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* 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 Worker
 */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <libgearman/gearman.h>

static void *wc(gearman_job_st *job, void *context, size_t *result_size,
                gearman_return_t *ret_ptr);

static void usage(char *name);

int main(int argc, char *argv[])
{
  int c;
  uint32_t count= 0;
  char *host= NULL;
  in_port_t port= 0;
  gearman_return_t ret;
  gearman_worker_st worker;

  while ((c = getopt(argc, argv, "c:h:p:")) != -1)
  {
    switch(c)
    {
    case 'c':
      count= (uint32_t)atoi(optarg);
      break;

    case 'h':
      host= optarg;
      break;

    case 'p':
      port= (in_port_t)atoi(optarg);
      break;

    default:
      usage(argv[0]);
      exit(1);
    }
  }

  if (gearman_worker_create(&worker) == NULL)
  {
    fprintf(stderr, "Memory allocation failure on worker creation\n");
    exit(1);
  }

  ret= gearman_worker_add_server(&worker, host, port);
  if (ret != GEARMAN_SUCCESS)
  {
    fprintf(stderr, "%s\n", gearman_worker_error(&worker));
    exit(1);
  }

  ret= gearman_worker_add_function(&worker, "wc", 0, wc, NULL);
  if (ret != GEARMAN_SUCCESS)
  {
    fprintf(stderr, "%s\n", gearman_worker_error(&worker));
    exit(1);
  }

  while (1)
  {
    ret= gearman_worker_work(&worker);
    if (ret != GEARMAN_SUCCESS)
    {
      fprintf(stderr, "%s\n", gearman_worker_error(&worker));
      break;
    }

    if (count > 0)
    {
      count--;
      if (count == 0)
        break;
    }
  }

  gearman_worker_free(&worker);

  return 0;
}

static void *wc(gearman_job_st *job, void *context, size_t *result_size,
                gearman_return_t *ret_ptr)
{
  const uint8_t *workload;
  uint8_t *result;
  size_t x;
  uint64_t count= 0;
  (void)context;

  workload= gearman_job_workload(job);
  *result_size= gearman_job_workload_size(job);

  result= malloc(21); /* Max digits for a 64 bit int. */
  if (result == NULL)
  {
    fprintf(stderr, "malloc:%d\n", errno);
    *ret_ptr= GEARMAN_WORK_FAIL;
    return NULL;
  }

  if (workload != NULL)
  {
    if (workload[0] != ' ' && workload[0] != '\t' && workload[0] != '\n')
      count++;

    for (x= 0; x < *result_size; x++)
    {
      if (workload[x] != ' ' && workload[x] != '\t' && workload[x] != '\n')
        continue;

      count++;

      while (workload[x] == ' ' || workload[x] == '\t' || workload[x] == '\n')
      {
        x++;
        if (x == *result_size)
        {
          count--;
          break;
        }
      }
    }
  }

  snprintf((char *)result, 21, "%" PRIu64, count);

  printf("Job=%s Workload=%.*s Result=%s\n", gearman_job_handle(job),
         (int)*result_size, workload, result);

  *result_size= strlen((char *)result) + 1;

  *ret_ptr= GEARMAN_SUCCESS;
  return result;
}

static void usage(char *name)
{
  printf("\nusage: %s [-h <host>] [-p <port>]\n", name);
  printf("\t-h <host> - job server host\n");
  printf("\t-p <port> - job server port\n");
}