static int
transfer_next_block (BlockTxClient *client)
{
    TransferTask *task = client->info->task;

    if (client->curr_block_id) {
        g_queue_pop_head (task->block_ids);
        g_free (client->curr_block_id);
        client->curr_block_id = NULL;
    }

    if (g_queue_get_length (task->block_ids) == 0) {
        seaf_debug ("Transfer blocks done.\n");
        client->info->result = BLOCK_CLIENT_SUCCESS;
        client->break_loop = TRUE;
        return 0;
    }

    client->curr_block_id = g_queue_peek_head (task->block_ids);

    if (task->type == TASK_TYPE_UPLOAD) {
        seaf_debug ("Put block %s.\n", client->curr_block_id);

        if (send_block_header (client, REQUEST_COMMAND_PUT) < 0) {
            seaf_warning ("Failed to send block header for PUT %s.\n",
                          client->curr_block_id);
            return -1;
        }

        if (send_block_content (client) < 0) {
            seaf_warning ("Failed to send block content for %s.\n",
                          client->curr_block_id);
            return -1;
        }

        seaf_debug ("recv_state set to HEADER.\n");

        client->parser.content_cb = handle_block_header_content_cb;
        client->recv_state = RECV_STATE_HEADER;
    } else {
        seaf_debug ("Get block %s.\n", client->curr_block_id);

        if (send_block_header (client, REQUEST_COMMAND_GET) < 0) {
            seaf_warning ("Failed to send block header for GET %s.\n",
                          client->curr_block_id);
            return -1;
        }

        seaf_debug ("recv_state set to HEADER.\n");

        client->parser.content_cb = handle_block_header_content_cb;
        client->parser.fragment_cb = save_block_content_cb;
        client->recv_state = RECV_STATE_HEADER;
    }

    return 0;
}
 //
 // process_request
 //
 // Generate a report based on the supplied request.
 // XXX_JML number of commands is currently small and performance is not an issue
 // XXX_JML later might want to be table driven.
 // XXX_JML request's firs arg is object to respond.
 //
 void Monitor::process_request() {
     if (_argc > 0) {
         // get the command
         char *command = _args[0];
         if (is_equal(command, "blocks"))     { send_all_blocks();           return; }
         if (is_equal(command, "rootblocks")) { send_root_blocks();          return; }
         if (is_equal(command, "content"))    { send_block_content();        return; }
         if (is_equal(command, "describe"))   { send_block_description();    return; }
         if (is_equal(command, "leaks"))      { send_leaks();                return; }
         if (is_equal(command, "references")) { send_references();           return; }
         if (is_equal(command, "roots"))      { send_roots();                return; }
         if (is_equal(command, "samples"))    { send_zone_samples();         return; }
         if (is_equal(command, "samplesAll")) { send_process_samples();      return; }
         if (is_equal(command, "zones"))      { send_all_zones();            return; }
         print("Unknown command %s\n", command);
     }
 }
Beispiel #3
0
static int
handle_block_header_content_cb (char *content, int clen, void *cbarg)
{
    BlockTxServer *server = cbarg;
    RequestHeader *hdr;

    if (clen != sizeof(RequestHeader)) {
        seaf_warning ("Invalid block request header length %d.\n", clen);
        send_block_response_header (server, STATUS_BAD_REQUEST);
        return -1;
    }

    hdr = (RequestHeader *)content;
    hdr->command = ntohl (hdr->command);

    if (hdr->command != REQUEST_COMMAND_GET &&
        hdr->command != REQUEST_COMMAND_PUT) {
        seaf_warning ("Unknow command %d.\n", hdr->command);
        send_block_response_header (server, STATUS_BAD_REQUEST);
        return -1;
    }

    server->command = hdr->command;
    memcpy (server->curr_block_id, hdr->block_id, 40);

    if (server->command == REQUEST_COMMAND_GET) {
        BlockMetadata *md;
        int block_size;

        seaf_debug ("Received GET request for block %s.\n", server->curr_block_id);

        md = seaf_block_manager_stat_block (seaf->block_mgr,
                                            server->store_id,
                                            server->repo_version,
                                            server->curr_block_id);
        if (!md) {
            seaf_warning ("Failed to stat block %s.\n", server->curr_block_id);
            send_block_response_header (server, STATUS_NOT_FOUND);
            return -1;
        }
        block_size = md->size;
        g_free (md);

        if (send_block_response_header (server, STATUS_OK) < 0)
            return -1;

        if (send_block_content (server, block_size) < 0)
            return -1;

        seaf_debug ("recv_state set to HEADER.\n");

        server->recv_state = RECV_STATE_HEADER;
    } else {
        seaf_debug ("Received PUT request for block %s.\n", server->curr_block_id);

        server->block = seaf_block_manager_open_block (seaf->block_mgr,
                                                       server->store_id,
                                                       server->repo_version,
                                                       server->curr_block_id,
                                                       BLOCK_WRITE);
        if (!server->block) {
            seaf_warning ("Failed to open block %s for write.\n",
                          server->curr_block_id);
            send_block_response_header (server, STATUS_INTERNAL_SERVER_ERROR);
            return -1;
        }

        seaf_debug ("recv_state set to CONTENT.\n");

        server->parser.fragment_cb = save_block_content_cb;
        server->recv_state = RECV_STATE_CONTENT;
    }

    return 0;
}