Пример #1
0
/**
 * Inserts the array into a root json_t * structure and dumps it into a
 * buffer that is send to the server and then freed.
 * @param main_struct : main structure of the program.
 * @param array is the json_t * array to be sent to the server
 */
static gint insert_array_in_root_and_send(main_struct_t *main_struct, json_t *array)
{
    json_t *root = NULL;
    gint success = CURLE_FAILED_INIT;


    if (main_struct != NULL && main_struct->comm != NULL && array != NULL)
    {

        root = json_object();
        insert_json_value_into_json_root(root, "data_array", array);

        /* readbuffer is the buffer sent to server */
        main_struct->comm->readbuffer = json_dumps(root, 0);

        success = post_url(main_struct->comm, "/Data_Array.json");

        if (success != CURLE_OK)
        {
            db_save_buffer(main_struct->database, "/Data_Array.json", main_struct->comm->readbuffer);
        }

        free_variable(main_struct->comm->readbuffer);
        json_decref(root);
        main_struct->comm->buffer = free_variable(main_struct->comm->buffer);

    }

    return success;
}
Пример #2
0
/**
 * Answers /Hash_Array.json POST request by answering to the client needed
 * hashs
 * @param server_struct is the main structure for the server.
 * @param connection is the connection in MHD
 * @param received_data is a gchar * string to the data that was received
 *        by the POST request.
 */
static int answer_hash_array_post_request(server_struct_t *server_struct, struct MHD_Connection *connection, gchar *received_data)
{
    gchar *answer = NULL;         /** gchar *answer : Do not free answer variable as MHD will do it for us !  */
    json_t *root = NULL;          /** json_t *root is the root that will contain all meta data json formatted */
    json_t *array = NULL;         /** json_t *array is the array that will receive base64 encoded hashs       */
    GList *hash_data_list = NULL;


    if (server_struct != NULL && connection != NULL && received_data != NULL)
        {

            root = load_json(received_data);
            hash_data_list = extract_glist_from_array(root, "hash_list", TRUE);
            json_decref(root);

            print_debug(_("Received hash array of %zd bytes size\n"), strlen(received_data));

            array = find_needed_hashs(server_struct, hash_data_list);

            root = json_object();
            insert_json_value_into_json_root(root, "hash_list", array);
            answer = json_dumps(root, 0);
            json_decref(root);
            g_list_free_full(hash_data_list, free_hdt_struct);
        }
    else
        {
            answer = g_strdup_printf(_("Error: could not convert json to metadata\n"));
        }

    return create_MHD_response(connection, answer);
}
Пример #3
0
/**
 * Inserts an integer value into a json…t *root array
 * @param[in,out] root is the root that will contain all meta data values
 * @param keyname is the keyname associated with the value (in fact it is
 *        variable's name)
 * @param value is the integer value to insert into root
 */
void insert_integer_value_into_json_root(json_t *root, gchar *keyname, guint64 value)
{
    json_t *nbr = NULL;

    nbr = json_integer(value);
    insert_json_value_into_json_root(root, keyname, nbr);
}
Пример #4
0
/**
 * Sends meta data to the server and returns it's answer or NULL in
 * case of an error.
 * @param main_struct : main structure of the program (contains pointers
 *        to the communication socket.
 * @param meta : the meta_data_t * structure to be saved.
 * @returns a newly allocated gchar * string that may be freed when no
 *          longer needed.
 */
static gchar *send_meta_data_to_server(main_struct_t *main_struct, meta_data_t *meta, gboolean data_sent)
{
    gchar *json_str = NULL;
    gchar *answer = NULL;
    gint success = CURLE_FAILED_INIT;
    json_t *root = NULL;
    json_t *array = NULL;

    if (main_struct != NULL && meta != NULL && main_struct->hostname != NULL)
    {
        json_str = convert_meta_data_to_json_string(meta, main_struct->hostname, data_sent);

        /* Sends meta data here: readbuffer is the buffer sent to server */
        print_debug(_("Sending meta data: %s\n"), json_str);
        main_struct->comm->readbuffer = json_str;
        success = post_url(main_struct->comm, "/Meta.json");

        if (success == CURLE_OK)
        {
            answer = g_strdup(main_struct->comm->buffer);
            main_struct->comm->buffer = free_variable(main_struct->comm->buffer);
        }
        else
        {
            /* Need to manage HTTP errors ? */
            /* Saving meta data that should have been sent to sqlite database */
            db_save_buffer(main_struct->database, "/Meta.json", main_struct->comm->readbuffer);

            /* An error occured -> we need the whole hash list to be saved
             * we are building a 'fake' answer with the whole hash list.
             */
            array = convert_hash_list_to_json(meta->hash_data_list);
            root = json_object();
            insert_json_value_into_json_root(root, "hash_list", array);
            answer = json_dumps(root, 0);
            json_decref(root);
        }

        free_variable(main_struct->comm->readbuffer);
    }

    return answer;
}
Пример #5
0
/**
 * Answers /Meta.json POST request by storing data and answering to the
 * client.
 * @param server_struct is the main structure for the server.
 * @param connection is the connection in MHD
 * @param received_data is a gchar * string to the data that was received
 *        by the POST request.
 */
static int answer_meta_json_post_request(server_struct_t *server_struct, struct MHD_Connection *connection, gchar *received_data)
{
    server_meta_data_t *smeta = NULL; /** server_meta_data_t *smeta stores meta data along with hostname of the client */
    gchar *answer = NULL;             /** gchar *answer : Do not free answer variable as MHD will do it for us !       */
    json_t *root = NULL;              /** json_t *root is the root that will contain all meta data json formatted      */
    json_t *array = NULL;             /** json_t *array is the array that will receive base64 encoded hashs            */

    smeta = convert_json_to_smeta_data(received_data);

    if (smeta != NULL && smeta->meta != NULL)
        {   /* The convertion went well and smeta contains the meta data */

            print_debug(_("Received meta data (%zd bytes) for file %s\n"), strlen(received_data), smeta->meta->name);

            if (smeta->data_sent == FALSE)
                {
                    array = find_needed_hashs(server_struct, smeta->meta->hash_data_list);
                }
            else
                {
                    array = json_array();
                }

            root = json_object();
            insert_json_value_into_json_root(root, "hash_list", array);
            answer = json_dumps(root, 0);
            json_decref(root);


            /**
             * Sending smeta data into the queue in order to be treated by
             * the corresponding thread. smeta is freed there and should not
             * be used after this "call" here.
             */
            g_async_queue_push(server_struct->meta_queue, smeta);
        }
    else
        {
            answer = g_strdup_printf(_("Error: could not convert json to metadata\n"));
        }

    return create_MHD_response(connection, answer);
}
Пример #6
0
/**
 * Makes an array with the hashs in the list and sends them to
 * /Hash_Array.json URL of the server. The server must answer with a list
 * of needed hashs
 * @param comm a comm_t * structure that must contain an initialized
 *        curl_handle (must not be NULL)
 * @param hash_data_list : list of hash_data already processed that are
 *        ready to be transmited to server (if needed)
 * @returns a gchar * containing the JSON array of needed hashs
 */
static gchar *send_hash_array_to_server(comm_t *comm, GList *hash_data_list)
{
    json_t *array = NULL;
    json_t *root = NULL;
    gchar *whole_hash_list = NULL;
    gchar *answer = NULL;
    gboolean success = CURLE_FAILED_INIT;

    if (comm != NULL && hash_data_list != NULL)
    {
        array = convert_hash_list_to_json(hash_data_list);
        root = json_object();
        insert_json_value_into_json_root(root, "hash_list", array);

        whole_hash_list = json_dumps(root, 0);
        json_decref(root);
        comm->readbuffer = whole_hash_list;
        print_debug("Hash_Array: %s\n", whole_hash_list);


        success = post_url(comm, "/Hash_Array.json");

        if (success == CURLE_OK)
        {
            answer = g_strdup(comm->buffer);
            comm->buffer = free_variable(comm->buffer);
        }
        else
        {
            answer = g_strdup(whole_hash_list);
        }

        free_variable(comm->readbuffer);
    }

    return answer;
}