/****************************************************************************** * * * Function: get_data_from_server * * * * Purpose: get configuration and other data from server * * * * Parameters: * * * * Return value: SUCCESS - processed successfully * * FAIL - an error occurred * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ int get_data_from_server(zbx_sock_t *sock, const char *request) { const char *__function_name = "get_data_from_server"; int ret = FAIL; struct zbx_json j; zabbix_log(LOG_LEVEL_DEBUG, "In %s() request:'%s'", __function_name, request); zbx_json_init(&j, 128); zbx_json_addstring(&j, "request", request, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "host", CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); if (FAIL == send_data_to_server(sock, j.buffer)) goto exit; if (FAIL == recv_data_from_server(sock)) goto exit; ret = SUCCEED; exit: zbx_json_free(&j); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: put_data_to_server * * * * Purpose: send data to server * * * * Parameters: * * * * Return value: SUCCESS - processed successfully * * FAIL - an error occurred * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ int put_data_to_server(zbx_sock_t *sock, struct zbx_json *j, char **error) { const char *__function_name = "put_data_to_server"; char *info = NULL, *err = NULL; int ret = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In %s() datalen:" ZBX_FS_SIZE_T, __function_name, (zbx_fs_size_t)j->buffer_size); if (SUCCEED != send_data_to_server(sock, j->buffer)) goto out; if (SUCCEED != zbx_recv_response(sock, &info, 0, &err)) { *error = zbx_dsprintf(*error, "error:\"%s\", info:\"%s\"", ZBX_NULL2EMPTY_STR(err), ZBX_NULL2EMPTY_STR(info)); goto out; } ret = SUCCEED; out: zbx_free(info); zbx_free(err); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: put_data_to_server * * * * Purpose: send data from server * * * * Parameters: * * * * Return value: SUCCESS - processed succesfully * * FAIL - an error occured * * * * Author: Alksander Vladishev * * * * Comments: * * * ******************************************************************************/ int put_data_to_server(zbx_sock_t *sock, struct zbx_json *j) { struct zbx_json_parse jp; int ret = FAIL; char *answer, value[MAX_STRING_LEN]; zabbix_log(LOG_LEVEL_DEBUG, "In put_data_to_server() [datalen:%zd]", j->buffer_size); if (FAIL == send_data_to_server(sock, j->buffer)) goto exit; if (FAIL == recv_data_from_server(sock, &answer)) goto exit; if (FAIL == zbx_json_open(answer, &jp)) goto exit; if (FAIL == zbx_json_value_by_name(&jp, ZBX_PROTO_TAG_RESPONSE, value, sizeof(value))) goto exit; if (0 != strcmp(value, ZBX_PROTO_VALUE_SUCCESS)) goto exit; ret = SUCCEED; exit: zabbix_log(LOG_LEVEL_DEBUG, "End of put_data_to_server():%s", zbx_result_string(ret)); return ret; }
/** * Process the file that is not already in our local cache * @param main_struct : main structure of the program * @param meta is the meta data of the file to be processed (it does * not contain any hashs at that point). */ static void process_small_file_not_in_cache(main_struct_t *main_struct, meta_data_t *meta) { GFile *a_file = NULL; gchar *answer = NULL; gint success = 0; /** success returns a CURL Error status such as CURLE_OK for instance */ a_clock_t *mesure_time = NULL; if (main_struct != NULL && main_struct->opt != NULL && meta != NULL) { print_debug(_("Processing small file: %s\n"), meta->name); if (meta->file_type == G_FILE_TYPE_REGULAR) { mesure_time = new_clock_t(); /* Calculates hashs and takes care of data */ a_file = g_file_new_for_path(meta->name); meta->hash_data_list = calculate_hash_data_list_for_file(a_file, meta->blocksize); a_file = free_object(a_file); end_clock(mesure_time, "calculate_hash_data_list"); } mesure_time = new_clock_t(); answer = send_meta_data_to_server(main_struct, meta, FALSE); end_clock(mesure_time, "send_meta_data_to_server"); mesure_time = new_clock_t(); if (meta->size < meta->blocksize) { /* Only one block to send (size is less than blocksize's value) */ meta->hash_data_list = send_data_to_server(main_struct, meta->hash_data_list, answer); } else { /* A least 2 blocks to send */ meta->hash_data_list = send_all_data_to_server(main_struct, meta->hash_data_list, answer); } end_clock(mesure_time, "send_(all)_data_to_server"); free_variable(answer); /* Not used by now */ if (success == CURLE_OK) { /* Everything has been transmitted so we can save meta data into the local db cache */ /* This is usefull for file carving to avoid sending too much things to the server */ mesure_time = new_clock_t(); db_save_meta_data(main_struct->database, meta, TRUE); end_clock(mesure_time, "db_save_meta_data"); } } }
__interrupt void Timer_A (void) { timer_counter++; // if water is flowing, increase flowing time if(is_flowing == 1){ is_flowing = 0; flowing_time++; } // If water flow has stopped else{ // Calculate flow rate flow_rate = (float)pulse_counter / flowing_time; /* * 7Hz -> 60 second -> 1 Litre * 1Hz -> 01 second -> 1 / (7 * 60) * flow_rate (Hz) -> flowing_time (second) -> (flow_rate * flowing_time) / (7 * 60) Litre */ flow_milli_litres = ((flow_rate * flowing_time) / (calibration_factor * 60)) * 1000; // Calculate total amount of water flowed for a certain period (i.e. for 10 minute) total_flow_milli_litres += flow_milli_litres; // reset values pulse_counter = 0; flowing_time = 0; flow_milli_litres = 0; flow_rate = 0; } // Send data to the server in every 1 min if(timer_counter == 20){ water_low_byte |= total_flow_milli_litres; // get the low byte of the amount of water consumed total_flow_milli_litres >>= 8; water_high_byte |= total_flow_milli_litres; // get the high byte of the amount of water consumed // send data to the server send_data_to_server(); // reset water amount and timer counter total_flow_milli_litres = 0; timer_counter = 0; }
void LocalClient::send_chat_message(const std::string &msg) { if (!connected) { PRINT("chat: not connected to server.\n"); return; } fprintf(stderr, "sending chat message \"%s\"\n", msg.c_str()); static char chat_msg_buffer[PACKET_SIZE_MAX]; int message_len = MIN(msg.length(), PTCL_PACKET_BODY_LEN_MAX); PTCLHEADERDATA CHAT_HEADER = protocol_make_header(client.seq_number, client.info.id, C_CHAT_MESSAGE, message_len); protocol_copy_header(chat_msg_buffer, &CHAT_HEADER); memcpy(chat_msg_buffer + PTCL_HEADER_LENGTH, msg.c_str(), message_len); size_t total_size = PTCL_HEADER_LENGTH + message_len; chat_msg_buffer[PACKET_SIZE_MAX - 1] = '\0'; send_data_to_server(chat_msg_buffer, total_size); }
/****************************************************************************** * * * Function: get_data_from_server * * * * Purpose: get configuration and othed data from server * * * * Parameters: * * * * Return value: SUCCESS - processed succesfully * * FAIL - an error occured * * * * Author: Alksander Vladishev * * * * Comments: * * * ******************************************************************************/ int get_data_from_server(zbx_sock_t *sock, const char *request, char **data) { int ret = FAIL; struct zbx_json j; zabbix_log(LOG_LEVEL_DEBUG, "In get_data_from_server() [request:%s]", request); zbx_json_init(&j, 128); zbx_json_addstring(&j, "request", request, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "host", CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); if (FAIL == send_data_to_server(sock, j.buffer)) goto exit; if (FAIL == recv_data_from_server(sock, data)) goto exit; ret = SUCCEED; exit: zbx_json_free(&j); return ret; }
void LocalClient::ListenTaskThread::post_quit_message() { PTCLHEADERDATA QUIT_HEADER = protocol_make_header( client.seq_number, client.info.id, C_QUIT); protocol_copy_header(buffer, &QUIT_HEADER); send_data_to_server(buffer, PTCL_HEADER_LENGTH); }
void LocalClient::KeystateTaskThread::post_keystate() { PTCLHEADERDATA KEYSTATE_HEADER = { PROTOCOL_ID, client.seq_number, client.info.id, C_KEYSTATE }; KEYSTATE_HEADER.cmd_arg_mask.ch[1] = client.keystate; copy_to_buffer(VAR_SZ(KEYSTATE_HEADER), 0); send_data_to_server(buffer, PTCL_HEADER_LENGTH); }
void LocalClient::PingmanagerTaskThread::ping() { PTCLHEADERDATA PING_HEADER = { PROTOCOL_ID, client.seq_number, client.info.id, C_PING }; copy_to_buffer(VAR_SZ(PING_HEADER), 0); _latest_ping_seq_number = client.seq_number; send_data_to_server(buffer, PTCL_HEADER_LENGTH); }
int LocalClient::handshake() { char handshake_buffer[PACKET_SIZE_MAX]; PTCLHEADERDATA HANDSHAKE_HEADER = protocol_make_header(client.seq_number, ID_CLIENT_UNASSIGNED, C_HANDSHAKE); client.info.name = preferred_name; PRINT("name = %s\n", client.info.name.c_str()); protocol_copy_header(handshake_buffer, &HANDSHAKE_HEADER); copy_to_ext_buffer(handshake_buffer, client.info.name.c_str(), client.info.name.length(), PTCL_HEADER_LENGTH); send_data_to_server(handshake_buffer, PTCL_HEADER_LENGTH + client.info.name.length()); #define RETRY_GRANULARITY_MS 1000 #define NUM_RETRIES 3 PRINT("LocalClient::sending handshake to remote.\n"); // FIXME: currently broken when handshaking with localhost :P // the WS2 select() call reports "data available" when sending to 127.0.0.1, despite different port. int received_data = 0; for (int i = 0; i < NUM_RETRIES; ++i && shutdown_requested) { int select_r = socket.wait_for_incoming_data(RETRY_GRANULARITY_MS); if (select_r > 0) { // could be wrong data. should check the data we received and retry if it wasn't the correct stuff received_data = 1; break; } PRINT("Re-sending handshake to remote...\n"); send_data_to_server(handshake_buffer, PTCL_HEADER_LENGTH + client.info.name.length()); } if (!received_data) { PRINT("Handshake timed out.\n"); LocalClient::request_shutdown(); return 0; } struct sockaddr_in from; int bytes = socket.receive_data(handshake_buffer, &from); PTCLHEADERDATA header; protocol_get_header_data(handshake_buffer, &header); if (header.protocol_id != PROTOCOL_ID) { PRINT( "LocalClient: handshake: protocol id mismatch (received %d)\n", header.protocol_id); LocalClient::request_shutdown(); return 0; } const unsigned char &cmd = header.cmd_arg_mask.ch[0]; if (cmd != S_HANDSHAKE_OK) { PRINT("LocalClient: handshake: received cmdbyte != S_HANDSHAKE_OK (%x). Handshake failed.\n", cmd); LocalClient::request_shutdown(); return 0; } // get player id embedded within the packet copy_from_ext_buffer(handshake_buffer, &client.info.id, sizeof(client.info.id), PTCL_HEADER_LENGTH); // get player name received from server client.info.name = std::string(handshake_buffer + PTCL_HEADER_LENGTH + sizeof(client.info.id)); PRINT("Client: received player id %d and name %s from %s. =)\n", client.info.id, client.info.name.c_str(), get_dot_notation_ipv4(&from).c_str()); connected = true; // VarTracker_track_unit(double, LocalClient::latency_ms, "ms"); return 1; }