<commit_before>/*
 * CamIO - The Cambridge Input/Output API 
 * Copyright (c) 2015, All rights reserved.
 * See LICENSE.txt for full details. 
 * 
 *  Created:   May 2, 2015
 *  File name: camio_udp_test.c
 *  Description:
 *  Test the features of the CamIO API regardless of the stream used
 */

#include <stdio.h>
#include <src/api/api_easy.h>
#include <src/camio_debug.h>


USE_CAMIO;

int main(int argc, char** argv)
{
    //We don't use these for the test ... yet
    (void)argc;
    (void)argv;

    //Create a new multiplexer for streams to go into
    camio_mux_t* mux = NULL;
    camio_error_t err = camio_mux_new(CAMIO_MUX_HINT_PERFORMANCE, &mux);

    //Create and connect to a new stream
    camio_stream_t* stream = NULL;
    err = camio_stream_new("udp:localhost:2000?wp=4000", &stream);
    if(err){ DBG("Could not connect to stream\n"); return CAMIO_EINVALID; /*TODO XXX put a better error here*/ }

    //Put the read stream into the mux
    camio_mux_insert(mux,&stream->rd_muxable);

   //Read and write bytes to and from the stream - just do loopback for now
    camio_rd_buffer_t* rd_buffer = NULL;
    while(1){
        camio_read_request(stream,0,0); //Tell the stream that we want some data

        camio_muxable_t* which = NULL;
        camio_mux_select(mux,&which); //Block waiting for a stream to be ready

        err = camio_read_acquire(which->parent.stream, &rd_buffer);
        if(err){ DBG("Got a read error %i\n", err); return -1; }

        //Get a buffer for writing stuff
        camio_rd_buffer_t* wr_buffer = NULL;
        err = camio_write_acquire(stream, &wr_buffer);
        if(err){ DBG("Could not connect to stream\n"); return CAMIO_EINVALID; /*TODO XXX put a better error here*/ }

        //Got some new data on the read stream, copy it over to the write stream
        err = camio_copy_rw(&wr_buffer,&rd_buffer,0,rd_buffer->data_len);
        if(err){ DBG("Got a copy error %i\n", err); return -1; }

        //Data copied, let's commit it and send it off
        err = camio_write_commit(stream, &wr_buffer );
        if(err){ DBG("Got a commit error %i\n", err); return -1; }

        //Done with the write buffer
        camio_write_release(stream,&wr_buffer);

        //And we're done with that buffer
        err = camio_read_release(stream, &rd_buffer);
        if(err){ DBG("Got a read release error %i\n", err); return -1; }
    }

    //Done with the write buffer too


    return 0;
}
<commit_msg>A little bit of code cleanup<commit_after>/*
 * CamIO - The Cambridge Input/Output API 
 * Copyright (c) 2015, All rights reserved.
 * See LICENSE.txt for full details. 
 * 
 *  Created:   May 2, 2015
 *  File name: camio_udp_test.c
 *  Description:
 *  Test the features of the CamIO API regardless of the stream used
 */

#include <stdio.h>
#include <src/api/api_easy.h>
#include <src/camio_debug.h>


USE_CAMIO;

int main(int argc, char** argv)
{
    //We don't use these for the test ... yet
    (void)argc;
    (void)argv;

    //Create a new multiplexer for streams to go into
    camio_mux_t* mux = NULL;
    camio_error_t err = camio_mux_new(CAMIO_MUX_HINT_PERFORMANCE, &mux);

    //Create and connect to a new stream
    camio_stream_t* stream = NULL;
    err = camio_stream_new("udp:localhost:2000?wp=4000", &stream);
    if(err){ DBG("Could not connect to stream\n"); return CAMIO_EINVALID; /*TODO XXX put a better error here*/ }

    //Put the read stream into the mux
    camio_mux_insert(mux,&stream->rd_muxable);

   //Read and write bytes to and from the stream - just do loopback for now
    camio_rd_buffer_t* rd_buffer = NULL;
    camio_rd_buffer_t* wr_buffer = NULL;
    camio_muxable_t* which       = NULL;
    while(1){
        //Tell the stream that we want some data
        camio_read_request(stream,0,0);

        //Block waiting for a stream to be ready
        camio_mux_select(mux,&which);

        //Acquire a pointer to the new data
        err = camio_read_acquire(which->parent.stream, &rd_buffer);
        if(err){ DBG("Got a read error %i\n", err); return -1; }

        //Get a buffer for writing stuff
        err = camio_write_acquire(stream, &wr_buffer);
        if(err){ DBG("Could not connect to stream\n"); return CAMIO_EINVALID; /*TODO XXX put a better error here*/ }

        //Copy data over from the read buffer to the write buffer
        err = camio_copy_rw(&wr_buffer,&rd_buffer,0,rd_buffer->data_len);
        if(err){ DBG("Got a copy error %i\n", err); return -1; }

        //Data copied, let's commit it and send it off
        err = camio_write_commit(stream, &wr_buffer );
        if(err){ DBG("Got a commit error %i\n", err); return -1; }

        //Done with the write buffer, release it
        camio_write_release(stream,&wr_buffer);

        //And we're done with the read buffer too, release it as well
        err = camio_read_release(stream, &rd_buffer);
        if(err){ DBG("Got a read release error %i\n", err); return -1; }
    }

    //Done with the write buffer too


    return 0;
}

{'fraction_non_alphanumeric': 0.11077844311377245, 'fraction_numerical': 0.010379241516966068, 'mean_word_length': 2.839846743295019, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5066081', 'n_tokens_mistral': 1732, 'n_tokens_neox': 1640, 'n_words': 554}