void IPC::send_message(string recipient, string message_head, string message_body, bool do_log) { static int lock_file_count = 0; string lock_file_name = "lock_" + self_name + to_string(lock_file_count); write_string_to_file(ipc_path + slash + lock_file_name, ""); ++lock_file_count; vector<string> file_name_vec = list_files_in_directory(ipc_path); bool found = true; int file_count = 0; while (found) { found = false; for (string file_name_current : file_name_vec) if (file_name_current == recipient + to_string(file_count)) { found = true; ++file_count; break; } } static int sent_count = 0; string path_new = ipc_path + slash + recipient + to_string(file_count); write_string_to_file(path_new, message_head + "!" + message_body); ++sent_count; delete_file(ipc_path + slash + lock_file_name); // if (do_log) // console_log("message sent: " + recipient + " " + message_head + " " + message_body, false); }
void IPC::send_message(const string recipient, const string message_head, const string message_body) { vector<string> file_name_vec = list_files_in_directory(ipc_path); bool found = true; int file_count = 0; while (found) { found = false; for (string file_name_current : file_name_vec) if (file_name_current == recipient + to_string(file_count)) { found = true; ++file_count; break; } } static int sent_count = 0; const string path_old = ipc_path + "\\s" + self_name + to_string(sent_count); const string path_new = ipc_path + "\\" + recipient + to_string(file_count); write_string_to_file(path_old, message_head + "!" + message_body); rename_file(path_old, path_new); ++sent_count; COUT << "message sent: " << recipient << " " << message_head << " " << message_body << endl; }
void IPC::clear() { vector<string> file_name_vec = list_files_in_directory(ipc_path); for (string file_name_current : file_name_vec) { string file_name_everyone = ""; if (file_name_current.size() >= 8) file_name_everyone = file_name_current.substr(0, 8); if (file_name_current.size() > self_name.size() || file_name_everyone == "everyone") { string file_name = ""; string file_name_id_str = ""; if (file_name_everyone != "everyone") { file_name = file_name_current.substr(0, self_name.size()); file_name_id_str = file_name_current.substr(self_name.size(), file_name_current.size()); } else continue; if (file_name == self_name || file_name_everyone == "everyone") delete_file(ipc_path + slash + file_name_current); } } }
void IPC::open_udp_channel(const string recipient, const int port_num) { udp_map[recipient] = &(udp_pool[udp_pool_index]); ++udp_pool_index; UDP* udp_ptr = udp_map[recipient]; bool file_found = false; vector<string> file_name_vec = list_files_in_directory(ipc_path); for (string file_name_current : file_name_vec) if (file_name_current == "udp_port") file_found = true; if (!file_found) { if (port_num == -1) { int port_old = 0; int port_new = 0; int* port_new_ptr = &port_new; get_response(recipient, "open udp channel", "", [udp_ptr, port_new_ptr](const string message_body) { const int port = atoi(message_body.c_str()); udp_ptr->set_port(port); *port_new_ptr = port; COUT << "udp port is " << port << endl; }); while (port_old == port_new) { Sleep(20); update(); } } else { send_message(recipient, "open udp channel", to_string(port_num)); const int port = port_num; udp_ptr->set_port(port); COUT << "udp port is " << port << endl; } } else { int port = atoi(read_text_file(ipc_path + "\\udp_port")[0].c_str()); udp_ptr->set_port(port); COUT << "udp port is " << port << endl; } }
void IPC::run_js(vector<string> lines) { string recipient = "menu_plus"; string message_head = "//evaluate javascript"; string message_body = ""; for(string& line : lines) message_body += line + "\n"; static int lock_file_count = 0; string lock_file_name = "lock_" + self_name + to_string(lock_file_count); write_string_to_file(ipc_path + slash + lock_file_name, ""); ++lock_file_count; vector<string> file_name_vec = list_files_in_directory(ipc_path); bool found = true; int file_count = 0; while (found) { found = false; for (string file_name_current : file_name_vec) if (file_name_current == recipient + to_string(file_count)) { found = true; ++file_count; break; } } static int sent_count = 0; string path_new = ipc_path + slash + recipient + to_string(file_count); message_body = "//" + path_new + "\n" + message_body; write_string_to_file(path_new, message_head + "!" + message_body); ++sent_count; delete_file(ipc_path + slash + lock_file_name); // console_log("message sent: " + recipient + " " + message_head + " " + message_body, false); }
void IPC::update() { static bool updated = true; if (!updated) return; updated = false; static unordered_map<string, bool> file_name_processed_map; vector<string> file_name_vec = list_files_in_directory(ipc_path); for (string file_name_current : file_name_vec) { string file_name_lock = ""; if (file_name_current.size() >= 4) file_name_lock = file_name_current.substr(0, 4); if (file_name_lock == "lock") { updated = true; return; } } for (string file_name_current : file_name_vec) { string file_name_everyone = ""; if (file_name_current.size() >= 8) file_name_everyone = file_name_current.substr(0, 8); if (file_name_current.size() > self_name.size() || file_name_everyone == "everyone") { if (file_name_processed_map[file_name_current] == true) continue; else file_name_processed_map[file_name_current] = true; string file_name = ""; string file_name_id_str = ""; if (file_name_everyone != "everyone") { file_name = file_name_current.substr(0, self_name.size()); file_name_id_str = file_name_current.substr(self_name.size(), file_name_current.size()); } if (file_name == self_name || file_name_everyone == "everyone") { Sleep(20); vector<string> lines = read_text_file(ipc_path + slash + file_name_current); // delete_file(ipc_path + slash + file_name_current); vector<string> message_vec = split_string(lines[0], "!"); string message_head = message_vec[0]; string message_body = message_vec[1]; // console_log("message_received " + message_head + " " + message_body + " " + file_name_current, false); if (response_map.count(message_head) == 0) { if (command_map.count(message_head)) command_map[message_head](message_body); } else { function<void (string)> func = response_map[message_head]; response_map.erase(message_head); func(message_body); } } } } updated = true; }
void delete_all_files(const string path) { vector<string> file_name_vec = list_files_in_directory(path); for (string file_name : file_name_vec) delete_file(path + slash + file_name); }
int check_process_running(string name, bool partial) { #ifdef _WIN32 int process_count = 0; HANDLE SnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if(SnapShot == INVALID_HANDLE_VALUE) return false; PROCESSENTRY32 procEntry; procEntry.dwSize = sizeof(PROCESSENTRY32); if(!Process32First(SnapShot, &procEntry)) return false; do { if(strcmp(procEntry.szExeFile, name.c_str()) == 0) ++process_count; } while (Process32Next(SnapShot, &procEntry)); return process_count; #elif __APPLE__ vector<string> file_name_vec = list_files_in_directory(executable_path); for (string file_name_current : file_name_vec) if (file_name_current == "lock") return true; write_string_to_file(executable_path + "/lock", ""); string command = "ps -e > " + executable_path + "/processes.txt"; system(command.c_str()); vector<string> lines = read_text_file(executable_path + "/processes.txt"); delete_file(executable_path + "/processes.txt"); for (string& str : lines) if (partial) { if (str.find(name) != std::string::npos) { delete_file(executable_path + "/lock"); return true; } } else { vector<string> str_parts = split_string(str, "/"); if (str_parts.size() > 1) { string process_name = str_parts[str_parts.size() - 1]; if (process_name == name) { delete_file(executable_path + "/lock"); return true; } } } delete_file(executable_path + "/lock"); return false; #endif }
void save(const string name) { const string extension = "nrocinunerrad"; vector<string> file_name_vec = list_files_in_directory(pose_database_path); int name_count = 0; for (string& name_current : file_name_vec) { vector<string> name_extension_vec = split_string(name_current, "."); if (name_extension_vec.size() <= 1) continue; if (name_extension_vec[1] != extension) continue; string name_without_num = ""; for (char& c : name_current) { string char_str = ""; char_str += c; if (char_str != "0" && atoi(char_str.c_str()) == 0) name_without_num += c; else break; } if (name_without_num == name) { string num_without_name = ""; for (char& c : name_current) { string char_str = ""; char_str += c; if (char_str == "0" || atoi(char_str.c_str()) != 0) num_without_name += c; else continue; } int num = atoi(num_without_name.c_str()); if (num > name_count) name_count = num; } } name_count += 1; string data = ""; for (Point& pt : points_current) data += to_string(pt.x) + "!" + to_string(pt.y) + "\n"; data.pop_back(); const string path = pose_database_path + slash + name + to_string(name_count) + "." + extension; write_string_to_file(path, data); points_collection.push_back(points_current); names_collection.push_back(name); cout << "pose data saved: " + name << endl; }
void load() { vector<string> file_name_vec = list_files_in_directory(pose_database_path); for (string& name_current : file_name_vec) { string name_without_num = ""; for(char& c : name_current) { string char_str = ""; char_str += c; if (char_str != "0" && atoi(char_str.c_str()) == 0) name_without_num += c; else break; } vector<string> name_extension_vec = split_string(name_current, "."); if (name_extension_vec.size() > 1 && name_extension_vec[1] == "nrocinunerrad") { const string path = pose_database_path + slash + name_current; vector<string> data = read_text_file(path); vector<Point> points; for (string& str : data) { vector<string> str_pair = split_string(str, "!"); int x = atoi(str_pair[0].c_str()); int y = atoi(str_pair[1].c_str()); points.push_back(Point(x, y)); } string pose_name_loaded = name_extension_vec[0]; int num_count = 0; for (char& c : pose_name_loaded) { string char_str = ""; char_str += c; if (atoi(char_str.c_str()) != 0 || char_str == "0") ++num_count; } for (int i = 0; i < num_count; ++i) pose_name_loaded.pop_back(); points_collection.push_back(points); names_collection.push_back(pose_name_loaded); const string label_path = pose_database_path + slash + "labels" + slash + name_current; vector<string> label_data = read_text_file(label_path); vector<Point> labels; for (String& str : label_data) { vector<string> str_pair = split_string(str, "!"); labels.push_back(Point(atoi(str_pair[0].c_str()), atoi(str_pair[1].c_str()))); } labels_collection.push_back(labels); } else if (name_extension_vec.size() > 1 && name_extension_vec[1] == "png") { const string path = pose_database_path + slash + name_current; Mat image_loaded = imread(path); } } }