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 "gear_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 bool verbose;
59
reverse_worker_options_treverse_worker_options_t60 reverse_worker_options_t():
61 chunk(false),
62 status(false),
63 unique(false),
64 verbose(true)
65 { }
66 };
67
68 #ifndef __INTEL_COMPILER
69 #pragma GCC diagnostic ignored "-Wold-style-cast"
70 #endif
71
reverse_worker(gearman_job_st * job,void * context)72 static gearman_return_t reverse_worker(gearman_job_st *job, void *context)
73 {
74 reverse_worker_options_t options= *((reverse_worker_options_t *)context);
75
76 const char *workload= (const char *)gearman_job_workload(job);
77 const size_t workload_size= gearman_job_workload_size(job);
78
79 if (options.verbose)
80 {
81 std::cout << "Recieved " << workload_size << " bytes" << std::endl;
82 }
83
84 std::vector<char> result;
85 result.resize(workload_size);
86
87 // Copy workload
88 for (size_t y= 0, x= workload_size; x; x--, y++)
89 {
90 result[y]= ((uint8_t *)workload)[x - 1];
91 }
92
93 if (options.chunk) // Chunk the result set
94 {
95 for (size_t y= 0, x= workload_size; x; x--, y++)
96 {
97 if (gearman_failed(gearman_job_send_data(job, &result[y], 1)))
98 {
99 return GEARMAN_ERROR;
100 }
101
102 if (options.status)
103 {
104 // Notice that we send based on y divided by zero.
105 if (gearman_failed(gearman_job_send_status(job, (uint32_t)y, (uint32_t)workload_size)))
106 {
107 return GEARMAN_ERROR;
108 }
109 }
110 }
111 }
112 else
113 {
114 if (options.status)
115 {
116 // Notice that we send based on y divided by zero.
117 if (gearman_failed(gearman_job_send_status(job, (uint32_t)0, (uint32_t)workload_size)))
118 {
119 return GEARMAN_ERROR;
120 }
121 }
122
123 if (gearman_failed(gearman_job_send_data(job, &result[0], workload_size)))
124 {
125 return GEARMAN_ERROR;
126 }
127
128 if (options.status)
129 {
130 // Notice that we send based on y divided by zero.
131 if (gearman_failed(gearman_job_send_status(job, (uint32_t)workload_size, (uint32_t)workload_size)))
132 {
133 return GEARMAN_ERROR;
134 }
135 }
136 }
137
138 if (options.verbose)
139 {
140 std::cout << "Job=" << gearman_job_handle(job);
141 }
142
143 if (options.unique and options.verbose)
144 {
145 std::cout << "Unique=" << gearman_job_unique(job);
146 }
147
148
149 if (options.verbose)
150 {
151 std::cout << " Reversed=";
152 std::cout.write(workload, workload_size);
153 std::cout << std::endl;
154 }
155
156 return GEARMAN_SUCCESS;
157 }
158
main(int args,char * argv[])159 int main(int args, char *argv[])
160 {
161 uint64_t limit;
162 reverse_worker_options_t options;
163 int timeout;
164
165 in_port_t port;
166 std::string host;
167 std::string identifier;
168 boost::program_options::options_description desc("Options");
169 desc.add_options()
170 ("help", "Options related to the program.")
171 ("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
172 ("identifier", boost::program_options::value<std::string>(&identifier),"Assign identifier")
173 ("port,p", boost::program_options::value<in_port_t>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT), "Port number use for connection")
174 ("count,c", boost::program_options::value<uint64_t>(&limit)->default_value(0), "Number of jobs to run before exiting")
175 ("timeout,u", boost::program_options::value<int>(&timeout)->default_value(-1), "Timeout in milliseconds")
176 ("chunk,d", boost::program_options::bool_switch(&options.chunk)->default_value(false), "Send result back in data chunks")
177 ("status,s", boost::program_options::bool_switch(&options.status)->default_value(false), "Send status updates and sleep while running job")
178 ("unique,u", boost::program_options::bool_switch(&options.unique)->default_value(false), "When grabbing jobs, grab the uniqie id")
179 ("verbose", boost::program_options::bool_switch(&options.verbose)->default_value(true), "Print to stdout information as job is processed.")
180 ;
181
182 boost::program_options::variables_map vm;
183 try
184 {
185 boost::program_options::store(boost::program_options::parse_command_line(args, argv, desc), vm);
186 boost::program_options::notify(vm);
187 }
188 catch(std::exception &e)
189 {
190 std::cout << e.what() << std::endl;
191 return EXIT_FAILURE;
192 }
193
194 if (vm.count("help"))
195 {
196 std::cout << desc << std::endl;
197 return EXIT_SUCCESS;
198 }
199
200 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
201 {
202 std::cerr << "signal:" << strerror(errno) << std::endl;
203 return EXIT_FAILURE;
204 }
205
206 gearman_worker_st *worker;
207 if ((worker= gearman_worker_create(NULL)) == NULL)
208 {
209 std::cerr << "Memory allocation failure on worker creation." << std::endl;
210 return EXIT_FAILURE;
211 }
212
213 if (options.unique)
214 {
215 gearman_worker_add_options(worker, GEARMAN_WORKER_GRAB_UNIQ);
216 }
217
218 if (timeout >= 0)
219 {
220 gearman_worker_set_timeout(worker, timeout);
221 }
222
223 if (gearman_failed(gearman_worker_add_server(worker, host.c_str(), port)))
224 {
225 std::cerr << gearman_worker_error(worker) << std::endl;
226 return EXIT_FAILURE;
227 }
228
229 if (identifier.empty() == false)
230 {
231 if (gearman_failed(gearman_worker_set_identifier(worker, identifier.c_str(), identifier.size())))
232 {
233 std::cerr << gearman_worker_error(worker) << std::endl;
234 return EXIT_FAILURE;
235 }
236 }
237
238 gearman_function_t worker_fn= gearman_function_create(reverse_worker);
239 if (gearman_failed(gearman_worker_define_function(worker,
240 gearman_literal_param("reverse"),
241 worker_fn,
242 0,
243 &options)))
244 {
245 std::cerr << gearman_worker_error(worker) << std::endl;
246 return EXIT_FAILURE;
247 }
248
249 // Add one if count is not zero
250 if (limit != 0)
251 {
252 limit++;
253 }
254
255 while (--limit)
256 {
257 if (gearman_failed(gearman_worker_work(worker)))
258 {
259 std::cerr << gearman_worker_error(worker) << std::endl;
260 break;
261 }
262 }
263
264 gearman_worker_free(worker);
265
266 return EXIT_SUCCESS;
267 }
268