bool setup_test(size_t object_size) {
    object_t* object = benchmark_object_create(object_size);
    binn root;
    bool ok;
    if (object->type == type_map) {
        binn_create_object(&root);
        ok = write_object(&root, object);
    } else {
        binn_create_list(&root);
        ok = write_list(&root, object);
    }
    object_destroy(object);

    if (!ok) {
        fprintf(stderr, "binn error writing data!\n");
        binn_free(&root);
        return false;
    }

    char filename[64];
    benchmark_filename(filename, sizeof(filename), object_size, BENCHMARK_FORMAT_BINN, NULL);
    FILE* file = fopen(filename, "wb");
    fwrite(binn_ptr(&root), binn_size(&root), 1, file);
    fclose(file);

    // like libbson, we call binn_free() regardless of whether
    // we placed it on the stack or the heap; it stores a flag
    // to say whether it should be freed. this apparently doesn't
    // cause warnings since GCC chooses not to inline binn_free().
    binn_free(&root);
    return true;
}
/**
 * Response message
 *
 * @param snum message code (1 - error, 2 - critical error, 3 - unknown command, 100 - okay)
 * @param sdesc text message
 * @param write
 */
void CommandsSession::response_msg(unsigned int snum, const char * sdesc, bool write)
{
    binn *write_binn = binn_list();
    binn_list_add_uint32(write_binn, snum);
    binn_list_add_str(write_binn, (char *)sdesc);

    m_write_msg = std::string(static_cast<char*>(binn_ptr(write_binn)), static_cast<uint>(binn_size(write_binn)));
}
Exemple #3
0
int dsm_send_msg(int nodefd, dsm_message_t *msg)
{
	binn *obj;
	
	obj = binn_object();
	binn_object_set_int32(obj, DSM_MSG_KEY_TYPE, msg->type);

	switch (msg->type) {
		case CONNECT:
			binn_object_set_int32(obj, DSM_MSG_KEY_BITNESS, msg->connect_args.bitness);
			binn_object_set_int32(obj, DSM_MSG_KEY_PAGESIZE, msg->connect_args.pagesize);
			break;
		case CONNECT_ACK:
			binn_object_set_int16(obj, DSM_MSG_KEY_BITNESS, msg->connect_ack_args.bitness_ok);
			binn_object_set_int16(obj, DSM_MSG_KEY_PAGESIZE, msg->connect_ack_args.pagesize_ok);
			binn_object_set_int32(obj, DSM_MSG_KEY_PAGECOUNT, msg->connect_ack_args.page_count);
			break;
		case LOCKPAGE:
			binn_object_set_int32(obj, DSM_MSG_KEY_PAGEID, msg->lockpage_args.page_id);
			binn_object_set_int16(obj, DSM_MSG_KEY_RIGHTS, msg->lockpage_args.access_rights);
			break;
		case INVALIDATE:
			binn_object_set_int32(obj, DSM_MSG_KEY_PAGEID, msg->invalidate_args.page_id);
			break;
		case INVALIDATE_ACK:
			binn_object_set_int32(obj, DSM_MSG_KEY_PAGEID, msg->invalidate_ack_args.page_id);
			break;
		case GIVEPAGE:
			binn_object_set_int32(obj, DSM_MSG_KEY_PAGEID, msg->givepage_args.page_id);
			binn_object_set_int16(obj, DSM_MSG_KEY_RIGHTS, msg->givepage_args.access_rights);
			binn_object_set_blob(obj, DSM_MSG_KEY_DATA, msg->givepage_args.data, dsm_g->mem->pagesize);
			break;
		case SYNC_BARRIER:
			binn_object_set_int16(obj, DSM_MSG_KEY_BARRIER, msg->sync_barrier_args.slave_to_wait);
		case BARRIER_ACK:
			break;
		case TERMINATE:
			break;
		default:
        	log("Unknown message type to send !\n");
        	return -1;
	}

	if(dsm_send(nodefd, binn_ptr(obj), binn_size(obj)) < 0) {
		log("Could not send to node %d message type: %d\n", nodefd, msg->type);
		return -1;
	}
	binn_free(obj);

	return 0;
}
/**
 * Main command operations
 */
void CommandsSession::cmd_process()
{
    binn *read_binn;
    read_binn = binn_open((void*)&m_read_msg[0]);

    unsigned char command;

    if (!binn_list_get_uint8(read_binn, 1, &command)) {
        response_msg(STATUS_ERROR, "Invalid Binn data: reading command failed", true);
        return;
    }

    switch (command) {
        case COMMAND_EXEC: {
            char * exec_command;
            char * work_dir;

            if (!binn_list_get_str(read_binn, 2, &exec_command)) {
                response_msg(STATUS_ERROR, "Invalid exec command", true);
                break;
            }

            if (!binn_list_get_str(read_binn, 3, &work_dir)) {
                response_msg(STATUS_ERROR, "Invalid work directory", true);
                break;
            }

            if (strlen(work_dir) == 0) {
                response_msg(STATUS_ERROR, "Empty work directory", true);
                break;
            }

            if (!fs::is_directory(work_dir)) {
                response_msg(STATUS_ERROR, "Invalid work directory", true);
                break;
            }

            fs::current_path(work_dir);

            std::cout << "Executing command: \"" << exec_command << "\"" << std::endl;
            std::string out;
            int exit_code = GameAP::exec(std::string(exec_command), out);

            binn *write_binn = binn_list();
            binn_list_add_uint32(write_binn, STATUS_OK);
            binn_list_add_int32(write_binn, exit_code);
            binn_list_add_str(write_binn, &out[0]);

            m_write_msg = std::string(static_cast<char*>(binn_ptr(write_binn)), static_cast<uint>(binn_size(write_binn)));

            break;
        };

        default: {
            std::cerr << "Unknown Command" << std::endl;
            response_msg(STATUS_UNKNOWN_COMMAND, "Unknown command", true);
            return;
        };
    }

    do_write();
}