Browse Source

Convert examples to C++, fix a number of little memory leaks.

Brian Aker 14 years ago
parent
commit
c214933f2f
10 changed files with 495 additions and 341 deletions
  1. 1 0
      .bzrignore
  2. 3 3
      bin/gearadmin.cc
  3. 10 10
      bin/gearman.cc
  4. 0 89
      examples/echo_client.c
  5. 131 0
      examples/echo_client.cc
  6. 0 89
      examples/echo_worker.c
  7. 131 0
      examples/echo_worker.cc
  8. 49 22
      examples/include.am
  9. 0 128
      examples/reverse_client.c
  10. 170 0
      examples/reverse_client.cc

+ 1 - 0
.bzrignore

@@ -91,3 +91,4 @@ tests/var/tmp/*
 tests/worker_test
 tests/worker_test.res
 unittests/unittests
+tests/var/

+ 3 - 3
bin/gearadmin.cc

@@ -120,7 +120,7 @@ int main(int args, char *argv[])
   catch(std::exception &e)
   { 
     std::cout << e.what() << std::endl;
-    return 1;
+    return EXIT_FAILURE;
   }
 
   Instance instance;
@@ -131,7 +131,7 @@ int main(int args, char *argv[])
   if (vm.count("help"))
   {
     std::cout << desc << std::endl;
-    return 1;
+    return EXIT_FAILURE;
   }
 
   if (vm.count("shutdown"))
@@ -160,5 +160,5 @@ int main(int args, char *argv[])
 
   instance.run();
 
-  return 0;
+  return EXIT_SUCCESS;
 }

+ 10 - 10
bin/gearman.cc

@@ -126,7 +126,7 @@ int main(int argc, char *argv[])
   if (args.usage())
   {
     usage(argv[0]);
-    exit(0);
+    return EXIT_SUCCESS;
   }
 
   signal_setup();
@@ -137,19 +137,19 @@ int main(int argc, char *argv[])
     {
     case -1:
       fprintf(stderr, "gearmand: fork:%d\n", errno);
-      return 1;
+      return EXIT_FAILURE;
 
     case 0:
       break;
 
     default:
-      return 0;
+      return EXIT_SUCCESS;
     }
 
     if (setsid() == -1)
     {
       fprintf(stderr, "gearmand: setsid:%d\n", errno);
-      return 1;
+      return EXIT_FAILURE;
     }
 
     close_stdio= true;
@@ -165,19 +165,19 @@ int main(int argc, char *argv[])
       if (dup2(fd, STDIN_FILENO) == -1)
       {
         fprintf(stderr, "gearmand: dup2:%d\n", errno);
-        return 1;
+        return EXIT_FAILURE;
       }
 
       if (dup2(fd, STDOUT_FILENO) == -1)
       {
         fprintf(stderr, "gearmand: dup2:%d\n", errno);
-        return 1;
+        return EXIT_FAILURE;
       }
 
       if (dup2(fd, STDERR_FILENO) == -1)
       {
         fprintf(stderr, "gearmand: dup2:%d\n", errno);
-        return 1;
+        return EXIT_FAILURE;
       }
 
       close(fd);
@@ -189,7 +189,7 @@ int main(int argc, char *argv[])
   if (not _pid_file.create())
   {
     error::perror(_pid_file.error_message().c_str());
-    return 1;
+    return EXIT_FAILURE;
   }
 
   if (args.worker())
@@ -459,7 +459,7 @@ void _worker(Args &args)
   if (ret != GEARMAN_SUCCESS)
   {
     error::message("gearman_worker_add_server", worker);
-    exit(0);
+    _exit(EXIT_FAILURE);
   }
 
   gearman_worker_set_workload_free_fn(&worker, _worker_free, NULL);
@@ -474,7 +474,7 @@ void _worker(Args &args)
     if (ret != GEARMAN_SUCCESS)
     {
       error::message("gearman_worker_add_function", worker);
-      exit(0);
+      _exit(EXIT_FAILURE);
     }
   }
 

+ 0 - 89
examples/echo_client.c

@@ -1,89 +0,0 @@
-/* Gearman server and library
- * Copyright (C) 2008 Brian Aker, Eric Day
- * All rights reserved.
- *
- * Use and distribution licensed under the BSD license.  See
- * the COPYING file in the parent directory for full text.
- */
-
-/**
- * @file
- * @brief Echo Client
- */
-
-#include "config.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#ifdef HAVE_GETOPT_H
-#include <getopt.h>
-#endif
-
-#include <libgearman/gearman.h>
-
-static void usage(char *name);
-
-int main(int argc, char *argv[])
-{
-  int c;
-  char *host= NULL;
-  in_port_t port= 0;
-  gearman_return_t ret;
-  gearman_client_st client;
-
-  while ((c = getopt(argc, argv, "h:p:")) != -1)
-  {
-    switch(c)
-    {
-    case 'h':
-      host= optarg;
-      break;
-
-    case 'p':
-      port= (in_port_t)atoi(optarg);
-      break;
-
-    default:
-      usage(argv[0]);
-      exit(1);
-    }
-  }
-
-  if (argc != (optind + 1))
-  {
-    usage(argv[0]);
-    exit(1);
-  }
-
-  if (gearman_client_create(&client) == NULL)
-  {
-    fprintf(stderr, "Memory allocation failure on client creation\n");
-    exit(1);
-  }
-
-  ret= gearman_client_add_server(&client, host, port);
-  if (ret != GEARMAN_SUCCESS)
-  {
-    fprintf(stderr, "%s\n", gearman_client_error(&client));
-    exit(1);
-  }
-
-  ret= gearman_client_echo(&client, (void *)argv[optind],
-                           (size_t)strlen(argv[optind]));
-  if (ret != GEARMAN_SUCCESS)
-    fprintf(stderr, "%s\n", gearman_client_error(&client));
-
-  gearman_client_free(&client);
-
-  return 0;
-}
-
-static void usage(char *name)
-{
-  printf("\nusage: %s [-h <host>] [-p <port>] <string>\n", name);
-  printf("\t-h <host> - job server host\n");
-  printf("\t-p <port> - job server port\n");
-}

+ 131 - 0
examples/echo_client.cc

@@ -0,0 +1,131 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Gearmand client and server library.
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  Copyright (C) 2008 Brian Aker, Eric Day
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+
+#include "config.h"
+
+#include <iostream>
+#include <cstdlib>
+#include <string>
+
+#include <libgearman/gearman.h>
+#include <boost/program_options.hpp>
+
+#pragma GCC diagnostic ignored "-Wold-style-cast"
+
+int main(int args, char *argv[])
+{
+  std::string text_to_echo;
+  std::string host;
+  in_port_t port;
+
+  boost::program_options::options_description desc("Options");
+  desc.add_options()
+    ("help", "Options related to the program.")
+    ("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
+    ("port,p", boost::program_options::value<in_port_t>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT), "Port number use for connection")
+    ("text", boost::program_options::value<std::string>(&text_to_echo), "Text used for echo")
+            ;
+
+  boost::program_options::positional_options_description text_options;
+  text_options.add("text", -1);
+
+  boost::program_options::variables_map vm;
+  try
+  {
+    boost::program_options::store(boost::program_options::command_line_parser(args, argv).
+                                  options(desc).positional(text_options).run(), vm);
+    boost::program_options::notify(vm);
+  }
+  catch(std::exception &e)
+  { 
+    std::cout << e.what() << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  if (vm.count("help"))
+  {
+    std::cout << desc << std::endl;
+    return EXIT_SUCCESS;
+  }
+
+  if (text_to_echo.empty())
+  {
+    while(std::cin.good())
+    { 
+      char buffer[1024];
+
+      std::cin.read(buffer, sizeof(buffer));
+      text_to_echo.append(buffer, std::cin.gcount());
+    }
+
+    if (text_to_echo.empty())
+    {
+      std::cerr << "No text was provided for --text or via stdin" << std::endl;
+      std::cerr << desc << std::endl;
+      return EXIT_FAILURE;
+    }
+  }
+
+  gearman_client_st client;
+  if (gearman_client_create(&client) == NULL)
+  {
+    std::cerr << "Memory allocation failure on client creation" << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  gearman_return_t ret;
+  ret= gearman_client_add_server(&client, host.c_str(), port);
+  if (ret != GEARMAN_SUCCESS)
+  {
+    std::cerr << gearman_client_error(&client) << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  ret= gearman_client_echo(&client, text_to_echo.c_str(), text_to_echo.size());
+  gearman_client_free(&client);
+  if (ret != GEARMAN_SUCCESS)
+  {
+    std::cerr << gearman_client_error(&client) << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  std::cout << text_to_echo;
+
+  return EXIT_SUCCESS;
+}

+ 0 - 89
examples/echo_worker.c

@@ -1,89 +0,0 @@
-/* Gearman server and library
- * Copyright (C) 2008 Brian Aker, Eric Day
- * All rights reserved.
- *
- * Use and distribution licensed under the BSD license.  See
- * the COPYING file in the parent directory for full text.
- */
-
-/**
- * @file
- * @brief Echo Worker
- */
-
-#include "config.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#ifdef HAVE_GETOPT_H
-#include <getopt.h>
-#endif
-
-#include <libgearman/gearman.h>
-
-static void usage(char *name);
-
-int main(int argc, char *argv[])
-{
-  int c;
-  char *host= NULL;
-  in_port_t port= 0;
-  gearman_return_t ret;
-  gearman_worker_st worker;
-
-  while ((c = getopt(argc, argv, "h:p:")) != -1)
-  {
-    switch(c)
-    {
-    case 'h':
-      host= optarg;
-      break;
-
-    case 'p':
-      port= (in_port_t)atoi(optarg);
-      break;
-
-    default:
-      usage(argv[0]);
-      exit(1);
-    }
-  }
-
-  if (argc != (optind + 1))
-  {
-    usage(argv[0]);
-    exit(1);
-  }
-
-  if (gearman_worker_create(&worker) == NULL)
-  {
-    fprintf(stderr, "Memory allocation failure on worker creation\n");
-    exit(1);
-  }
-
-  ret= gearman_worker_add_server(&worker, host, port);
-  if (ret != GEARMAN_SUCCESS)
-  {
-    fprintf(stderr, "%s\n", gearman_worker_error(&worker));
-    exit(1);
-  }
-
-  ret= gearman_worker_echo(&worker, (void *)argv[optind],
-                           (size_t)strlen(argv[optind]));
-  if (ret != GEARMAN_SUCCESS)
-    fprintf(stderr, "%s\n", gearman_worker_error(&worker));
-
-  gearman_worker_free(&worker);
-
-  return 0;
-}
-
-static void usage(char *name)
-{
-  printf("\nusage: %s [-h <host>] [-p <port>] <string>\n", name);
-  printf("\t-h <host> - job server host\n");
-  printf("\t-p <port> - job server port\n");
-}

+ 131 - 0
examples/echo_worker.cc

@@ -0,0 +1,131 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Gearmand client and server library.
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  Copyright (C) 2008 Brian Aker, Eric Day
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "config.h"
+
+#include <cstdlib>
+#include <iostream>
+#include <string>
+
+#include <libgearman/gearman.h>
+#include <boost/program_options.hpp>
+
+#pragma GCC diagnostic ignored "-Wold-style-cast"
+
+int main(int args, char *argv[])
+{
+  std::string text_to_echo;
+  std::string host;
+  in_port_t port;
+
+  boost::program_options::options_description desc("Options");
+  desc.add_options()
+    ("help", "Options related to the program.")
+    ("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
+    ("port,p", boost::program_options::value<in_port_t>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT), "Port number use for connection")
+    ("text", boost::program_options::value<std::string>(&text_to_echo), "Text used for echo")
+    ;
+
+  boost::program_options::positional_options_description text_options;
+  text_options.add("text", -1);
+
+  boost::program_options::variables_map vm;
+  try
+  {
+    boost::program_options::store(boost::program_options::command_line_parser(args, argv).
+                                  options(desc).positional(text_options).run(), vm);
+
+    boost::program_options::notify(vm);
+  }
+  catch(std::exception &e)
+  { 
+    std::cout << e.what() << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  if (vm.count("help"))
+  {
+    std::cout << desc << std::endl;
+    return EXIT_SUCCESS;
+  }
+
+  if (text_to_echo.empty())
+  {
+    while(std::cin.good())
+    { 
+      char buffer[1024];
+
+      std::cin.read(buffer, sizeof(buffer));
+      text_to_echo.append(buffer, std::cin.gcount());
+    }
+
+    if (text_to_echo.empty())
+    {
+      std::cerr << "No text was provided for --text or via stdin" << std::endl;
+      std::cerr << desc << std::endl;
+      return EXIT_FAILURE;
+    }
+  }
+
+  gearman_worker_st worker;
+  if (gearman_worker_create(&worker) == NULL)
+  {
+    std::cerr << "Memory allocation failure on worker creation" << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  gearman_return_t ret;
+  ret= gearman_worker_add_server(&worker, host.c_str(), port);
+  if (ret != GEARMAN_SUCCESS)
+  {
+    std::cerr << gearman_worker_error(&worker) << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  ret= gearman_worker_echo(&worker, text_to_echo.c_str(), text_to_echo.size());
+  gearman_worker_free(&worker);
+  if (ret != GEARMAN_SUCCESS)
+  {
+    std::cerr << gearman_worker_error(&worker) << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  std::cout << text_to_echo;
+
+  return EXIT_SUCCESS;
+}

+ 49 - 22
examples/include.am

@@ -2,6 +2,7 @@
 # Gearman server and library
 # Copyright (C) 2008 Brian Aker, Eric Day
 # Copyright (C) 2009 Brian Aker, Eric Day, Monty Taylor
+# Copyright (C) 2011 Data Differential, http://datadifferential.com/
 # All rights reserved.
 #
 # Use and distribution licensed under the BSD license.  See
@@ -10,34 +11,60 @@
 # Included from Top Level Makefile.am
 # All paths should be given relative to the root
 
-EXAMPLES_LDADD= $(LTLIBUUID) $(LTLIBEVENT) libgearman/libgearman.la
+EXAMPLES_LDADD= \
+		$(AM_LDADD) \
+		$(BOOST_PROGRAM_OPTIONS_LIBS) \
+		$(LTLIBEVENT) \
+		$(LTLIBUUID) \
+		libgearman/libgearman.la
 
 noinst_PROGRAMS+= \
-	examples/echo_client \
-	examples/echo_worker \
-	examples/reverse_client \
-	examples/reverse_client_bg \
-	examples/reverse_client_cb \
-	examples/reverse_worker \
-	examples/wc_worker
+		  examples/echo_client \
+		  examples/echo_worker \
+		  examples/reverse_client \
+		  examples/reverse_client_bg \
+		  examples/reverse_client_cb \
+		  examples/reverse_worker \
+		  examples/wc_worker
 
-examples_echo_client_SOURCES= examples/echo_client.c
-examples_echo_client_LDADD= $(AM_LDADD) $(EXAMPLES_LDADD)
+examples_echo_client_SOURCES= examples/echo_client.cc
+examples_echo_client_LDADD= $(EXAMPLES_LDADD)
+examples_echo_client_CPPFLAGS= \
+			       $(AM_CPPFLAGS) \
+			       $(BOOST_CPPFLAGS)
 
-examples_echo_worker_SOURCES= examples/echo_worker.c
-examples_echo_worker_LDADD= $(AM_LDADD) $(EXAMPLES_LDADD)
+examples_echo_worker_SOURCES= examples/echo_worker.cc
+examples_echo_worker_LDADD= $(EXAMPLES_LDADD)
+examples_echo_worker_CPPFLAGS= \
+			       $(AM_CPPFLAGS) \
+			       $(BOOST_CPPFLAGS)
 
-examples_reverse_client_SOURCES= examples/reverse_client.c
-examples_reverse_client_LDADD= $(AM_LDADD) $(EXAMPLES_LDADD)
+examples_reverse_client_SOURCES= examples/reverse_client.cc
+examples_reverse_client_LDADD= $(EXAMPLES_LDADD)
+examples_reverse_client_CPPFLAGS= \
+				  $(AM_CPPFLAGS) \
+				  $(BOOST_CPPFLAGS)
 
-examples_reverse_client_bg_SOURCES= examples/reverse_client_bg.c
-examples_reverse_client_bg_LDADD= $(AM_LDADD) $(EXAMPLES_LDADD)
+examples_reverse_client_bg_SOURCES= examples/reverse_client_bg.cc
+examples_reverse_client_bg_LDADD= $(EXAMPLES_LDADD)
+examples_reverse_client_bg_CPPFLAGS= \
+				     $(AM_CPPFLAGS) \
+				     $(BOOST_CPPFLAGS)
 
-examples_reverse_client_cb_SOURCES= examples/reverse_client_cb.c
-examples_reverse_client_cb_LDADD= $(AM_LDADD) $(EXAMPLES_LDADD)
+examples_reverse_client_cb_SOURCES= examples/reverse_client_cb.cc
+examples_reverse_client_cb_LDADD= $(EXAMPLES_LDADD)
+examples_reverse_client_cb_CPPFLAGS= \
+				     $(AM_CPPFLAGS) \
+				     $(BOOST_CPPFLAGS)
 
-examples_reverse_worker_SOURCES= examples/reverse_worker.c
-examples_reverse_worker_LDADD= $(AM_LDADD) $(EXAMPLES_LDADD)
+examples_reverse_worker_SOURCES= examples/reverse_worker.cc
+examples_reverse_worker_LDADD= $(EXAMPLES_LDADD)
+examples_reverse_CPPFLAGS= \
+			   $(AM_CPPFLAGS) \
+			   $(BOOST_CPPFLAGS)
 
-examples_wc_worker_SOURCES= examples/wc_worker.c
-examples_wc_worker_LDADD= $(AM_LDADD) $(EXAMPLES_LDADD)
+examples_wc_worker_SOURCES= examples/wc_worker.cc
+examples_wc_worker_LDADD= $(EXAMPLES_LDADD)
+examples_wc_CPPFLAGS= \
+		      $(AM_CPPFLAGS) \
+		      $(BOOST_CPPFLAGS)

+ 0 - 128
examples/reverse_client.c

@@ -1,128 +0,0 @@
-/* Gearman server and library
- * Copyright (C) 2008 Brian Aker, Eric Day
- * All rights reserved.
- *
- * Use and distribution licensed under the BSD license.  See
- * the COPYING file in the parent directory for full text.
- */
-
-/**
- * @file
- * @brief Example Client
- */
-
-#include "config.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#ifdef HAVE_GETOPT_H
-#include <getopt.h>
-#endif
-
-#include <libgearman/gearman.h>
-
-static void usage(char *name);
-
-int main(int argc, char *argv[])
-{
-  int c;
-  char *host= NULL;
-  in_port_t port= 0;
-  int timeout= -1;
-  gearman_return_t ret;
-  gearman_client_st client;
-  char *result;
-  size_t result_size;
-  uint32_t numerator;
-  uint32_t denominator;
-
-  while ((c = getopt(argc, argv, "h:p:t:")) != -1)
-  {
-    switch(c)
-    {
-    case 'h':
-      host= optarg;
-      break;
-
-    case 'p':
-      port= (in_port_t)atoi(optarg);
-      break;
-
-    case 't':
-      timeout= atoi(optarg);
-      break;
-
-    default:
-      usage(argv[0]);
-      exit(1);
-    }
-  }
-
-  if (argc != (optind + 1))
-  {
-    usage(argv[0]);
-    exit(1);
-  }
-
-  if (gearman_client_create(&client) == NULL)
-  {
-    fprintf(stderr, "Memory allocation failure on client creation\n");
-    exit(1);
-  }
-
-  if (timeout >= 0)
-    gearman_client_set_timeout(&client, timeout);
-
-  ret= gearman_client_add_server(&client, host, port);
-  if (ret != GEARMAN_SUCCESS)
-  {
-    fprintf(stderr, "%s\n", gearman_client_error(&client));
-    exit(1);
-  }
-
-  while (1)
-  {
-    result= (char *)gearman_client_do(&client, "reverse", NULL,
-                                      (void *)argv[optind],
-                                      (size_t)strlen(argv[optind]),
-                                      &result_size, &ret);
-    if (ret == GEARMAN_WORK_DATA)
-    {
-      printf("Data=%.*s\n", (int)result_size, result);
-      free(result);
-      continue;
-    }
-    else if (ret == GEARMAN_WORK_STATUS)
-    {
-      gearman_client_do_status(&client, &numerator, &denominator);
-      printf("Status: %u/%u\n", numerator, denominator);
-      continue;
-    }
-    else if (ret == GEARMAN_SUCCESS)
-    {
-      printf("Result=%.*s\n", (int)result_size, result);
-      free(result);
-    }
-    else if (ret == GEARMAN_WORK_FAIL)
-      fprintf(stderr, "Work failed\n");
-    else
-      fprintf(stderr, "%s\n", gearman_client_error(&client));
-
-    break;
-  }
-
-  gearman_client_free(&client);
-
-  return 0;
-}
-
-static void usage(char *name)
-{
-  printf("\nusage: %s [-h <host>] [-p <port>] <string>\n", name);
-  printf("\t-h <host>    - job server host\n");
-  printf("\t-p <port>    - job server port\n");
-  printf("\t-t <timeout> - timeout in milliseconds\n");
-}

+ 170 - 0
examples/reverse_client.cc

@@ -0,0 +1,170 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  Gearmand client and server library.
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  Copyright (C) 2008 Brian Aker, Eric Day
+ *  All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions are
+ *  met:
+ *
+ *      * Redistributions of source code must retain the above copyright
+ *  notice, this list of conditions and the following disclaimer.
+ *
+ *      * Redistributions in binary form must reproduce the above
+ *  copyright notice, this list of conditions and the following disclaimer
+ *  in the documentation and/or other materials provided with the
+ *  distribution.
+ *
+ *      * The names of its contributors may not be used to endorse or
+ *  promote products derived from this software without specific prior
+ *  written permission.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "config.h"
+
+#include <cstdlib>
+#include <cstring>
+#include <iostream>
+#include <string>
+
+#include <libgearman/gearman.h>
+#include <boost/program_options.hpp>
+
+#pragma GCC diagnostic ignored "-Wold-style-cast"
+int main(int args, char *argv[])
+{
+  in_port_t port;
+  std::string text_to_echo;
+  std::string host;
+  int timeout;
+
+  boost::program_options::options_description desc("Options");
+  desc.add_options()
+    ("help", "Options related to the program.")
+    ("host,h", boost::program_options::value<std::string>(&host)->default_value("localhost"),"Connect to the host")
+    ("port,p", boost::program_options::value<in_port_t>(&port)->default_value(GEARMAN_DEFAULT_TCP_PORT), "Port number use for connection")
+    ("timeout,u", boost::program_options::value<int>(&timeout)->default_value(-1), "Timeout in milliseconds")
+    ("text", boost::program_options::value<std::string>(&text_to_echo), "Text used for echo")
+            ;
+
+  boost::program_options::positional_options_description text_options;
+  text_options.add("text", -1);
+
+  boost::program_options::variables_map vm;
+  try
+  {
+    boost::program_options::store(boost::program_options::command_line_parser(args, argv).
+                                  options(desc).positional(text_options).run(), vm);
+    boost::program_options::notify(vm);
+  }
+  catch(std::exception &e)
+  { 
+    std::cout << e.what() << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  if (vm.count("help"))
+  {
+    std::cout << desc << std::endl;
+    return EXIT_SUCCESS;
+  }
+
+  if (text_to_echo.empty())
+  {
+    while(std::cin.good())
+    { 
+      char buffer[1024];
+
+      std::cin.read(buffer, sizeof(buffer));
+      text_to_echo.append(buffer, std::cin.gcount());
+    }
+
+    if (text_to_echo.empty())
+    {
+      std::cerr << "No text was provided for --text or via stdin" << std::endl;
+      std::cerr << desc << std::endl;
+      return EXIT_FAILURE;
+    }
+  }
+
+  gearman_client_st client;
+  if (gearman_client_create(&client) == NULL)
+  {
+    std::cerr << "Memory allocation failure on client creation" << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  if (timeout >= 0)
+    gearman_client_set_timeout(&client, timeout);
+
+  gearman_return_t ret;
+  ret= gearman_client_add_server(&client, host.c_str(), port);
+  if (ret != GEARMAN_SUCCESS)
+  {
+    std::cerr << gearman_client_error(&client) << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  int exit_code= EXIT_SUCCESS;
+  while (1)
+  {
+    size_t result_size;
+    char *result;
+    result= (char *)gearman_client_do(&client, "reverse", NULL,
+                                      text_to_echo.c_str(), text_to_echo.size(),
+                                      &result_size, &ret);
+    if (ret == GEARMAN_WORK_DATA)
+    {
+      std::cout.write(result, result_size);
+
+      free(result);
+      continue;
+    }
+    else if (ret == GEARMAN_WORK_STATUS)
+    {
+      uint32_t numerator;
+      uint32_t denominator;
+
+      gearman_client_do_status(&client, &numerator, &denominator);
+      std::clog << "Status: " << numerator << "/" << denominator << std::endl;
+      continue;
+    }
+    else if (ret == GEARMAN_SUCCESS)
+    {
+      std::cout.write(result, result_size);
+      free(result);
+    }
+    else if (ret == GEARMAN_WORK_FAIL)
+    {
+      std::cerr << "Work failed" << std::endl;
+      exit_code= EXIT_FAILURE;
+    }
+    else
+    {
+      std::cerr << gearman_client_error(&client) << std::endl;
+      exit_code= EXIT_FAILURE;
+    }
+
+    break;
+  }
+
+  gearman_client_free(&client);
+
+  return EXIT_SUCCESS;
+}

Some files were not shown because too many files changed in this diff