Exemplo n.º 1
0
bstring get_oauth_header(char *url, char *method)
{
    bstring args = get_oauth_args(url, method);
    bKeyValues *keyvalues;
    keyvalues = get_key_values(args);
    bstring oauth_header = build_oauth_header(keyvalues);

    bdestroy(args);
    destroy_keyvalues(keyvalues);

    return oauth_header;
}
Exemplo n.º 2
0
enum conflate_mgmt_cb_result on_conflate_ping_test(void *userdata,
                                                   conflate_handle_t *handle,
                                                   const char *cmd,
                                                   bool direct,
                                                   kvpair_t *form,
                                                   conflate_form_result *r)
{
    (void)userdata;
    (void)handle;
    (void)cmd;
    (void)direct;
    assert(userdata);

    // The form key-multivalues looks roughly like...
    //
    //  servers
    //    svrname1
    //    svrname2
    //  svr-svrname1
    //    host=mc1.foo.net
    //    port=11211
    //    bucket=buck1
    //    usr=test1
    //    pwd=password
    //  svr-svrname2
    //    host=mc2.foo.net
    //    port=11211
    //    bucket=buck1
    //    usr=test1
    //    pwd=password
    //  tests
    //    test1
    //    test2
    //  def-test1
    //    ksize=16
    //    vsize=16
    //    iterations=500
    //  def-test2
    //    ksize=64
    //    vsize=524288
    //    iterations=50
    //
    if (!form) {
        return RV_BADARG;
    }

    char detail_key[200];

    // Discover test configuration.
    char **tests = get_key_values(form, "tests");
    int nrecipes = charlistlen(tests);
    struct ping_test_recipe recipes[nrecipes+1];
    memset(recipes, 0x00, sizeof(struct ping_test_recipe) * (nrecipes+1));
    for (int j = 0; j < nrecipes; j++) {
        snprintf(detail_key, sizeof(detail_key), "def-%s", tests[j]);
        recipes[j].name = strdup(detail_key);
        assert(recipes[j].name);
        load_ping_recipe(get_key_values(form, detail_key), &recipes[j]);
    }

    // Initialize each server and run the tests
    char **servers = get_key_values(form, "servers");
    for (int j = 0; servers != NULL && servers[j]; j++) {
        snprintf(detail_key, sizeof(detail_key),
                 "svr-%s", servers[j]);

        if (settings.verbose > 1) {
            moxi_log_write("ping_test %s\n", detail_key);
        }

        proxy_behavior behavior;

        memset(&behavior, 0, sizeof(behavior));

        char **props = get_key_values(form, detail_key);
        for (int k = 0; props && props[k]; k++) {
            cproxy_parse_behavior_key_val_str(props[k], &behavior);
        }

        ping_server(servers[j], recipes, &behavior, r);
    }

    /* The recipe memory allocations */
    for (int j = 0; j < nrecipes; j++) {
        free(recipes[j].name);
    }

    return RV_OK;
}
Exemplo n.º 3
0
int grib_tool_new_handle_action(grib_runtime_options* options, grib_handle* h) {
  int err=0;
  double *lat=0,*lon=0,*val=0;
  double missing_value=9999.;
  int skip_missing=1;
  char *kmiss=NULL, *p=NULL;
  char *missing_string=NULL;
  int i=0;
  grib_values* values=NULL;
  grib_iterator* iter = NULL;
  char* format=NULL;
  char* default_format="%.10e";
  int print_keys=grib_options_on("p:");
  long numberOfPoints=0;
  double *data_values=0,*lats=0,*lons=0;
  int n = 0;
  size_t size=0;

  if (grib_options_on("F:"))
    format=grib_options_get_option("F:");
  else
    format=default_format;

  if ((err=grib_get_long(h,"numberOfPoints",&numberOfPoints)) !=GRIB_SUCCESS) {
    fprintf(dump_file,"ERROR: unable to get number of points\n");
    return err;
  }

  iter=grib_iterator_new(h,0,&err);

  data_values=(double*)calloc(numberOfPoints+1,sizeof(double));

  if (iter) {
    lats=(double*)calloc(numberOfPoints+1,sizeof(double));
    lons=(double*)calloc(numberOfPoints+1,sizeof(double));
    lat=lats; lon=lons; val=data_values;
    while(grib_iterator_next(iter,lat++,lon++,val++)) {}
  } else if (err==GRIB_NOT_IMPLEMENTED || err==GRIB_SUCCESS){
    size=numberOfPoints;
    grib_get_double_array(h,"values",data_values,&size);
    if (size!=numberOfPoints) {
      if (!grib_options_on("q"))
       fprintf(dump_file,"ERROR: wrong number of points %d\n",(int)numberOfPoints);
      if (grib_options_on("f")) exit(1);
    }
  } else {
    grib_context_log(h->context,GRIB_LOG_ERROR,
                           "%s",grib_get_error_message(err));
    exit(err);
  }

  skip_missing=1;
  if (grib_options_on("m:")) {
    char* end=0;
    double mval=0;
    skip_missing=0;
    kmiss=grib_options_get_option("m:");
    p=kmiss;
    while (*p != ':' && *p != '\0') p++;
    if (*p == ':' && *(p+1) != '\0') {
      *p='\0';
      missing_string=strdup(p+1);
    } else {
      missing_string=strdup(kmiss);
    }
    mval=strtod(kmiss,&end);
    if (end==NULL) missing_value=mval;
    grib_set_double(h,"missingValue",missing_value);
  }

  if (iter)
    fprintf(dump_file,"Latitude, Longitude, ");

  fprintf(dump_file,"Value");

  if (print_keys)
    for (i=0;i<options->print_keys_count; i++)
      fprintf(dump_file,", %s",options->print_keys[i].name);

  fprintf(dump_file,"\n");

  if (print_keys)
    values=get_key_values(options,h);

  if (skip_missing==0){
    for (i=0;i<numberOfPoints;i++) {
      if (iter) fprintf(dump_file,"%9.3f%9.3f ",lats[i],lons[i]);

      if (data_values[i] == missing_value)
        fprintf(dump_file,"%s",missing_string);
      else
        fprintf(dump_file,format,data_values[i]);

      if (print_keys)
        print_key_values(values,options->print_keys_count);
      fprintf(dump_file,"\n");
      n++;
    }

  } else if ( skip_missing==1 ){
    for (i=0;i<numberOfPoints;i++) {
      if (data_values[i] != missing_value){
        if (iter) fprintf(dump_file,"%9.3f%9.3f ",lats[i],lons[i]);
        fprintf(dump_file,format,data_values[i]);
        if (print_keys)
          print_key_values(values,options->print_keys_count);
        fprintf(dump_file,"\n");
        n++;
      }
    }
  }

  if (iter) grib_iterator_delete(iter);

  free(data_values);
  if (iter) {
    free(lats);
    free(lons);
  }

  return 0;
}
Exemplo n.º 4
0
static
void cproxy_on_new_config(void *data0, void *data1) {
    work_collect *completion = data0;
    proxy_main *m = completion->data;
    assert(m);

    kvpair_t *kvs = data1;
    assert(kvs);
    assert(is_listen_thread());

    m->stat_configs++;

    uint32_t max_config_ver = 0;

    for (proxy *p = m->proxy_head; p != NULL; p = p->next) {
        pthread_mutex_lock(&p->proxy_lock);
        if (max_config_ver < p->config_ver) {
            max_config_ver = p->config_ver;
        }
        pthread_mutex_unlock(&p->proxy_lock);
    }

    uint32_t new_config_ver = max_config_ver + 1;

    if (settings.verbose > 2) {
        fprintf(stderr, "conc new_config_ver %u\n", new_config_ver);
    }

    // The kvs key-multivalues look roughly like...
    //
    //  pool-customer1-a
    //    svrname3
    //  pool-customer1-b
    //    svrname1
    //    svrname2
    //  svr-svrname1
    //    host=mc1.foo.net
    //    port=11211
    //    weight=1
    //    bucket=buck1
    //    usr=test1
    //    pwd=password
    //  svr-svrnameX
    //    host=mc2.foo.net
    //    port=11211
    //  behavior-customer1-a
    //    wait_queue_timeout=1000
    //    downstream_max=10
    //  behavior-customer1-b
    //    wait_queue_timeout=1000
    //    downstream_max=10
    //  pool_drain-customer1-b
    //    svrname1
    //    svrname3
    //  pools
    //    customer1-a
    //    customer1-b
    //  bindings
    //    11221
    //    11331
    //
    char **pools    = get_key_values(kvs, "pools");
    char **bindings = get_key_values(kvs, "bindings");

    if (pools == NULL) {
        goto fail;
    }

    int npools    = 0;
    int nbindings = 0;

    while (pools && pools[npools])
        npools++;

    while (bindings && bindings[nbindings])
        nbindings++;

    if (nbindings > 0 &&
        nbindings != npools) {
        if (settings.verbose > 1) {
            fprintf(stderr, "npools does not match nbindings\n");
        }
        goto fail;
    }

    char **behavior_kvs = get_key_values(kvs, "behavior");
    if (behavior_kvs != NULL) {
        // Update the default behavior.
        //
        proxy_behavior m_behavior = m->behavior;

        for (int k = 0; behavior_kvs[k]; k++) {
            char *bstr = trimstrdup(behavior_kvs[k]);
            if (bstr != NULL) {
                cproxy_parse_behavior_key_val_str(bstr, &m_behavior);
                free(bstr);
            }
        }

        m->behavior = m_behavior;
    }

    for (int i = 0; i < npools; i++) {
        char *pool_name = skipspace(pools[i]);
        if (pool_name != NULL &&
            pool_name[0] != '\0') {
            char buf[200];

            snprintf(buf, sizeof(buf), "pool-%s", pool_name);

            char **servers = get_key_values(kvs, trimstr(buf));
            if (servers != NULL) {
                // Parse proxy-level behavior.
                //
                proxy_behavior proxyb = m->behavior;

                if (parse_kvs_behavior(kvs, "behavior", pool_name, &proxyb)) {
                    if (settings.verbose > 1) {
                        cproxy_dump_behavior(&proxyb,
                                             "conc proxy_behavior", 1);
                    }
                }

                // The legacy way to get a port is through the bindings,
                // but they're also available as an inheritable
                // proxy_behavior field of port_listen.
                //
                int pool_port = proxyb.port_listen;

                if (i < nbindings &&
                    bindings != NULL &&
                    bindings[i]) {
                    pool_port = atoi(skipspace(bindings[i]));
                }

                if (pool_port > 0) {
                    // Number of servers in this pool.
                    //
                    int s = 0;
                    while (servers[s])
                        s++;

                    if (s > 0) {
                        // Parse server-level behaviors, so we'll have an
                        // array of behaviors, one entry for each server.
                        //
                        proxy_behavior_pool behavior_pool = {
                            .base = proxyb,
                            .num  = s,
                            .arr  = calloc(s, sizeof(proxy_behavior))
                        };

                        if (behavior_pool.arr != NULL) {
                            char *config_str =
                                parse_kvs_servers("svr", pool_name, kvs,
                                                  servers, &behavior_pool);
                            if (config_str != NULL &&
                                config_str[0] != '\0') {
                                if (settings.verbose > 2) {
                                    fprintf(stderr, "conc config: %s\n",
                                            config_str);
                                }

                                cproxy_on_new_pool(m, pool_name, pool_port,
                                                   config_str, new_config_ver,
                                                   &behavior_pool);

                                free(config_str);
                            }

                            free(behavior_pool.arr);
                        } else {
                            if (settings.verbose > 1) {
                                fprintf(stderr, "ERROR: oom on re-config malloc\n");;
                            }
                            goto fail;
                        }
                    } else {
                        // Note: ignore when no servers for an existing pool.
                        // Because the config_ver won't be updated, we'll
                        // fall into the empty_pool code path below.
                    }
                } else {