~clint-fewbar/ubuntu/precise/gearmand/drop-unneeded-patches

« back to all changes in this revision

Viewing changes to examples/reverse_worker.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2009-09-28 21:43:31 UTC
  • mto: (1.2.3 upstream) (6.1.1 sid)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20090928214331-9bku0d3v1b1ypgp4
ImportĀ upstreamĀ versionĀ 0.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2
 
 * 
3
 
 *  Gearmand client and server library.
4
 
 *
5
 
 *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
6
 
 *  Copyright (C) 2008 Brian Aker, Eric Day
7
 
 *  All rights reserved.
8
 
 *
9
 
 *  Redistribution and use in source and binary forms, with or without
10
 
 *  modification, are permitted provided that the following conditions are
11
 
 *  met:
12
 
 *
13
 
 *      * Redistributions of source code must retain the above copyright
14
 
 *  notice, this list of conditions and the following disclaimer.
15
 
 *
16
 
 *      * Redistributions in binary form must reproduce the above
17
 
 *  copyright notice, this list of conditions and the following disclaimer
18
 
 *  in the documentation and/or other materials provided with the
19
 
 *  distribution.
20
 
 *
21
 
 *      * The names of its contributors may not be used to endorse or
22
 
 *  promote products derived from this software without specific prior
23
 
 *  written permission.
24
 
 *
25
 
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
 
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
 
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28
 
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29
 
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30
 
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31
 
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32
 
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33
 
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34
 
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
 
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
 
 *
37
 
 */
38
 
 
39
 
 
40
 
 
41
 
#include <config.h>
42
 
 
43
 
#include <cerrno>
44
 
#include <cstdio>
45
 
#include <cstdlib>
46
 
#include <cstring>
47
 
#include <iostream>
48
 
#include <signal.h>
49
 
 
50
 
#include <libgearman/gearman.h>
51
 
#include <boost/program_options.hpp>
52
 
 
53
 
struct reverse_worker_options_t
54
 
{
55
 
  bool chunk;
56
 
  bool status;
57
 
  bool unique;
58
 
 
59
 
  reverse_worker_options_t():
60
 
    chunk(false),
61
 
    status(false),
62
 
    unique(false)
63
 
  { }
64
 
};
65
 
 
66
 
#ifndef __INTEL_COMPILER
67
 
#pragma GCC diagnostic ignored "-Wold-style-cast"
68
 
#endif
69
 
 
70
 
static void *reverse(gearman_job_st *job, void *context,
71
 
                     size_t *result_size, gearman_return_t *ret_ptr);
72
 
 
73
 
int main(int args, char *argv[])
74
 
{
75
 
  uint32_t count;
76
 
  reverse_worker_options_t options;
77
 
  int timeout;
78
 
 
79
 
  in_port_t port;
80
 
  std::string host;
81
 
  boost::program_options::options_description desc("Options");
82
 
  desc.add_options()
83
 
    ("help", "Options related to the program.")
84
 
    ("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
85
 
    ("port,p", boost::program_options::value<in_port_t>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT), "Port number use for connection")
86
 
    ("count,c", boost::program_options::value<uint32_t>(&count)->default_value(0), "Number of jobs to run before exiting")
87
 
    ("timeout,u", boost::program_options::value<int>(&timeout)->default_value(-1), "Timeout in milliseconds")
88
 
    ("chunk,d", boost::program_options::bool_switch(&options.chunk)->default_value(false), "Send result back in data chunks")
89
 
    ("status,s", boost::program_options::bool_switch(&options.status)->default_value(false), "Send status updates and sleep while running job")
90
 
    ("unique,u", boost::program_options::bool_switch(&options.unique)->default_value(false), "When grabbing jobs, grab the uniqie id")
91
 
            ;
92
 
 
93
 
  boost::program_options::variables_map vm;
94
 
  try
95
 
  {
96
 
    boost::program_options::store(boost::program_options::parse_command_line(args, argv, desc), vm);
97
 
    boost::program_options::notify(vm);
98
 
  }
99
 
  catch(std::exception &e)
100
 
  { 
101
 
    std::cout << e.what() << std::endl;
102
 
    return EXIT_FAILURE;
103
 
  }
104
 
 
105
 
  if (vm.count("help"))
106
 
  {
107
 
    std::cout << desc << std::endl;
108
 
    return EXIT_SUCCESS;
109
 
  }
110
 
 
111
 
  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
112
 
  {
113
 
    std::cerr << "signal:" << strerror(errno) << std::endl;
114
 
    return EXIT_FAILURE;
115
 
  }
116
 
 
117
 
  gearman_worker_st worker;
118
 
  if (gearman_worker_create(&worker) == NULL)
119
 
  {
120
 
    std::cerr << "Memory allocation failure on worker creation." << std::endl;
121
 
    return EXIT_FAILURE;
122
 
  }
123
 
 
124
 
  if (options.unique)
125
 
    gearman_worker_add_options(&worker, GEARMAN_WORKER_GRAB_UNIQ);
126
 
 
127
 
  if (timeout >= 0)
128
 
    gearman_worker_set_timeout(&worker, timeout);
129
 
 
130
 
  gearman_return_t ret;
131
 
  ret= gearman_worker_add_server(&worker, host.c_str(), port);
132
 
  if (ret != GEARMAN_SUCCESS)
133
 
  {
134
 
    std::cerr << gearman_worker_error(&worker) << std::endl;
135
 
    return EXIT_FAILURE;
136
 
  }
137
 
 
138
 
  ret= gearman_worker_add_function(&worker, "reverse", 0, reverse, &options);
139
 
  if (ret != GEARMAN_SUCCESS)
140
 
  {
141
 
    std::cerr << gearman_worker_error(&worker) << std::endl;
142
 
    return EXIT_FAILURE;
143
 
  }
144
 
 
145
 
  while (1)
146
 
  {
147
 
    ret= gearman_worker_work(&worker);
148
 
    if (ret != GEARMAN_SUCCESS)
149
 
    {
150
 
      std::cerr << gearman_worker_error(&worker) << std::endl;
151
 
      break;
152
 
    }
153
 
 
154
 
    if (count > 0)
155
 
    {
156
 
      count--;
157
 
      if (count == 0)
158
 
        break;
159
 
    }
160
 
  }
161
 
 
162
 
  gearman_worker_free(&worker);
163
 
 
164
 
  return EXIT_SUCCESS;
165
 
}
166
 
 
167
 
static void *reverse(gearman_job_st *job, void *context,
168
 
                     size_t *result_size, gearman_return_t *ret_ptr)
169
 
{
170
 
  reverse_worker_options_t options= *((reverse_worker_options_t *)context);
171
 
 
172
 
 
173
 
  const char *workload;
174
 
  workload= (const char *)gearman_job_workload(job);
175
 
  *result_size= gearman_job_workload_size(job);
176
 
 
177
 
  char *result;
178
 
  result= (char *)malloc(*result_size);
179
 
  if (result == NULL)
180
 
  {
181
 
    perror("malloc");
182
 
    *ret_ptr= GEARMAN_WORK_FAIL;
183
 
    return NULL;
184
 
  }
185
 
 
186
 
  size_t x;
187
 
  size_t y;
188
 
  for (y= 0, x= *result_size; x; x--, y++)
189
 
  {
190
 
    result[y]= ((uint8_t *)workload)[x - 1];
191
 
 
192
 
    if (options.chunk)
193
 
    {
194
 
      *ret_ptr= gearman_job_send_data(job, &(result[y]), 1);
195
 
      if (*ret_ptr != GEARMAN_SUCCESS)
196
 
      {
197
 
        free(result);
198
 
        return NULL;
199
 
      }
200
 
    }
201
 
 
202
 
    if (options.status)
203
 
    {
204
 
      *ret_ptr= gearman_job_send_status(job, (uint32_t)y,
205
 
                                        (uint32_t)*result_size);
206
 
      if (*ret_ptr != GEARMAN_SUCCESS)
207
 
      {
208
 
        free(result);
209
 
        return NULL;
210
 
      }
211
 
 
212
 
      sleep(1);
213
 
    }
214
 
  }
215
 
 
216
 
  std::cout << "Job=" << gearman_job_handle(job);
217
 
 
218
 
  if (options.unique)
219
 
  {
220
 
    std::cout << "Unique=" << gearman_job_unique(job);
221
 
  }
222
 
 
223
 
 
224
 
  std::cout << "  Workload=";
225
 
  std::cout.write(workload, *result_size);
226
 
 
227
 
  std::cout << "  Result=";
228
 
  std::cout.write(result, *result_size);
229
 
 
230
 
  std::cout << std::endl;
231
 
 
232
 
  *ret_ptr= GEARMAN_SUCCESS;
233
 
 
234
 
  if (options.chunk)
235
 
  {
236
 
    *result_size= 0;
237
 
    return NULL;
238
 
  }
239
 
 
240
 
  return result;
241
 
}