/** * Transmits each row found in the database * @param userp is a pointer to a transmited_t * structure that must contain * a comm_t * pointer and a db_t * pointer. * @param nb_col gives the number of columns in this row. * @param data contains the data of each column. * @param name_col contains the name of each column. * @returns always 0. */ static int transmit_callback(void *userp, int nb_col, char **data, char **name_col) { transmited_t *trans = (transmited_t *) userp; gchar *sql_command = NULL; gint success = 0; if (trans != NULL && data != NULL && trans->comm != NULL && trans->database != NULL) { trans->comm->readbuffer = data[2]; /** data[2] is the data column in buffers table of the database */ success = post_url(trans->comm, data[1]); /** data[1] is the url column in buffers table of the database */ if (success == CURLE_OK) { sql_begin(trans->database); sql_command = g_strdup_printf("INSERT INTO transmited (buffer_id) VALUES ('%s');", data[0]); exec_sql_cmd(trans->database, sql_command, _("(%d) Error while inserting into the table 'transmited': %s\n")); free_variable(sql_command); sql_commit(trans->database); } /** @todo use the result of post to be able to manage errors */ } return 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; }
// https://wiki.london.hackspace.org.uk/view/Project:Tool_Access_Control/Solexious_Proposal#Report_tool_status int setToolStatus(int status, Card card) { int ret = -1; Serial.println("Setting tool status:"); // /[nodeID]/status/[new_status]/by/[cardWithAdminPermissions] char url[64]; sprintf(url, "/%d/status/%d/by/", acsettings.nodeid, status); card.str(url + strlen(url)); ret = post_url(url); return ret; }
// https://wiki.london.hackspace.org.uk/view/Project:Tool_Access_Control/Solexious_Proposal#Tool_usage_.28live.29 // status: 1 == in use, 0 == out of use. int reportToolUse(Card card, int status) { int ret = -1; Serial.println("Setting tool usage:"); // /[nodeID]/tooluse/[status]/[cardID] char url[64]; sprintf(url, "/%d/tooluse/%d/", acsettings.nodeid, status); card.str(url + strlen(url)); ret = post_url(url); return ret; }
// https://wiki.london.hackspace.org.uk/view/Project:Tool_Access_Control/Solexious_Proposal#Tool_usage_.28usage_time.29 // Time reported is in SECONDS. int toolUseTime(Card card, int time) { int ret = -1; Serial.println("Setting tool status:"); // /[nodeID]/tooluse/time/for/[cardID]/[timeUsed] char url[64]; sprintf(url, "/%d/tooluse/time/for/", acsettings.nodeid); card.str(url + strlen(url)); sprintf(url + strlen(url), "/%d", time); ret = post_url(url); return ret; }
// https://wiki.london.hackspace.org.uk/view/Project:Tool_Access_Control/Solexious_Proposal#Add_card void addNewUser(Card card, Card maintainer) { char url[64]; Serial.println("Adding card:"); // /<nodeid>/grant-to-card/<trainee card uid>/by-card/<maintainer card uid> sprintf(url, "/%d/grant-to-card/", acsettings.nodeid); card.str(url + strlen(url)); sprintf(url + strlen(url), "/by-card/"); maintainer.str(url + strlen(url)); post_url(url); }
/** * 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; }
static void *url_worker(void *arg){ url_data *data = arg; buf_data *bdata = malloc(sizeof(buf_data)); bdata->offset = 0; bdata->size = 0; bdata->buf = NULL; bdata->postData = data->postData; data->data = bdata; if(data->httpType == CURL_HTTP_GET){ //printf("%s\n", data->url); data->npcode = get_url(data->url, bdata, g_hash_table_lookup(g_FRManager.hash_table, data->instance)); }else{ data->npcode = post_url(data->url, bdata, g_hash_table_lookup(g_FRManager.hash_table, data->instance)); } //TODO gdk_threads_add_idle(stream_write, arg); return NULL; }
/** * 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; }
/** * Sends data as requested by the server 'cdpfglserver'. * @param main_struct : main structure of the program. * @param hash_data_list : list of hash_data_t * pointers containing * all the data to be saved. * @param answer is the request sent back by server when we had send * meta data. * @note using directly main_struct->comm->buffer -> not threadable as is. */ static GList *send_data_to_server(main_struct_t *main_struct, GList *hash_data_list, gchar *answer) { json_t *root = NULL; GList *hash_list = NULL; /** hash_list is local to this function */ GList *head = NULL; gint success = CURLE_FAILED_INIT; GList *iter = NULL; hash_data_t *found = NULL; hash_data_t *hash_data = NULL; if (main_struct != NULL && main_struct->comm != NULL && answer != NULL && hash_data_list!= NULL) { root = load_json(answer); if (root != NULL) { /* This hash_list is the needed hashs from server */ hash_list = extract_glist_from_array(root, "hash_list", TRUE); json_decref(root); head = hash_list; while (hash_list != NULL) { hash_data = hash_list->data; /* hash_data_list contains all hashs and their associated data */ iter = find_hash_in_list(hash_data_list, hash_data->hash); found = iter->data; /* readbuffer is the buffer sent to server */ main_struct->comm->readbuffer = convert_hash_data_t_to_string(found); success = post_url(main_struct->comm, "/Data.json"); if (success != CURLE_OK) { db_save_buffer(main_struct->database, "/Data.json", main_struct->comm->readbuffer); } free_variable(main_struct->comm->readbuffer); hash_data_list = g_list_remove_link(hash_data_list, iter); /* iter is now a single element list and we can delete * data in this element and then remove this single element list */ g_list_free_full(iter, free_hdt_struct); main_struct->comm->buffer = free_variable(main_struct->comm->buffer); hash_list = g_list_next(hash_list); } if (head != NULL) { g_list_free_full(head, free_hdt_struct); } } else { print_error(__FILE__, __LINE__, _("Error while loading JSON answer from server\n")); } } return hash_data_list; }