Ejemplo n.º 1
0
int main(int argc, char **argv) {
  char buffer[512];
  char p[2048];
  char *cert=0;
  char appId[64];

  strcpy(appId, "example.foo_test");
  cert = foo_get_cert_once(appId);
  if(cert!=0) {
    fprintf(stderr, "Certificate for appid [%s] is [%s]\n",appId,cert);
    strncpy(p,cert, sizeof(p)-1);
    free(cert);
  } else {
    fprintf(stderr, "Certificate for appid [%s] not found\n",appId);
    strcpy(p, "dummy");
  }

  zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);

  zh = zookeeper_init("localhost:2181", watcher, 10000, 0, 0, 0);
  if (!zh) {
	fprintf(stderr, "Error1: %s\n", strerror(errno));
    return errno;
  }
  if (zoo_add_auth(zh, "foo", p, strlen(p), 0, 0) != ZOK) {
    fprintf(stderr, "Error2: %s\n", strerror(errno));
    return 2;
  }

  struct ACL CREATE_ONLY_ACL[] = {{ZOO_PERM_CREATE, ZOO_AUTH_IDS}};
  struct ACL_vector CREATE_ONLY = {1, CREATE_ONLY_ACL};
  int rc = zoo_create(zh, "/xyz", "value", 5, &CREATE_ONLY, ZOO_EPHEMERAL, (char*)&buffer, sizeof(buffer) - 1);
  if (!rc) {
    fprintf(stderr, "Error3: %s\n", strerror(errno));
    return errno;
  }

  /** this operation will fail with a ZNOAUTH error */
  int buflen= sizeof(buffer);
  struct Stat stat;
  rc = zoo_get(zh, "/xyz", 0, buffer, &buflen, &stat);
  if (rc) {
    fprintf(stderr, "Error4: %d for %s\n", rc, __LINE__);
  } else {
	fprintf(stdout, "Got %s\n", (char*)&buffer);
  }

  zookeeper_close(zh);
  return 0;
}
Ejemplo n.º 2
0
int main()
{
  char buffer[512];
  char p[2048];
  char *cert=0;
  char appId[64];
 
  strcpy(appId, "example.foo_test");
  cert = foo_get_cert_once(appId);
  if(cert!=0) {
    fprintf(stderr,
            "Certificate for appid [%s] is [%s]\n",appId,cert);
    strncpy(p,cert, sizeof(p)-1);
    free(cert);
  } else {
    fprintf(stderr, "Certificate for appid [%s] not found\n",appId);
    strcpy(p, "dummy");
  }
 
  zoo_set_debug_level(ZOO_LOG_LEVEL_ERROR);
 
  zh = zookeeper_init("127.0.0.1:2181", watcher, 10000, 0, 0, 0);
  zh2 = zookeeper_init("127.0.0.1:2181", watcher, 10000, 0, 0, 0);
  // zookeeper_init是一个异步函数需要等待zh->state非0
  while(zoo_state(zh) == 0) {}
  while(zoo_state(zh2) == 0) {}

// struct Id id_tmp = {(char*)"digest", (char*)"user:tpUq/4Pn5A64fVZyQ0gOJ8ZWqkY="};

  int rc = zoo_add_auth(zh,"digest", "dbscale:dbscale",strlen("dbscale:dbscale"),0,0);
  if (rc == ZOK){
    printf("zoo add auth sucess\n");
  } else {
    printf("zoo add auth failed\n");
  }
 // struct ACL CREATE_ONLY_ACL[] = {{ZOO_PERM_ALL, id_tmp}};//, {ZOO_PERM_READ, ZOO_AUTH_IDS}, {ZOO_PERM_WRITE, ZOO_AUTH_IDS}};
//  struct ACL_vector CREATE_ONLY = {1, CREATE_ONLY_ACL};
  rc = zoo_create(zh,"/xyz","value", 5, &ZOO_CREATOR_ALL_ACL/*&CREATE_ONLY*/, ZOO_EPHEMERAL,
                      buffer, sizeof(buffer)-1);
 
  if (rc == ZOK) {
    printf("create ok \n");
  } else {
    printf("create faild%d\n" ,rc);
  }

  struct ACL_vector ret;
  printf("zoo set acl %d\n", zoo_get_acl(zh, "/xyz", &ret, 0));
  rc = zoo_exists(zh, "/xyz", 0, 0);
  if (rc != ZOK) {
    printf("%d exist fail\n", rc);
  } else {
    printf("/xyz is exist\n");
  }
  /** this operation will fail with a ZNOAUTH error */
  int buflen= sizeof(buffer);
  struct Stat stat;
  rc = zoo_exists(zh2, "/xyz", 0, 0);
if (rc == ZNOAUTH) {
    fprintf(stderr, "Error %d for %d\n", rc, __LINE__);
  }
  else {
    printf("zh2 exist %s\n", buffer);
  }

  rc = zoo_get(zh2, "/xyz", 0, buffer, &buflen, &stat);
  if (rc == ZNOAUTH) {
    fprintf(stderr, "Error %d for %d\n", rc, __LINE__);
  }
  else {
    printf("get data %s\n", buffer);
  }
   if (rc == ZOK){
    printf("zoo create xyz  sucess\n");
  } else {
    printf("zoo create xyz  failed %s\n", zerror(rc));
  }

  rc = zoo_add_auth(zh2,"digest", "dbscale:dbscale",strlen("dbscale:dbscale"),0,0);
  if (rc == ZOK){
    printf("zoo add auth sucess\n");
  } else {
    printf("zoo add auth failed\n");
  }
  rc = zoo_get(zh2, "/xyz", 0, buffer, &buflen, &stat);
  if (rc == ZNOAUTH) {
    fprintf(stderr, "Error %d for %d\n", rc, __LINE__);
  }
  else {
    printf("get data %s\n", buffer);
  }

  rc = zoo_wget(zh2, "/xyz", watcher, NULL, buffer, &buflen, &stat);
  if (rc == ZNOAUTH) {
    fprintf(stderr, "Error %d for %d\n", rc, __LINE__);
  }
  else {
    printf("wget sucess %s\n", buffer);
  }

  zookeeper_close(zh);
  return 0;
}