X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/347996b4a10c4e8579080692afa60e0afb88b60a..0b7d1f86375fc32d181c2ab574eed9dadee72db6:/src/mc/mc_client.cpp diff --git a/src/mc/mc_client.cpp b/src/mc/mc_client.cpp index 07e65fb66c..7598472033 100644 --- a/src/mc/mc_client.cpp +++ b/src/mc/mc_client.cpp @@ -19,37 +19,41 @@ #include "src/mc/mc_client.h" // We won't need those once the separation MCer/MCed is complete: -#include "src/mc/mc_mmalloc.h" #include "src/mc/mc_ignore.h" #include "src/mc/mc_private.h" // MC_deadlock_check() #include "src/mc/mc_smx.h" -extern "C" { - XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic"); -mc_client_t mc_client; +namespace simgrid { +namespace mc { + +std::unique_ptr Client::client_; -void MC_client_init(void) +Client* Client::initialize() { - if (mc_mode != MC_MODE_NONE) - return; + // We are not in MC mode: + // TODO, handle this more gracefully. if (!getenv(MC_ENV_SOCKET_FD)) - return; - mc_mode = MC_MODE_CLIENT; + return nullptr; - if (mc_client) { - XBT_WARN("MC_client_init called more than once."); - return; - } + // Do not break if we are called multiple times: + if (client_) + return client_.get(); + + // Check and set the mode: + if (mc_mode != MC_MODE_NONE) + abort(); + mc_mode = MC_MODE_CLIENT; + // Fetch socket from MC_ENV_SOCKET_FD: char* fd_env = std::getenv(MC_ENV_SOCKET_FD); if (!fd_env) - xbt_die("MC socket not found"); - - int fd = xbt_str_parse_int(fd_env,bprintf("Variable %s should contain a number but contains '%%s'", MC_ENV_SOCKET_FD)); + xbt_die("No MC socket passed in the environment"); + int fd = xbt_str_parse_int(fd_env, bprintf("Variable %s should contain a number but contains '%%s'", MC_ENV_SOCKET_FD)); XBT_DEBUG("Model-checked application found socket FD %i", fd); + // Check the socket type/validity: int type; socklen_t socklen = sizeof(type); if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0) @@ -58,36 +62,25 @@ void MC_client_init(void) xbt_die("Unexpected socket type %i", type); XBT_DEBUG("Model-checked application found expected socket type"); - mc_client = xbt_new0(s_mc_client_t, 1); - mc_client->fd = fd; - mc_client->active = 1; + client_ = std::unique_ptr(new simgrid::mc::Client(fd)); - // Waiting for the model-checker: - if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) == -1 || raise(SIGSTOP) != 0) + // Wait for the model-checker: + if (ptrace(PTRACE_TRACEME, 0, nullptr, NULL) == -1 || raise(SIGSTOP) != 0) xbt_die("Could not wait for the model-checker"); - MC_client_handle_messages(); -} - -void MC_client_send_message(void* message, size_t size) -{ - if (MC_protocol_send(mc_client->fd, message, size)) - xbt_die("Could not send message %i", (int) ((mc_message_t)message)->type); -} -void MC_client_send_simple_message(e_mc_message_type type) -{ - if (MC_protocol_send_simple_message(mc_client->fd, type)) - xbt_die("Could not send message %i", type); + client_->handleMessages(); + return client_.get(); } -void MC_client_handle_messages(void) +void Client::handleMessages() { while (1) { XBT_DEBUG("Waiting messages from model-checker"); char message_buffer[MC_MESSAGE_LENGTH]; ssize_t s; - if ((s = MC_receive_message(mc_client->fd, &message_buffer, sizeof(message_buffer), 0)) < 0) + + if ((s = channel_.receive(&message_buffer, sizeof(message_buffer))) < 0) xbt_die("Could not receive commands from the model-checker"); s_mc_message_t message; @@ -102,7 +95,7 @@ void MC_client_handle_messages(void) s_mc_int_message_t answer; answer.type = MC_MESSAGE_DEADLOCK_CHECK_REPLY; answer.value = result; - if (MC_protocol_send(mc_client->fd, &answer, sizeof(answer))) + if (channel_.send(answer)) xbt_die("Could not send response"); } break; @@ -120,7 +113,8 @@ void MC_client_handle_messages(void) if (!process) xbt_die("Invalid pid %lu", (unsigned long) message.pid); SIMIX_simcall_handle(&process->simcall, message.value); - MC_protocol_send_simple_message(mc_client->fd, MC_MESSAGE_WAITING); + if (channel_.send(MC_MESSAGE_WAITING)) + xbt_die("Could not send MESSAGE_WAITING to model-checker"); } break; @@ -144,13 +138,15 @@ void MC_client_handle_messages(void) } } -void MC_client_main_loop(void) +void Client::mainLoop(void) { while (1) { - MC_protocol_send_simple_message(mc_client->fd, MC_MESSAGE_WAITING); - MC_client_handle_messages(); - MC_wait_for_requests(); + if (channel_.send(MC_MESSAGE_WAITING)) + xbt_die("Could not send WAITING mesage to model-checker"); + this->handleMessages(); + simgrid::mc::wait_for_requests(); } } } +}