예제 #1
0
static bool
producer_make_data(void *data)
{
    void *v;
    size_t size;
    struct sol_blob *blob;
    struct sol_buffer buf = SOL_BUFFER_INIT_EMPTY;
    static uint16_t packets_created = 0;
    bool keep_running = true;
    int r;

    //Stop the production until the pendind blob is sent
    if (pending_blob) {
        printf("Waiting for blob data: %.*s to be transfered.\n",
            SOL_STR_SLICE_PRINT(sol_str_slice_from_blob(pending_blob)));
        return true;
    }

    packets_created++;

    //Generate data
    if (packets_created != MAX_PACKETS)
        r = sol_util_uuid_gen(true, true, &buf);
    else {
        r = sol_buffer_append_slice(&buf, sol_str_slice_from_str("close"));
        keep_running = false;
    }

    if (r < 0) {
        fprintf(stderr, "Could not create the UUID - Reason: %s\n",
            sol_util_strerrora(-r));
        goto err_exit;
    }

    v = sol_buffer_steal(&buf, &size);

    blob = sol_blob_new(&SOL_BLOB_TYPE_DEFAULT, NULL,
        v, size + 1);

    if (!blob) {
        fprintf(stderr, "Could not alloc memory for the blob\n");
        goto err_exit;
    }

    //Send it
    if (!send_blob(blob))
        goto err_exit;

    if (!keep_running)
        goto exit;
    return true;

err_exit:
    sol_quit();
exit:
    producer_timeout = NULL;
    return false;
}
예제 #2
0
struct sol_file_reader *
sol_file_reader_open(const char *filename)
{
    int fd = -1, saved_errno;
    struct sol_file_reader *fr, *result = NULL;
    struct sol_buffer *buffer;
    size_t size;

    fr = malloc(sizeof(*fr));
    if (!fr)
        return NULL;

    fr->mmapped = false;

    fd = open(filename, O_RDONLY | O_CLOEXEC);
    if (fd < 0)
        goto err;

    if (fstat(fd, &fr->st) < 0)
        goto err;

    fr->contents = mmap(NULL, fr->st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (fr->contents != MAP_FAILED) {
        fr->mmapped = true;
        goto success;
    } else if (errno == ENOMEM) {
        goto err;
    }

    buffer = sol_util_load_file_raw(fd);
    if (!buffer)
        goto err;
    fr->contents = sol_buffer_steal(buffer, &size);
    fr->st.st_size = size;
    free(buffer);

success:
    result = fr;
    fr = NULL;
err:
    saved_errno = errno;
    free(fr);
    if (fd >= 0)
        close(fd);
    errno = saved_errno;
    return result;
}
예제 #3
0
static int
child_read(struct sol_blob **p_blob, bool *eof, int fd)
{
    struct sol_buffer buf = SOL_BUFFER_INIT_EMPTY;
    struct timespec start = sol_util_timespec_get_current();
    size_t size;
    void *v;
    int ret = 0;

    *eof = false;
    do {
        struct timespec now = sol_util_timespec_get_current();
        struct timespec elapsed;
        ssize_t r;

        sol_util_timespec_sub(&now, &start, &elapsed);
        if (elapsed.tv_sec > 0 ||
            elapsed.tv_nsec > (time_t)CHUNK_MAX_TIME_NS)
            break;

        r = sol_util_fill_buffer(fd, &buf, CHUNK_READ_SIZE);
        if (r == 0) {
            *eof = true;
            break;
        } else if (r < 0) {
            /* Not a problem if failed because buffer could not be increased */
            if (r != -ENOMEM)
                ret = -errno;
            break;
        }
    } while (1);

    if (ret < 0 && ret != -EAGAIN) {
        sol_buffer_fini(&buf);
        return ret;
    }

    v = sol_buffer_steal(&buf, &size);
    *p_blob = sol_blob_new(&SOL_BLOB_TYPE_DEFAULT, NULL, v, size);
    SOL_NULL_CHECK_GOTO(*p_blob, blob_error);
    return 0;

blob_error:
    sol_buffer_fini(&buf);
    return -ENOMEM;
}
예제 #4
0
SOL_API void *
sol_buffer_steal_or_copy(struct sol_buffer *buf, size_t *size)
{
    void *r;

    SOL_NULL_CHECK(buf, NULL);

    r = sol_buffer_steal(buf, size);
    if (!r) {
        r = sol_util_memdup(buf->data, buf->used);
        SOL_NULL_CHECK(r, NULL);

        if (size)
            *size = buf->used;
    }

    return r;
}
예제 #5
0
SOL_API struct sol_file_reader *
sol_file_reader_from_fd(int fd)
{
    int saved_errno;
    struct sol_file_reader *fr, *result = NULL;
    struct sol_buffer *buffer;
    size_t size;

    fr = malloc(sizeof(*fr));
    SOL_NULL_CHECK(fr, NULL);

    fr->mmapped = false;

    if (fstat(fd, &fr->st) < 0)
        goto err;

    fr->contents = mmap(NULL, fr->st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    if (fr->contents != MAP_FAILED) {
        fr->mmapped = true;
        goto success;
    } else if (errno == ENOMEM) {
        goto err;
    }

    buffer = sol_util_load_file_fd_raw(fd);
    if (!buffer)
        goto err;
    fr->contents = sol_buffer_steal(buffer, &size);
    fr->st.st_size = size;
    free(buffer);

success:
    result = fr;
    fr = NULL;
err:
    saved_errno = errno;
    free(fr);
    errno = saved_errno;
    return result;
}
예제 #6
0
/*
 *----------------------- string --------------------------
 */
static void
string_read_data(void *data, int fd)
{
    struct unix_socket_data *mdata = data;
    struct sol_buffer buf = SOL_BUFFER_INIT_EMPTY;
    char *str;
    size_t len;
    int r;

    if (FILL_BUFFER(fd, len) < 0)
        return;

    r = sol_util_fill_buffer_exactly(fd, &buf, len);
    if (r < 0)
        goto end;

    str = sol_buffer_steal(&buf, NULL);
    sol_flow_send_string_take_packet(mdata->node,
        SOL_FLOW_NODE_TYPE_UNIX_SOCKET_STRING_READER__OUT__OUT, str);

end:
    sol_buffer_fini(&buf);
}
예제 #7
0
static int
light_resource_to_rep(const struct sol_coap_resource *resource,
    bool state, struct sol_buffer *buf)
{
    SOL_BUFFER_DECLARE_STATIC(buffer, 64);
    int r;

    r = sol_coap_uri_path_to_buf(resource->path, &buffer, 0, NULL);
    SOL_INT_CHECK(r, < 0, r);

    r = sol_buffer_append_printf(buf,
        OC_CORE_ELEM_JSON_START, (char *)sol_buffer_steal(&buffer, NULL));
    SOL_INT_CHECK(r, < 0, r);

    r = sol_buffer_append_printf(buf,
        OC_CORE_PROP_JSON_NUMBER, "power", 100);
    SOL_INT_CHECK(r, < 0, r);

    r = sol_buffer_append_printf(buf, OC_CORE_JSON_SEPARATOR);
    SOL_INT_CHECK(r, < 0, r);

    r = sol_buffer_append_printf(buf,
        OC_CORE_PROP_JSON_STRING, "name", "Soletta LAMP!");
    SOL_INT_CHECK(r, < 0, r);

    r = sol_buffer_append_printf(buf, OC_CORE_JSON_SEPARATOR);
    SOL_INT_CHECK(r, < 0, r);

    r = sol_buffer_append_printf(buf,
        OC_CORE_PROP_JSON_BOOLEAN, "state", state ? "true" : "false" );
    SOL_INT_CHECK(r, < 0, r);

    r = sol_buffer_append_printf(buf, OC_CORE_ELEM_JSON_END);
    SOL_INT_CHECK(r, < 0, r);

    return 0;
}
예제 #8
0
SOL_API char *
sol_util_load_file_fd_string(int fd, size_t *size)
{
    int r;
    size_t size_read;
    char *data = NULL;
    struct sol_buffer buf = SOL_BUFFER_INIT_EMPTY;

    r = sol_util_load_file_fd_buffer(fd, &buf);
    SOL_INT_CHECK_GOTO(r, < 0, err);
    r = sol_buffer_trim(&buf);
    SOL_INT_CHECK_GOTO(r, < 0, err);
    data = sol_buffer_steal(&buf, &size_read);
    SOL_NULL_CHECK_GOTO(data, err);

    if (size)
        *size = size_read;
    return data;

err:
    if (size)
        *size = 0;
    return NULL;
}
예제 #9
0
char *
sol_util_load_file_fd_string(int fd, size_t *size)
{
    int saved_errno;
    size_t size_read;
    char *data = NULL;
    struct sol_buffer *buffer = NULL;

    buffer = sol_util_load_file_raw(fd);
    if (!buffer) {
        data = strdup("");
        size_read = 1;
    } else {
        buffer->flags = SOL_BUFFER_FLAGS_DEFAULT;
        if (sol_buffer_ensure_nul_byte(buffer) < 0)
            goto err;

        data = sol_buffer_steal(buffer, &size_read);
        if (!data)
            goto err;
    }

    sol_buffer_free(buffer);
    if (size)
        *size = size_read;
    return data;

err:
    saved_errno = errno;
    free(data);
    sol_buffer_free(buffer);
    errno = saved_errno;
    if (size)
        *size = 0;
    return NULL;
}