1
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
* Gearmand client and server library.
5
* Copyright (C) 2011 Data Differential, http://datadifferential.com/
6
* Copyright (C) 2008 Brian Aker, Eric Day
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions are
13
* * Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
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
21
* * The names of its contributors may not be used to endorse or
22
* promote products derived from this software without specific prior
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.
46
#include <libgearman/gearman.h>
47
#include <boost/program_options.hpp>
49
#ifndef __INTEL_COMPILER
50
#pragma GCC diagnostic ignored "-Wold-style-cast"
53
int main(int args, char *argv[])
56
std::string text_to_echo;
60
boost::program_options::options_description desc("Options");
62
("help", "Options related to the program.")
63
("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
64
("port,p", boost::program_options::value<in_port_t>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT), "Port number use for connection")
65
("timeout,u", boost::program_options::value<int>(&timeout)->default_value(-1), "Timeout in milliseconds")
66
("text", boost::program_options::value<std::string>(&text_to_echo), "Text used for echo")
69
boost::program_options::positional_options_description text_options;
70
text_options.add("text", -1);
72
boost::program_options::variables_map vm;
75
boost::program_options::store(boost::program_options::command_line_parser(args, argv).
76
options(desc).positional(text_options).run(), vm);
77
boost::program_options::notify(vm);
79
catch(std::exception &e)
81
std::cout << e.what() << std::endl;
87
std::cout << desc << std::endl;
91
if (text_to_echo.empty())
93
while(std::cin.good())
97
std::cin.read(buffer, sizeof(buffer));
98
text_to_echo.append(buffer, std::cin.gcount());
101
if (text_to_echo.empty())
103
std::cerr << "No text was provided for --text or via stdin" << std::endl;
104
std::cerr << desc << std::endl;
109
gearman_client_st client;
110
if (gearman_client_create(&client) == NULL)
112
std::cerr << "Memory allocation failure on client creation" << std::endl;
117
gearman_client_set_timeout(&client, timeout);
119
gearman_return_t ret;
120
ret= gearman_client_add_server(&client, host.c_str(), port);
121
if (ret != GEARMAN_SUCCESS)
123
std::cerr << gearman_client_error(&client) << std::endl;
127
int exit_code= EXIT_SUCCESS;
132
result= (char *)gearman_client_do(&client, "reverse", NULL,
133
text_to_echo.c_str(), text_to_echo.size(),
135
if (ret == GEARMAN_WORK_DATA)
137
std::cout.write(result, result_size);
142
else if (ret == GEARMAN_WORK_STATUS)
145
uint32_t denominator;
147
gearman_client_do_status(&client, &numerator, &denominator);
148
std::clog << "Status: " << numerator << "/" << denominator << std::endl;
151
else if (ret == GEARMAN_SUCCESS)
153
std::cout.write(result, result_size);
156
else if (ret == GEARMAN_WORK_FAIL)
158
std::cerr << "Work failed" << std::endl;
159
exit_code= EXIT_FAILURE;
163
std::cerr << gearman_client_error(&client) << std::endl;
164
exit_code= EXIT_FAILURE;
170
gearman_client_free(&client);