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

« back to all changes in this revision

Viewing changes to bin/gearadmin.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
 
 *  All rights reserved.
7
 
 *
8
 
 *  Redistribution and use in source and binary forms, with or without
9
 
 *  modification, are permitted provided that the following conditions are
10
 
 *  met:
11
 
 *
12
 
 *      * Redistributions of source code must retain the above copyright
13
 
 *  notice, this list of conditions and the following disclaimer.
14
 
 *
15
 
 *      * Redistributions in binary form must reproduce the above
16
 
 *  copyright notice, this list of conditions and the following disclaimer
17
 
 *  in the documentation and/or other materials provided with the
18
 
 *  distribution.
19
 
 *
20
 
 *      * The names of its contributors may not be used to endorse or
21
 
 *  promote products derived from this software without specific prior
22
 
 *  written permission.
23
 
 *
24
 
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
 
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
 
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
 
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28
 
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
 
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
 
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
 
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
 
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
 
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
 
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
 
 *
36
 
 */
37
 
 
38
 
#include <config.h>
39
 
 
40
 
#include <cstdio>
41
 
#include <cstdlib>
42
 
#include <cstring>
43
 
#include <iostream>
44
 
#include <vector>
45
 
 
46
 
#include <netdb.h>
47
 
#include <sys/socket.h>
48
 
 
49
 
#ifdef HAVE_ERRNO_H
50
 
#include <errno.h>
51
 
#endif
52
 
#ifdef HAVE_FCNTL_H
53
 
#include <fcntl.h>
54
 
#endif
55
 
#ifdef HAVE_PWD_H
56
 
#include <pwd.h>
57
 
#endif
58
 
#ifdef HAVE_SIGNAL_H
59
 
#include <signal.h>
60
 
#endif
61
 
#ifdef HAVE_STDIO_H
62
 
#include <stdio.h>
63
 
#endif
64
 
#ifdef HAVE_STDLIB_H
65
 
#include <stdlib.h>
66
 
#endif
67
 
#ifdef HAVE_STRING_H
68
 
#include <string.h>
69
 
#endif
70
 
#ifdef HAVE_SYS_RESOURCE_H
71
 
#include <sys/resource.h>
72
 
#endif
73
 
#ifdef HAVE_SYS_STAT_H
74
 
#include <sys/stat.h>
75
 
#endif
76
 
#ifdef HAVE_SYS_TYPES_H
77
 
#include <sys/types.h>
78
 
#endif
79
 
#ifdef HAVE_UNISTD_H
80
 
#include <unistd.h>
81
 
#endif
82
 
 
83
 
#include <libgearman-1.0/protocol.h>
84
 
#include <boost/program_options.hpp>
85
 
 
86
 
#include <util/instance.hpp>
87
 
#include <util/string.hpp>
88
 
 
89
 
using namespace datadifferential;
90
 
 
91
 
/*
92
 
  This is a very quickly build application, I am just tired of telneting to the port.
93
 
*/
94
 
 
95
 
class Finish : public util::Instance::Finish
96
 
{
97
 
public:
98
 
  bool call(bool success, const std::string &response)
99
 
  {
100
 
    if (success)
101
 
    {
102
 
      if (response.empty())
103
 
      {
104
 
        std::cout << "OK" << std::endl;
105
 
      }
106
 
      else
107
 
      {
108
 
        std::cout << response;
109
 
      }
110
 
    }
111
 
    else if (not response.empty())
112
 
    {
113
 
      std::cerr << "Error: " << response;
114
 
    }
115
 
    else
116
 
    {
117
 
#if 0
118
 
      std::cerr << "Error" << std::endl;
119
 
#endif
120
 
    }
121
 
 
122
 
    return true;
123
 
  }
124
 
};
125
 
 
126
 
 
127
 
int main(int args, char *argv[])
128
 
{
129
 
  boost::program_options::options_description desc("Options");
130
 
  std::string host;
131
 
  std::string port;
132
 
 
133
 
  desc.add_options()
134
 
    ("help", "Options related to the program.")
135
 
    ("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
136
 
    ("port,p", boost::program_options::value<std::string>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT_STRING), "Port number or service to use for connection")
137
 
    ("server-version", "Fetch the version number for the server.")
138
 
    ("server-verbose", "Fetch the verbose setting for the server.")
139
 
    ("create-function",  boost::program_options::value<std::string>(), "Create the function from the server.") 
140
 
    ("drop-function",  boost::program_options::value<std::string>(), "Drop the function from the server.")
141
 
    ("getpid", "Get Process ID for the server.")
142
 
    ("status", "Status for the server.")
143
 
    ("workers", "Workers for the server.")
144
 
    ("shutdown", "Shutdown server.")
145
 
            ;
146
 
 
147
 
  boost::program_options::variables_map vm;
148
 
  try
149
 
  {
150
 
    boost::program_options::store(boost::program_options::parse_command_line(args, argv, desc), vm);
151
 
    boost::program_options::notify(vm);
152
 
  }
153
 
  catch(std::exception &e)
154
 
  { 
155
 
    std::cout <<  argv[0] << " : " << e.what() << std::endl;
156
 
    std::cout <<  std::endl << desc << std::endl;
157
 
    return EXIT_FAILURE;
158
 
  }
159
 
 
160
 
  util::Instance instance(host, port);
161
 
  instance.set_finish(new Finish);
162
 
 
163
 
  if (vm.count("help"))
164
 
  {
165
 
    std::cout << desc << std::endl;
166
 
    return EXIT_SUCCESS;
167
 
  }
168
 
 
169
 
  if (vm.count("shutdown"))
170
 
  {
171
 
    instance.push(new util::Operation(util_literal_param("shutdown\r\n")));
172
 
  }
173
 
 
174
 
  if (vm.count("status"))
175
 
  {
176
 
    instance.push(new util::Operation(util_literal_param("status\r\n")));
177
 
  }
178
 
 
179
 
  if (vm.count("workers"))
180
 
  {
181
 
    instance.push(new util::Operation(util_literal_param("workers\r\n")));
182
 
  }
183
 
 
184
 
  if (vm.count("server-version"))
185
 
  {
186
 
    instance.push(new util::Operation(util_literal_param("version\r\n")));
187
 
  }
188
 
 
189
 
  if (vm.count("server-verbose"))
190
 
  {
191
 
    instance.push(new util::Operation(util_literal_param("verbose\r\n")));
192
 
  }
193
 
 
194
 
  if (vm.count("drop-function"))
195
 
  {
196
 
    std::string execute(util_literal_param("drop function "));
197
 
    execute.append(vm["drop-function"].as<std::string>());
198
 
    execute.append("\r\n");
199
 
    instance.push(new util::Operation(execute.c_str(), execute.size()));
200
 
  }
201
 
 
202
 
  if (vm.count("create-function"))
203
 
  {
204
 
    std::string execute(util_literal_param("create function "));
205
 
    execute.append(vm["create-function"].as<std::string>());
206
 
    execute.append("\r\n");
207
 
    instance.push(new util::Operation(execute.c_str(), execute.size()));
208
 
  }
209
 
 
210
 
  if (vm.count("getpid"))
211
 
  {
212
 
    instance.push(new util::Operation(util_literal_param("getpid\r\n")));
213
 
  }
214
 
 
215
 
  instance.run();
216
 
 
217
 
  return EXIT_SUCCESS;
218
 
}