~brianaker/gearmand/osx9-fixes

« back to all changes in this revision

Viewing changes to libgearman/universal.cc

  • Committer: Brian Aker
  • Date: 2013-02-04 06:00:39 UTC
  • mfrom: (621.46.3 workspace)
  • mto: This revision was merged to the branch mainline in revision 701.
  • Revision ID: brian@tangent.org-20130204060039-a1amgkajohn0g032
Merge of 1.0, date set for new version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
 
48
48
#include "libgearman/assert.hpp"
49
49
#include "libgearman/interface/push.hpp"
 
50
#include "libgearman/server_options.hpp"
50
51
 
51
52
#include <cerrno>
52
53
#include <cstdarg>
55
56
#include <cstring>
56
57
#include <cctype>
57
58
#include <unistd.h>
 
59
#include <memory>
58
60
 
59
61
void gearman_nap(int arg)
60
62
{
120
122
    free(universal.pfds);
121
123
    universal.pfds= NULL;
122
124
  }
 
125
 
 
126
  // clean-up server options
 
127
  while (universal.server_options_list)
 
128
  {
 
129
    delete universal.server_options_list;
 
130
  }
123
131
}
124
132
 
125
133
gearman_return_t gearman_universal_set_option(gearman_universal_st &self, gearman_universal_options_t option, bool value)
412
420
  return ret;
413
421
}
414
422
 
415
 
class Check {
416
 
public:
417
 
  virtual gearman_return_t success(gearman_connection_st*)= 0;
418
 
 
419
 
  virtual ~Check() {};
420
 
};
421
 
 
422
 
class EchoCheck : public Check {
423
 
public:
424
 
  EchoCheck(gearman_universal_st& universal_,
425
 
            const void *workload_, const size_t workload_size_) :
 
423
EchoCheck::EchoCheck(gearman_universal_st& universal_,
 
424
    const void *workload_, const size_t workload_size_) :
426
425
    _universal(universal_),
427
426
    _workload(workload_),
428
427
    _workload_size(workload_size_)
429
 
  {
430
 
  }
431
 
 
432
 
  gearman_return_t success(gearman_connection_st* con)
433
 
  {
434
 
    if (con->_packet.command != GEARMAN_COMMAND_ECHO_RES)
435
 
    {
436
 
      return gearman_error(_universal, GEARMAN_INVALID_COMMAND, "Wrong command sent in response to ECHO request");
437
 
    }
438
 
 
439
 
    if (con->_packet.data_size != _workload_size or
440
 
        memcmp(_workload, con->_packet.data, _workload_size))
441
 
    {
442
 
      return gearman_error(_universal, GEARMAN_ECHO_DATA_CORRUPTION, "corruption during echo");
443
 
    }
444
 
 
445
 
    return GEARMAN_SUCCESS;
446
 
  }
447
 
 
448
 
private:
449
 
  gearman_universal_st& _universal;
450
 
  const void *_workload;
451
 
  const size_t _workload_size;
452
 
};
453
 
 
454
 
class OptionCheck : public Check {
455
 
public:
456
 
  OptionCheck(gearman_universal_st& universal_):
 
428
{
 
429
}
 
430
 
 
431
gearman_return_t EchoCheck::success(gearman_connection_st* con)
 
432
{
 
433
  if (con->_packet.command != GEARMAN_COMMAND_ECHO_RES)
 
434
  {
 
435
    return gearman_error(_universal, GEARMAN_INVALID_COMMAND, "Wrong command sent in response to ECHO request");
 
436
  }
 
437
 
 
438
  if (con->_packet.data_size != _workload_size or
 
439
      memcmp(_workload, con->_packet.data, _workload_size))
 
440
  {
 
441
    return gearman_error(_universal, GEARMAN_ECHO_DATA_CORRUPTION, "corruption during echo");
 
442
  }
 
443
 
 
444
  return GEARMAN_SUCCESS;
 
445
}
 
446
 
 
447
OptionCheck::OptionCheck(gearman_universal_st& universal_):
457
448
    _universal(universal_)
458
 
  { }
 
449
{
 
450
}
459
451
 
460
 
  gearman_return_t success(gearman_connection_st* con)
 
452
gearman_return_t OptionCheck::success(gearman_connection_st* con)
 
453
{
 
454
  if (con->_packet.command == GEARMAN_COMMAND_ERROR)
461
455
  {
462
 
    if (con->_packet.command == GEARMAN_COMMAND_ERROR)
463
 
    {
464
 
      return gearman_error(_universal, GEARMAN_INVALID_ARGUMENT, "invalid server option");
465
 
    }
466
 
 
467
 
    return GEARMAN_SUCCESS;
 
456
    return gearman_error(_universal, GEARMAN_INVALID_SERVER_OPTION, "invalid server option");
468
457
  }
469
458
 
470
 
private:
471
 
  gearman_universal_st& _universal;
472
 
};
 
459
  return GEARMAN_SUCCESS;
 
460
}
473
461
 
474
462
static gearman_return_t connection_loop(gearman_universal_st& universal,
475
463
                                        const gearman_packet_st& message,
574
562
bool gearman_request_option(gearman_universal_st &universal,
575
563
                            gearman_string_t &option)
576
564
{
577
 
  const void *args[]= { gearman_c_str(option) };
578
 
  size_t args_size[]= { gearman_size(option) };
579
 
 
580
 
  gearman_packet_st message;
581
 
  gearman_return_t ret= gearman_packet_create_args(universal, message, GEARMAN_MAGIC_REQUEST,
582
 
                                                   GEARMAN_COMMAND_OPTION_REQ,
583
 
                                                   args, args_size, 1);
584
 
 
585
 
  if (gearman_success(ret))
586
 
  {
587
 
    PUSH_BLOCKING(universal);
588
 
 
589
 
    OptionCheck check(universal);
590
 
    ret= connection_loop(universal, message, check);
591
 
  }
592
 
  else
593
 
  {
594
 
    gearman_packet_free(&message);
595
 
    gearman_error(universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "gearman_packet_create_args()");
596
 
    return ret;
597
 
  }
598
 
 
599
 
  gearman_packet_free(&message);
600
 
 
601
 
  return gearman_success(ret);
 
565
  char* option_str_cpy = (char*) malloc(gearman_size(option));
 
566
 
 
567
  if (option_str_cpy == NULL)
 
568
  {
 
569
    gearman_error(universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "malloc()");
 
570
    return false;
 
571
  }
 
572
 
 
573
  strncpy(option_str_cpy, gearman_c_str(option), gearman_size(option));
 
574
 
 
575
  gearman_server_options_st *server_options = new (std::nothrow) gearman_server_options_st(universal, option_str_cpy, gearman_size(option));
 
576
  if (server_options == NULL)
 
577
  {
 
578
    free(option_str_cpy);
 
579
    gearman_error(universal, GEARMAN_MEMORY_ALLOCATION_FAILURE, "new gearman_server_options_st()");
 
580
    return false;
 
581
  }
 
582
 
 
583
  return true;
602
584
}
603
585
 
604
586
void gearman_free_all_packets(gearman_universal_st &universal)