void filestorage::save_string(t_filestore file_type, const std::string &filename, const std::string &data, bool overwrite) { if (filename == "") { throw std::invalid_argument("Fail to open file for write: empty filename"); } fs::path file_with_path; try { file_with_path = prepare_path_for_write(file_type, filename, overwrite); fs::ofstream file(file_with_path, std::ios::out | std::ios::binary); if (file.is_open()) { file << data; } else { throw std::invalid_argument("Fail to open file for write: " + filename); } file.close(); _dbg2("Successfully saved string to:" << file_with_path.native()); } catch(overwrite_error &err) { std::cout << err.what() << std::endl; if(text_ui::ask_user_forpermission("overwrite file?")){ save_string(file_type, filename, data, true); } else { _dbg2("Fail to save string"); } } }
void filestorage::save_string_mlocked(t_filestore file_type, const std::string &filename, const sodiumpp::locked_string &locked_data, bool overwrite) { if (filename == "") { throw std::invalid_argument("Fail to open file for write: empty filename"); } fs::path file_with_path; try { file_with_path = prepare_path_for_write(file_type, filename, overwrite); FILE *f_ptr; f_ptr = std::fopen(file_with_path.c_str(), "w"); // magic 1 is the size in bytes of each element to be written std::fwrite(locked_data.c_str(), 1, locked_data.size(), f_ptr); std::fclose(f_ptr); _dbg2("Successfully saved mlocked string to:" << file_with_path.native()); } catch(overwrite_error &err) { std::cout << err.what() << std::endl; if(text_ui::ask_user_forpermission("overwrite file?")){ save_string_mlocked(file_type, filename, locked_data, true); } else { _dbg2("Fail to save mlocked string"); } } }
void connection_basic::sleep_before_packet(size_t packet_size, int phase, int q_len) { double delay=0; // will be calculated do { // rate limiting if (m_was_shutdown) { _dbg2("m_was_shutdown - so abort sleep"); return; } { CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_out ); delay = network_throttle_manager::get_global_throttle_out().get_sleep_time_after_tick( packet_size ); } delay *= 0.50; if (delay > 0) { long int ms = (long int)(delay * 1000); MTRACE("Sleeping in " << __FUNCTION__ << " for " << ms << " ms before packet_size="<<packet_size); // debug sleep boost::this_thread::sleep(boost::posix_time::milliseconds( ms ) ); } } while(delay > 0); // XXX LATER XXX { CRITICAL_REGION_LOCAL( network_throttle_manager::m_lock_get_global_throttle_out ); network_throttle_manager::get_global_throttle_out().handle_trafic_exact( packet_size ); // increase counter - global } }
sodiumpp::locked_string filestorage::load_string_mlocked(t_filestore file_type, const std::string &filename) { FILE * f_ptr; fs::path file_with_path = get_full_path(file_type, filename); f_ptr = std::fopen(file_with_path.c_str(), "r"); if (f_ptr == NULL){ throw std::invalid_argument("Fail to open mlocked file for read: " + filename); } std::fseek(f_ptr, 0L, SEEK_END); size_t content_size = static_cast<size_t>(ftell(f_ptr)); std::rewind(f_ptr); sodiumpp::locked_string content(content_size); size_t byte_read = std::fread(content.buffer_writable(), 1, content_size, f_ptr); if (byte_read != content_size) { std::string err_msg = "Fail to read all content of file: " + filename; err_msg += " read: [" + std::to_string(byte_read); err_msg += "] bytes of [" + std::to_string(content_size) + "]"; throw std::invalid_argument(err_msg); } std::fclose(f_ptr); _dbg2("Successfully loaded mlocked string from:" << file_with_path.native()); return content; }
std::string filestorage::load_string(t_filestore file_type, const std::string &filename) { std::string content; fs::path file_with_path = get_full_path(file_type, filename); _dbg2("Loading file path: " << file_with_path.native()); if (!is_file_ok(file_with_path.native())) { throw std::invalid_argument("Fail to open file for read: " + filename); } else { fs::ifstream ifs(file_with_path); content.assign( (std::istreambuf_iterator<char>(ifs) ), (std::istreambuf_iterator<char>() ) ); ifs.close(); } _dbg2("Successfully loaded string from:" << file_with_path.native()); return content; }
void c_osi2_switch::send_hello_to_neighbors() { _dbg2("send test hello packet to all neighbors"); /// send test hello packet to all neighbors for (auto &nic : m_nic) { t_osi2_cost cost; c_osi2_nic * remote_nic = nic->get_connected_card_or_null(cost); if (remote_nic == nullptr) continue; t_osi3_uuid dest_addr = remote_nic->get_uuid(); /// addres of my neighbor // TODO: send it here. _NOTREADY_warn(); } }
void c_osi2_switch::connect_with(c_osi2_nic &target, c_world &world, t_osi2_cost cost) { create_nic(); // create a new NIC card (it will be at end) c_osi2_nic & my_new_port = * m_nic.back(); // get this new card // create the cable (it will be owned by the networld world) that connects this target to my new port c_osi2_cable_direct & cable = world.new_cable_between( target , my_new_port , cost ); // actually plug in the created table to both ends: my_new_port.plug_in_cable(cable); target.plug_in_cable(cable); // as result, the target NIC has access to our NIC and to us, and vice-versa _dbg2("In " << world << " connected the target " << target << " to my port " << my_new_port ); }