Esempio n. 1
0
static void *client_thread(void *object)
{
  (void)object;
  gearman_return_t rc;
  gearman_client_st client;
  gearman_client_st *client_ptr;
  size_t result_size;

  client_ptr= gearman_client_create(&client);

  if (client_ptr == NULL)
    abort(); // This would be pretty bad.

  rc= gearman_client_add_server(&client, NULL, CLIENT_TEST_PORT);
  if (rc != GEARMAN_SUCCESS)
    pthread_exit(0);

  gearman_client_set_timeout(&client, 400);
  for (size_t x= 0; x < 5; x++)
  {
    (void) gearman_client_do(&client, "client_test_temp", NULL, NULL, 0,
                             &result_size, &rc);

  }
  gearman_client_free(client_ptr);

  pthread_exit(0);
}
//http://gearman.info/libgearman/gearman_client_do_background.html
//http://gearman.info/libgearman/gearman_client_options.html
int main(void)
{
  gearman_client_st *client= gearman_client_create(NULL);

  gearman_return_t ret= gearman_client_add_server(client, "localhost", 0);
  if (gearman_failed(ret))
  {
    return EXIT_FAILURE;
  }

  gearman_job_handle_t job_handle;
  gearman_return_t rc= gearman_client_do_background(client,
                                                    "echo",
                                                    NULL,// unique Optional unique job identifier, or NULL for a new UUID.如果写成唯一值只会提交一个任务(即使重复提交)
                                                    "hello-world",
                                                    strlen("hello-world"),
                                                    job_handle);

  if (gearman_success(rc))
  {
    // Make use of value
    printf("return %s\n", job_handle);
  }

  gearman_client_free(client);

  return 0;
}
int main(void)
{
  gearman_client_st *client= gearman_client_create(NULL);

  gearman_return_t ret= gearman_client_add_server(client, "localhost", 0);
  if (gearman_failed(ret))
  {
    return EXIT_FAILURE;
  }

  size_t result_size;
  gearman_return_t rc;
  void *value= gearman_client_do(client, "reverse_function", "unique_value", 
                                 "my string to reverse", strlen("my string to reverse"), 
                                 &result_size, &rc);

  if (gearman_success(rc))
  {
    // Make use of value
  }
  free(value);

  gearman_client_free(client);

  return 0;
}
Esempio n. 4
0
/* create the gearman duplicate client */
int create_client_dup( gm_server_t * server_list[GM_LISTSIZE], gearman_client_st *client ) {
    gearman_return_t ret;
    int x = 0;

    gm_log( GM_LOG_TRACE, "create_client_dup()\n" );

    signal(SIGPIPE, SIG_IGN);

    client = gearman_client_create(client);
    if ( client == NULL ) {
        gm_log( GM_LOG_ERROR, "Memory allocation failure on client creation\n" );
        return GM_ERROR;
    }

    while ( server_list[x] != NULL ) {
        ret = gearman_client_add_server( client, server_list[x]->host, server_list[x]->port );
        if ( ret != GEARMAN_SUCCESS ) {
            gm_log( GM_LOG_ERROR, "client error: %s\n", gearman_client_error( client ) );
            return GM_ERROR;
        }
        x++;
    }

    current_client_dup = client;

    return GM_OK;
}
Esempio n. 5
0
int main (int argc, char **argv)
{
  (void)argc;
  (void)argv;
  gearman_client_st client;

  gearman_client_create(&client);
  gearman_client_free(&client);

  exit( 0 );
}
Esempio n. 6
0
SEXP createclient(void){
   rgearman_client_t *client;

   client = (rgearman_client_t *)R_Calloc(1,rgearman_client_t);

   if (client)
      client->client = gearman_client_create(NULL);

   return (client) ?
      R_MakeExternalPtr(client,R_NilValue,R_NilValue) :
      R_NilValue;
}
Esempio n. 7
0
int main(void)
{
  gearman_client_st *client= gearman_client_create(NULL);

  if (client == NULL)
  {
    return EXIT_FAILURE;
  }

  gearman_client_free(client);

  return EXIT_SUCCESS;
}
Esempio n. 8
0
/* create the gearman client */
int create_client( char ** server_list, gearman_client_st *client ) {
    gearman_return_t ret;
    int x = 0;

    gm_log( GM_LOG_TRACE, "create_client()\n" );

    signal(SIGPIPE, SIG_IGN);

    client = gearman_client_create(client);
    if ( client == NULL ) {
        gm_log( GM_LOG_ERROR, "Memory allocation failure on client creation\n" );
        return GM_ERROR;
    }

    while ( server_list[x] != NULL ) {
        char * server   = strdup( server_list[x] );
        char * server_c = server;
        char * host     = strsep( &server, ":" );
        char * port_val = strsep( &server, "\x0" );
        in_port_t port  = GM_SERVER_DEFAULT_PORT;
        if(port_val != NULL) {
            port  = ( in_port_t ) atoi( port_val );
        }
        ret = gearman_client_add_server( client, host, port );
        if ( ret != GEARMAN_SUCCESS ) {
            gm_log( GM_LOG_ERROR, "client error: %s\n", gearman_client_error( client ) );
            free(server_c);
            return GM_ERROR;
        }
        free(server_c);
        x++;
    }
    assert(x != 0);


    current_client = client;

    return GM_OK;
}
Esempio n. 9
0
int main(int argc, char *argv[])
{
  int c;
  char *host= NULL;
  in_port_t port= 0;
  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:")) != -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);
  }

  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;
}
Esempio n. 10
0
int main()
{
  gearman_client_st client;
  if (gearman_client_create(&client) == NULL)
  {
    std::cerr << "Memory allocation failure on client creation" << std::endl;
    return EXIT_FAILURE;
  }
  //
  int timeout=-1;
  //
  if (timeout >= 0)
  {
    gearman_client_set_timeout(&client, timeout);
  }

  gearman_return_t ret= gearman_client_add_server(&client, "localhost", GEARMAN_DEFAULT_TCP_PORT);
  if (ret != GEARMAN_SUCCESS)
  {
    std::cerr<<(gearman_client_error(&client));
    return EXIT_FAILURE;
  }
  if (gearman_failed(gearman_client_set_identifier(&client, "client1", strlen("client1"))))
  {
    std::cerr<<(gearman_client_error(&client));
    return EXIT_FAILURE;
  }
  int exit_code= EXIT_SUCCESS;
  int count=1; //if status is to be returned then don't change count on status receive
  do
  {
    size_t result_size;
    char *result;
    result= (char *)gearman_client_do(&client, "make_call", NULL,
                                      "scheme-string", strlen("scheme-string"),
                                      &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");
      exit_code= EXIT_FAILURE;
      break;
    }
    else
    {
      std::cerr<<(gearman_client_error(&client));
      exit_code= EXIT_FAILURE;
      break;
    }

    --count;

  } while (count);

  gearman_client_free(&client);

  return exit_code;
}