int main(int argc, char *argv[]) {
	pid_t  pid;
	pid = fork();
	int rc = 0;
	rc = populate_servers(servers);
	if (rc == -1) {
		printf("FATAL: Could not populate server configuration !");
		return 0;
	}
	/* Create a child processes to run as server */
	if (pid == 0) {
		if (argv[1]) {
			struct client_data *data = (struct client_data *)calloc(1, sizeof(struct client_data));
			pthread_t thread;
			data->ip = servers[atoi(argv[1])][0];
			data->port = servers[atoi(argv[1])][1];
			data->hash_table = hash_table;
			pthread_create (&thread, NULL, start_server, (void *)data);
			pthread_join(thread, NULL);	
		} else {
			printf("Did not start the server as an instance wasn't specified\n");
		}
	} else {
		pthread_t client_thread;
		pthread_create (&client_thread, NULL, start_client, NULL);
		pthread_join(client_thread, NULL);	
		kill( pid, SIGINT );
	}	
	return 0;
}
Exemplo n.º 2
0
static VBUCKET_CONFIG_HANDLE parse_vbucket_config(VBUCKET_CONFIG_HANDLE vb, cJSON *c)
{
    cJSON *json, *config;

    config = cJSON_GetObjectItem(c, "vBucketServerMap");
    if (config == NULL || config->type != cJSON_Object) {
        /* seems like config without envelop, try to parse it */
        config = c;
    }

    json = cJSON_GetObjectItem(config, "numReplicas");
    if (json == NULL || json->type != cJSON_Number ||
        json->valueint > MAX_REPLICAS) {
        errstr = "Expected number <= " STRINGIFY(MAX_REPLICAS) " for numReplicas";
        return NULL;
    }
    vb->num_replicas = json->valueint;

    json = cJSON_GetObjectItem(config, "serverList");
    if (json == NULL || json->type != cJSON_Array) {
        errstr = "Expected array for serverList";
        return NULL;
    }
    vb->num_servers = cJSON_GetArraySize(json);
    if (vb->num_servers == 0) {
        errstr = "Empty serverList";
        return NULL;
    }
    if (populate_servers(vb, json) != 0) {
        return NULL;
    }
    /* optionally update server info using envelop (couchdb_api_base etc.) */
    json = cJSON_GetObjectItem(c, "nodes");
    if (json) {
        if (json->type != cJSON_Array) {
            errstr = "Expected array for nodes";
            return NULL;
        }
        if (update_server_info(vb, json) != 0) {
            return NULL;
        }
    }

    json = cJSON_GetObjectItem(config, "vBucketMap");
    if (json == NULL || json->type != cJSON_Array) {
        errstr = "Expected array for vBucketMap";
        return NULL;
    }
    vb->num_vbuckets = cJSON_GetArraySize(json);
    if (vb->num_vbuckets == 0 || (vb->num_vbuckets & (vb->num_vbuckets - 1)) != 0) {
        errstr = "Number of buckets must be a power of two > 0 and <= " STRINGIFY(MAX_BUCKETS);
        return NULL;
    }
    vb->mask = vb->num_vbuckets - 1;
    if (populate_buckets(vb, json, 0) != 0) {
        return NULL;
    }

    /* vbucket forward map could possibly be null */
    json = cJSON_GetObjectItem(config, "vBucketMapForward");
    if (json) {
        if (json->type != cJSON_Array) {
            errstr = "Expected array for vBucketMapForward";
            return NULL;
        }
        if (populate_buckets(vb, json, 1) !=0) {
            return NULL;
        }
    }

    return vb;
}