Ejemplo n.º 1
0
ushort packet::write(void* src, ushort len)
{
	write_len(len);
	memcpy(&raw[pos], src, len);
	pos += len;
	return len;
}
Ejemplo n.º 2
0
void vbs_get_config(void *arg) {

    vbs_config_t *config = (vbs_config_t *)arg;
    assert(config);
    int hb_interval = 0;
    int vbs_fd;
    int retry_connection = 0;

retry:


    vbs_fd = 0;
    if (retry_connection != 0) {
            sleep(5);
    }

    retry_connection++;

    while (vbs_fd <= 0) {

       vbs_fd = connect_server(config);

        if (vbs_fd < 0) {
            moxi_log_write("ERROR: Sleep for 5 seconds  %s\n", config->hostname);
            sleep(5);
        }
    }

    //connected process init command
    char *buf = NULL;
    if (read_socket(vbs_fd, &buf, hb_interval) < 0) {
        //retry connect
        moxi_log_write("ERROR: Unable to read from socket retry connection  %s\n", config->hostname);
        close(vbs_fd);
        goto retry;
    }
    free(buf);

    //send the moxi init command
    write_len(INIT_CMD, vbs_fd);
    if (write(vbs_fd, INIT_CMD, strlen(INIT_CMD)) < 0) {
        moxi_log_write("ERROR: Unable to write to socket retry connection  %s\n", config->hostname);
        close(vbs_fd);
        goto retry;
    }

    // main config loop. Receive config command from moxi
    while (1) {

        int read_len = 0;

        if ((read_len = read_socket(vbs_fd, &buf, hb_interval)) < 0) {
            moxi_log_write("ERROR: Unable to write to socket retry connection  %s\n", config->hostname);
            close(vbs_fd);
            goto retry;
        } else if (read_len == 0) {

            // heartbeat interval
            write_len(ALIVE_CMD, vbs_fd);
            if (write(vbs_fd, ALIVE_CMD, strlen(ALIVE_CMD)) < 0) {
                moxi_log_write("ERROR: Unable to write to socket retry connection  %s\n", config->hostname);
                close(vbs_fd);
                goto retry;
            }
        } else {

            //process moxi config command
            //test the json config here before passing it on the cproxy thread
            cJSON *c = cJSON_Parse(buf);
            if (c != NULL) {
                //json okay. extract heartbeat interval
                cJSON *data = NULL;
                cJSON *heartbeat = cJSON_GetObjectItem(c, "HeartBeatTime");
                if (heartbeat != NULL &&
                    heartbeat->type == cJSON_Number &&
                    heartbeat->valueint > 0) {
                    hb_interval = heartbeat->valueint;
                } else {
                    hb_interval = 10; // 10 seconds
                }

                data = cJSON_GetObjectItem(c, "Data");
                if (data == NULL) {
                    moxi_log_write("ERROR : Unable to parse JSON config\n");
                } else {
                    cJSON *vbucket_config = cJSON_GetObjectItem(data, "buckets");
                    if (vbucket_config == NULL) {
                        moxi_log_write("ERROR: Unable to get vbucket config\n");
                    } else {
                        free(vbs_data);
                        vbs_data = cJSON_Print(vbucket_config);
                    }
                }

                cJSON_Delete(c);
            }

            free(buf);

            write_len(OK_CMD, vbs_fd);
            if (write(vbs_fd, OK_CMD, strlen(OK_CMD)) < 0) {
                moxi_log_write("ERROR: Unable to write to socket retry connection  %s\n", config->hostname);
                close(vbs_fd);
                goto retry;
            }
        }
    }
}