struct aws_dynamo_delete_item_response *aws_dynamo_delete_item(struct aws_handle *aws,
	const char *request, struct aws_dynamo_attribute *attributes, int num_attributes)
{
	const char *response;
	int response_len;
	struct aws_dynamo_delete_item_response *r;

	if (aws_dynamo_request(aws, AWS_DYNAMO_DELETE_ITEM, request) == -1) {
		return NULL;
	}

	response = http_get_data(aws->http, &response_len);

	if (response == NULL) {
		Warnx("aws_dynamo_delete_item: Failed to get response.");
		return NULL; 
	}

	if ((r = aws_dynamo_parse_delete_item_response(response, response_len,
		attributes, num_attributes)) == NULL) {
		Warnx("aws_dynamo_delete_item: Failed to parse response: '%s'", response);
		return NULL; 
	}

	return r;
}
struct aws_dynamo_create_table_response *aws_dynamo_create_table(struct
								 aws_handle
								 *aws, const char
								 *request)
{
	const char *response;
	int response_len;
	struct aws_dynamo_create_table_response *r;

	if (aws_dynamo_request(aws, AWS_DYNAMO_CREATE_TABLE, request) == -1) {
		return NULL;
	}

	response = http_get_data(aws->http, &response_len);

	if (response == NULL) {
		Warnx("aws_dynamo_create_table: Failed to get response.");
		return NULL;
	}

	if ((r = aws_dynamo_parse_create_table_response(response, response_len)) == NULL) {
		Warnx("aws_dynamo_create_table: Failed to parse response: '%s'", response);
		return NULL;
	}

	return r;
}
Exemplo n.º 3
0
int request_custom_command(buffer* recv_buf, server_stat* status) {
    unsigned char http_message[1000];      // used to hold http message from robot
    buffer* http_data = create_buffer(BUFFER_LEN);
    int n, retries;
    cst_header request_header;   // header from the client
    buffer* response;        // buffer to send to client
    struct timeval timeout;

    timeout.tv_sec = 0;
    timeout.tv_usec = 500000;

    fprintf(stdout, "\tProcessing request\n");

    // acknowledgement of command to client
    udp_send(recv_buf, status);

    // create the http request
    memset(http_message, '\0', 1000);
    request_header = extract_custom_header(recv_buf);
    switch (request_header.data[CST_COMMAND]) {
        case IMAGE:
            fprintf(stdout, "\t\tContacting image port\n");
            snprintf((char*)http_message, 100, "GET /snapshot?topic=/robot_%d/image?width=600?height=500 HTTP/1.1\r\n\r\n", status->r_stat.id);
            status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, IMAGE_PORT);
            break;
        case GPS:
            fprintf(stdout, "\t\tContacting gps port\n");
            snprintf((char*)http_message, 100, "GET /state?id=%s HTTP/1.1\r\n\r\n", status->r_stat.name);
            status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, GPS_PORT);
            break;
        case LASERS:
            fprintf(stdout, "\t\tContacting lasers port\n");
            snprintf((char*)http_message, 100, "GET /state?id=%s HTTP/1.1\r\n\r\n", status->r_stat.name);
            status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, LASERS_PORT);
            break;
        case dGPS:
            fprintf(stdout, "\t\tContacting dgps port\n");
            snprintf((char*)http_message, 100, "GET /state?id=%s HTTP/1.1\r\n\r\n", status->r_stat.name);
            status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, dGPS_PORT);
            break;
        case MOVE:
            fprintf(stdout, "\t\tContacting move port\n");
            snprintf((char*)http_message, 100, "GET /twist?id=%s&lx=1 HTTP/1.1\r\n\r\n", status->r_stat.name);
            status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, GPS_PORT);
            break;
        case TURN:
            fprintf(stdout, "\t\tContacting turn port\n");
            snprintf((char*)http_message, 100, "GET /twist?id=%s&az=1 HTTP/1.1\r\n\r\n", status->r_stat.name);
            status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, GPS_PORT);
            break;
        case STOP:
            fprintf(stdout, "\t\tContacting stop port\n");
            snprintf((char*)http_message, 100, "GET /twist?id=%s&lx=0 HTTP/1.1\r\n\r\n", status->r_stat.name);
            status->r_stat.http_sock = tcp_connect(status->r_stat.hostname, GPS_PORT);
            break;
        default:
            fprintf(stderr, "ERROR: Invalid client request\n");
            return -2;
            break;
    }

    // send the http request
    fprintf(stdout, "\t\tWriting request to server\n");
    timeout_setup(status->r_stat.http_sock, timeout);
    write(status->r_stat.http_sock, (char*)http_message, strlen((char*)http_message));

    // read http message into a buffer
    fprintf(stdout, "\t\tReceiving reply from server\n");
    memset(http_message, '\0', 1000);
    retries = 0;
    while (1) {
        n = read(status->r_stat.http_sock, (char*)http_message, 1000);
        if (n == -1) {
            if (retries > 2) {
                break;
            }
            retries++;
            write(status->r_stat.http_sock, (char*)http_message, strlen((char*)http_message));
            continue;
        } else if (n == 0) {
            break;
        }
        fprintf(stdout, "\t\t\tReceived %d bytes\n", n);
        append_buffer(http_data, (unsigned char*)http_message, n);
        memset(http_message, '\0', 1000);
    }
    fprintf(stdout, "\t\tTotal bytes received: %d\n", http_data->len);


    // see what we go
    fprintf(stdout, "\t\tAssembled Message:\n%s\n", http_data->data);

    // check for '200 OK'
    char ret_code[4];
    memcpy(ret_code, http_data->data + 9, 3); // HTTP/1.1 <ret_code> ~~~~
    ret_code[3] = '\0';
    if (atoi(ret_code) != 200) {
        fprintf(stderr, "ERROR: bad http request\n");
        response = create_custom_message(GROUP_NUMBER, status->password, HTTP_ERROR, 0, 0, 0);
        udp_send(response, status);
        return 0;
    }

    // send it to client
    int http_header_len = http_get_data(http_data->data) - http_data->data;
    int a = 0;
    while ((a + http_header_len) < http_data->len) {
        response = create_custom_message(request_header.data[CST_VERSION],
                request_header.data[CST_PASSWORD],
                request_header.data[DATA],
                request_header.data[CST_SEQUENCE],
                (http_data->len - http_header_len),
                ((http_data->len - (a + http_header_len)) > CST_MAX_PAYLOAD ? CST_MAX_PAYLOAD:(http_data->len - (a + http_header_len))));

        append_buffer(response, http_data->data + a + http_header_len, CST_MAX_PAYLOAD);
        fprintf(stdout, "\n\t\tAssembled packet:\n");
        print_header(response);
        fprintf(stdout, "%s\n", response->data + UP_HEADER_LEN);
        fprintf(stdout, "\t\tSending packet\n");
        udp_send(response, status);
        fprintf(stdout, "\t\tPacket sent\n");
        delete_buffer(response);
        a += 370;
    }
    fprintf(stdout, "\t\tRequest complete!\n");
    return 1;
}