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

« back to all changes in this revision

Viewing changes to examples/reverse_client.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2009-08-11 10:06:22 UTC
  • mto: (1.2.3 upstream) (6.1.1 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20090811100622-6ig4iknanc73olum
ImportĀ upstreamĀ versionĀ 0.9

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
 
#include <config.h>
40
 
 
41
 
#include <cstdlib>
42
 
#include <cstring>
43
 
#include <iostream>
44
 
#include <string>
45
 
 
46
 
#include <libgearman/gearman.h>
47
 
#include <boost/program_options.hpp>
48
 
 
49
 
#ifndef __INTEL_COMPILER
50
 
#pragma GCC diagnostic ignored "-Wold-style-cast"
51
 
#endif
52
 
 
53
 
int main(int args, char *argv[])
54
 
{
55
 
  in_port_t port;
56
 
  std::string text_to_echo;
57
 
  std::string host;
58
 
  int timeout;
59
 
 
60
 
  boost::program_options::options_description desc("Options");
61
 
  desc.add_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")
67
 
            ;
68
 
 
69
 
  boost::program_options::positional_options_description text_options;
70
 
  text_options.add("text", -1);
71
 
 
72
 
  boost::program_options::variables_map vm;
73
 
  try
74
 
  {
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);
78
 
  }
79
 
  catch(std::exception &e)
80
 
  { 
81
 
    std::cout << e.what() << std::endl;
82
 
    return EXIT_FAILURE;
83
 
  }
84
 
 
85
 
  if (vm.count("help"))
86
 
  {
87
 
    std::cout << desc << std::endl;
88
 
    return EXIT_SUCCESS;
89
 
  }
90
 
 
91
 
  if (text_to_echo.empty())
92
 
  {
93
 
    while(std::cin.good())
94
 
    { 
95
 
      char buffer[1024];
96
 
 
97
 
      std::cin.read(buffer, sizeof(buffer));
98
 
      text_to_echo.append(buffer, std::cin.gcount());
99
 
    }
100
 
 
101
 
    if (text_to_echo.empty())
102
 
    {
103
 
      std::cerr << "No text was provided for --text or via stdin" << std::endl;
104
 
      std::cerr << desc << std::endl;
105
 
      return EXIT_FAILURE;
106
 
    }
107
 
  }
108
 
 
109
 
  gearman_client_st client;
110
 
  if (gearman_client_create(&client) == NULL)
111
 
  {
112
 
    std::cerr << "Memory allocation failure on client creation" << std::endl;
113
 
    return EXIT_FAILURE;
114
 
  }
115
 
 
116
 
  if (timeout >= 0)
117
 
    gearman_client_set_timeout(&client, timeout);
118
 
 
119
 
  gearman_return_t ret;
120
 
  ret= gearman_client_add_server(&client, host.c_str(), port);
121
 
  if (ret != GEARMAN_SUCCESS)
122
 
  {
123
 
    std::cerr << gearman_client_error(&client) << std::endl;
124
 
    return EXIT_FAILURE;
125
 
  }
126
 
 
127
 
  int exit_code= EXIT_SUCCESS;
128
 
  while (1)
129
 
  {
130
 
    size_t result_size;
131
 
    char *result;
132
 
    result= (char *)gearman_client_do(&client, "reverse", NULL,
133
 
                                      text_to_echo.c_str(), text_to_echo.size(),
134
 
                                      &result_size, &ret);
135
 
    if (ret == GEARMAN_WORK_DATA)
136
 
    {
137
 
      std::cout.write(result, result_size);
138
 
 
139
 
      free(result);
140
 
      continue;
141
 
    }
142
 
    else if (ret == GEARMAN_WORK_STATUS)
143
 
    {
144
 
      uint32_t numerator;
145
 
      uint32_t denominator;
146
 
 
147
 
      gearman_client_do_status(&client, &numerator, &denominator);
148
 
      std::clog << "Status: " << numerator << "/" << denominator << std::endl;
149
 
      continue;
150
 
    }
151
 
    else if (ret == GEARMAN_SUCCESS)
152
 
    {
153
 
      std::cout.write(result, result_size);
154
 
      free(result);
155
 
    }
156
 
    else if (ret == GEARMAN_WORK_FAIL)
157
 
    {
158
 
      std::cerr << "Work failed" << std::endl;
159
 
      exit_code= EXIT_FAILURE;
160
 
    }
161
 
    else
162
 
    {
163
 
      std::cerr << gearman_client_error(&client) << std::endl;
164
 
      exit_code= EXIT_FAILURE;
165
 
    }
166
 
 
167
 
    break;
168
 
  }
169
 
 
170
 
  gearman_client_free(&client);
171
 
 
172
 
  return exit_code;
173
 
}