1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al. 3 * This file is part of Overpass_API. 5 * Overpass_API is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation, either version 3 of the 8 * License, or (at your option) any later version. 10 * Overpass_API is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 15 * You should have received a copy of the GNU Affero General Public License 16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>. 19#include "dispatcher_client.h" 20#include "dispatcher.h" 25#include <sys/socket.h> 36Dispatcher_Client::Dispatcher_Client(const std::string& dispatcher_share_name_) 370 : dispatcher_share_name(dispatcher_share_name_), socket("") 390 signal(SIGPIPE, SIG_IGN); 410 char* db_dir_env = getenv("OVERPASS_DB_DIR"); 47 // open dispatcher_share 480 int dispatcher_shm_fd = shm_open 490 (dispatcher_share_name.c_str(), O_RDWR, S_666); 500 if (dispatcher_shm_fd < 0) 520 (errno, dispatcher_share_name, "Dispatcher_Client::1"); 530 struct stat stat_buf; 540 fstat(dispatcher_shm_fd, &stat_buf); 550 volatile uint8* dispatcher_shm_ptr = (uint8*)mmap( 560 0, stat_buf.st_size, PROT_READ, MAP_SHARED, dispatcher_shm_fd, 0); 580 if (dispatcher_shm_ptr) 60 // get db_dir and shadow_name 610 db_dir = std::string((const char *)(dispatcher_shm_ptr + 4*sizeof(uint32)), 620 *(uint32*)(dispatcher_shm_ptr + 3*sizeof(uint32))); 630 shadow_name = std::string((const char *)(dispatcher_shm_ptr + 5*sizeof(uint32) 640 + db_dir.size()), *(uint32*)(dispatcher_shm_ptr + db_dir.size() + 4*sizeof(uint32))); 660 munmap((void*)dispatcher_shm_ptr, 670 Dispatcher::SHM_SIZE + db_dir.size() + shadow_name.size()); 690 close(dispatcher_shm_fd); 720 shadow_name = db_dir + (dispatcher_share_name.size() >= 8 730 && dispatcher_share_name.substr(dispatcher_share_name.size()-8) == "osm_base" 740 ? "/osm_base_shadow" : "/areas_shadow"); 760 if (!db_dir.empty() && db_dir.back() != '/') 79 // initialize the socket for the client 800 char* socket_dir_env = getenv("OVERPASS_SOCKET_DIR"); 810 socket.open((socket_dir_env ? std::string(socket_dir_env) : db_dir) + dispatcher_share_name_); 820 std::string socket_name = db_dir + dispatcher_share_name_; 840 pid_t pid = getpid(); 850 if (send(socket.descriptor(), &pid, sizeof(pid_t), 0) == -1) 860 throw File_Error(errno, dispatcher_share_name, "Dispatcher_Client::4"); 90bool file_present(const std::string& full_path) 93673673 q int result = stat(full_path.c_str(), &stat_buf); 98template< class TObject > 99void Dispatcher_Client::send_message(TObject message, const std::string& source_pos) 1010 if (send(socket.descriptor(), &message, sizeof(TObject), 0) == -1) 1020 throw File_Error(errno, dispatcher_share_name, source_pos); 106uint32 Dispatcher_Client::ack_arrived() 1090 int bytes_read = recv(socket.descriptor(), &answer, sizeof(uint32), 0); 1100 while (bytes_read == -1) 1130 bytes_read = recv(socket.descriptor(), &answer, sizeof(uint32), 0); 1150 if (bytes_read == sizeof(uint32)) 122void Dispatcher_Client::write_start() 1240 pid_t pid = getpid(); 1260 send_message(Dispatcher::WRITE_START, "Dispatcher_Client::write_start::socket"); 1320 if (file_exists(shadow_name + ".lock")) 1360 pid_t locked_pid = 0; 1370 std::ifstream lock((shadow_name + ".lock").c_str()); 1390 if (locked_pid == pid) 1440 send_message(Dispatcher::WRITE_START, "Dispatcher_Client::write_start::socket"); 151void Dispatcher_Client::migrate_start() 1530 pid_t pid = getpid(); 1550 send_message(Dispatcher::MIGRATE_START, "Dispatcher_Client::migrate_start::socket"); 1610 if (file_exists(shadow_name + ".lock")) 1650 pid_t locked_pid = 0; 1660 std::ifstream lock((shadow_name + ".lock").c_str()); 1680 if (locked_pid == pid) 1730 send_message(Dispatcher::MIGRATE_START, "Dispatcher_Client::migrate_start::socket"); 180void Dispatcher_Client::write_rollback() 1820 pid_t pid = getpid(); 1840 send_message(Dispatcher::WRITE_ROLLBACK, "Dispatcher_Client::write_rollback::socket"); 1900 if (file_exists(shadow_name + ".lock")) 1940 pid_t locked_pid = 0; 1950 std::ifstream lock((shadow_name + ".lock").c_str()); 1970 if (locked_pid != pid) 2040 send_message(Dispatcher::WRITE_ROLLBACK, "Dispatcher_Client::write_rollback::socket"); 212void Dispatcher_Client::migrate_rollback() 2140 pid_t pid = getpid(); 2160 send_message(Dispatcher::MIGRATE_ROLLBACK, "Dispatcher_Client::migrate_rollback::socket"); 2220 if (file_exists(shadow_name + ".lock")) 2260 pid_t locked_pid = 0; 2270 std::ifstream lock((shadow_name + ".lock").c_str()); 2290 if (locked_pid != pid) 2360 send_message(Dispatcher::MIGRATE_ROLLBACK, "Dispatcher_Client::migrate_rollback::socket"); 244void Dispatcher_Client::write_commit() 2460 pid_t pid = getpid(); 2480 send_message(Dispatcher::WRITE_COMMIT, "Dispatcher_Client::write_commit::socket"); 2550 if (file_exists(shadow_name + ".lock")) 2590 pid_t locked_pid = 0; 2600 std::ifstream lock((shadow_name + ".lock").c_str()); 2620 if (locked_pid != pid) 2710 send_message(Dispatcher::WRITE_COMMIT, "Dispatcher_Client::write_commit::socket"); 277void Dispatcher_Client::migrate_commit() 2790 pid_t pid = getpid(); 2810 send_message(Dispatcher::MIGRATE_COMMIT, "Dispatcher_Client::migrate_commit::socket"); 2880 if (file_exists(shadow_name + ".lock")) 2920 pid_t locked_pid = 0; 2930 std::ifstream lock((shadow_name + ".lock").c_str()); 2950 if (locked_pid != pid) 3040 send_message(Dispatcher::MIGRATE_COMMIT, "Dispatcher_Client::migrate_commit::socket"); 310void Dispatcher_Client::request_read_and_idx( 311 uint32 max_allowed_time, uint64 max_allowed_space, uint32 client_token, uint64 request_full_hash) 3150 while (ack == 0 && ++counter <= 100) 3170 send_message(Dispatcher::REQUEST_READ_AND_IDX, 3180 "Dispatcher_Client::request_read_and_idx::socket::1"); 3190 send_message(max_allowed_time, "Dispatcher_Client::request_read_and_idx::socket::2"); 3200 send_message(max_allowed_space, "Dispatcher_Client::request_read_and_idx::socket::3"); 3210 send_message(client_token, "Dispatcher_Client::request_read_and_idx::socket::4"); 3220 send_message(request_full_hash, "Dispatcher_Client::request_read_and_idx::socket::5"); 3240 ack = ack_arrived(); 3250 if (ack == Dispatcher::REQUEST_READ_AND_IDX) 3300 if (ack == Dispatcher::RATE_LIMITED) 3310 throw File_Error(0, dispatcher_share_name, "Dispatcher_Client::request_read_and_idx::rate_limited"); 3320 else if (ack == Dispatcher::QUERY_REJECTED || ack == 0) 3330 throw File_Error(0, dispatcher_share_name, "Dispatcher_Client::request_read_and_idx::timeout"); 3340 else if (ack == Dispatcher::DUPLICATE_QUERY) 3350 throw File_Error(0, dispatcher_share_name, "Dispatcher_Client::request_read_and_idx::duplicate_query"); 3370 throw File_Error(0, dispatcher_share_name, "Dispatcher_Client::request_read_and_idx::protocol_error"); 341void Dispatcher_Client::read_idx_finished() 3440 while (++counter <= 300) 3460 send_message(Dispatcher::READ_IDX_FINISHED, "Dispatcher_Client::read_idx_finished::socket"); 3510 throw File_Error(0, dispatcher_share_name, "Dispatcher_Client::read_idx_finished::timeout"); 355void Dispatcher_Client::read_finished() 3580 while (++counter <= 300) 3600 send_message(Dispatcher::READ_FINISHED, "Dispatcher_Client::read_finished::socket"); 3650 throw File_Error(0, dispatcher_share_name, "Dispatcher_Client::read_finished::timeout"); 369void Dispatcher_Client::purge(uint32 pid) 3730 send_message(Dispatcher::PURGE, "Dispatcher_Client::purge::socket::1"); 3740 send_message(pid, "Dispatcher_Client::purge::socket::2"); 382pid_t Dispatcher_Client::query_by_token(uint32 token) 3840 send_message(Dispatcher::QUERY_BY_TOKEN, "Dispatcher_Client::query_by_token::socket::1"); 3850 send_message(token, "Dispatcher_Client::query_by_token::socket::2"); 3870 return ack_arrived(); 391Client_Status Dispatcher_Client::query_my_status(uint32 token) 3930 send_message(Dispatcher::QUERY_MY_STATUS, "Dispatcher_Client::query_my_status::socket::1"); 3940 send_message(token, "Dispatcher_Client::query_my_status::socket::2"); 3960 Client_Status result; 3970 result.rate_limit = ack_arrived(); 4010 Running_Query query; 4020 query.status = ack_arrived(); 4030 if (query.status == 0) 4050 query.pid = ack_arrived(); 4060 query.max_time = ack_arrived(); 4070 query.max_space = ((uint64)ack_arrived() <<32) | ack_arrived(); 4080 query.start_time = ack_arrived(); 4090 result.queries.push_back(query); 4140 uint32 slot_start = ack_arrived(); 4150 if (slot_start == 0) 4170 result.slot_starts.push_back(slot_start); 4200 std::sort(result.slot_starts.begin(), result.slot_starts.end()); 426void Dispatcher_Client::set_global_limits( 427 uint64 max_allowed_space, uint64 max_allowed_time_units, int32_t rate_limit, int32_t bit_limits) 4310 send_message(Dispatcher::SET_GLOBAL_LIMITS, "Dispatcher_Client::set_global_limits::1"); 4320 send_message(max_allowed_space, "Dispatcher_Client::set_global_limits::2"); 4330 send_message(max_allowed_time_units, "Dispatcher_Client::set_global_limits::3"); 4340 send_message(rate_limit, "Dispatcher_Client::set_global_limits::4"); 4350 send_message(bit_limits, "Dispatcher_Client::set_global_limits::5"); 443void Dispatcher_Client::ping() 445// Ping-Feature removed. The concept of unassured messages doesn't fit in the context of strict 446// two-directional communication. 447// send_message(Dispatcher::PING, "Dispatcher_Client::ping::socket"); 451void Dispatcher_Client::terminate() 4550 send_message(Dispatcher::TERMINATE, "Dispatcher_Client::terminate::socket"); 463void Dispatcher_Client::output_status() 4670 send_message(Dispatcher::OUTPUT_STATUS, "Dispatcher_Client::output_status::socket"); 4730 std::ifstream status((shadow_name + ".status").c_str()); 4750 std::getline(status, buffer); 4760 while (status.good()) 4780 std::cout<<buffer<<'\n'; 4790 std::getline(status, buffer);