Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Make Process::socket_ private
authorGabriel Corona <gabriel.corona@loria.fr>
Thu, 28 May 2015 10:58:54 +0000 (12:58 +0200)
committerGabriel Corona <gabriel.corona@loria.fr>
Thu, 28 May 2015 10:58:54 +0000 (12:58 +0200)
src/mc/mc_global.cpp
src/mc/mc_process.cpp
src/mc/mc_process.h
src/mc/mc_protocol.cpp
src/mc/mc_protocol.h
src/mc/mc_server.cpp

index 337b1ba..1df6abe 100644 (file)
@@ -192,11 +192,10 @@ int MC_deadlock_check()
 {
   if (mc_mode == MC_MODE_SERVER) {
     int res;
-    if ((res = MC_protocol_send_simple_message(mc_model_checker->process().socket,
-      MC_MESSAGE_DEADLOCK_CHECK)))
+    if ((res = mc_model_checker->process().send_message(MC_MESSAGE_DEADLOCK_CHECK)))
       xbt_die("Could not check deadlock state");
     s_mc_int_message_t message;
-    ssize_t s = MC_receive_message(mc_model_checker->process().socket, &message, sizeof(message), 0);
+    ssize_t s = mc_model_checker->process().receive_message(message);
     if (s == -1)
       xbt_die("Could not receive message");
     else if (s != sizeof(message) || message.type != MC_MESSAGE_DEADLOCK_CHECK_REPLY) {
index d59e94d..e8b1b33 100644 (file)
@@ -190,7 +190,7 @@ Process::Process(pid_t pid, int sockfd)
   Process* process = this;
 
   process->process_flags = MC_PROCESS_NO_FLAG;
-  process->socket = sockfd;
+  process->socket_ = sockfd;
   process->pid_ = pid;
   if (pid==getpid())
     process->process_flags |= MC_PROCESS_SELF_FLAG;
@@ -239,6 +239,9 @@ Process::~Process()
 {
   Process* process = this;
 
+  if (this->socket_ >= 0 && close(this->socket_) < 0)
+    xbt_die("Could not close communication socket");
+
   process->process_flags = MC_PROCESS_NO_FLAG;
   process->pid_ = 0;
 
index 4e30122..a453d46 100644 (file)
@@ -7,7 +7,8 @@
 #ifndef MC_PROCESS_H
 #define MC_PROCESS_H
 
-#include <stdbool.h>
+#include <type_traits>
+
 #include <sys/types.h>
 
 #include <vector>
@@ -135,6 +136,25 @@ public:
     return status_;
   }
 
+  template<class M>
+  typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, int >::type
+  send_message(M const& m)
+  {
+    return MC_protocol_send(this->socket_, &m, sizeof(M));
+  }
+
+  int send_message(e_mc_message_type message_id)
+  {
+    return MC_protocol_send_simple_message(this->socket_, message_id);
+  }
+
+  template<class M>
+  typename std::enable_if< std::is_class<M>::value && std::is_trivial<M>::value, ssize_t >::type
+  receive_message(M& m)
+  {
+    return MC_receive_message(this->socket_, &m, sizeof(M), 0);
+  }
+
 private:
   void init_memory_map_info();
   void refresh_heap();
@@ -142,9 +162,7 @@ private:
 private:
   mc_process_flags_t process_flags;
   pid_t pid_;
-public: // to be private
-  int socket;
-private:
+  int socket_;
   int status_;
   bool running_;
   std::vector<VmMap> memory_map_;
index 01c4f9d..09b3227 100644 (file)
@@ -19,7 +19,7 @@ extern "C" {
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_protocol, mc, "Generic MC protocol logic");
 
-int MC_protocol_send(int socket, void* message, size_t size)
+int MC_protocol_send(int socket, const void* message, size_t size)
 {
   XBT_DEBUG("Protocol [%s] send %s",
     MC_mode_name(mc_mode),
index 38c053c..c71c554 100644 (file)
@@ -103,7 +103,7 @@ typedef struct s_mc_register_symbol_message {
   void* data;
 } s_mc_register_symbol_message_t, * mc_register_symbol_message_t;
 
-XBT_INTERNAL int MC_protocol_send(int socket, void* message, size_t size);
+XBT_INTERNAL int MC_protocol_send(int socket, const void* message, size_t size);
 XBT_INTERNAL int MC_protocol_send_simple_message(int socket, e_mc_message_type type);
 XBT_INTERNAL int MC_protocol_hello(int socket);
 XBT_INTERNAL ssize_t MC_receive_message(int socket, void* message, size_t size, int options);
index 9ea15bd..2c83424 100644 (file)
@@ -124,8 +124,7 @@ void s_mc_server::exit()
 
 void s_mc_server::resume(mc_process_t process)
 {
-  int socket = process->socket;
-  int res = MC_protocol_send_simple_message(socket, MC_MESSAGE_CONTINUE);
+  int res = process->send_message(MC_MESSAGE_CONTINUE);
   if (res)
     throw std::system_error(res, std::system_category());
   process->cache_flags = (mc_process_cache_flags_t) 0;
@@ -351,7 +350,7 @@ void MC_server_simcall_handle(mc_process_t process, unsigned long pid, int value
   m.type  = MC_MESSAGE_SIMCALL_HANDLE;
   m.pid   = pid;
   m.value = value;
-  MC_protocol_send(mc_model_checker->process().socket, &m, sizeof(m));
+  mc_model_checker->process().send_message(m);
   process->cache_flags = (mc_process_cache_flags_t) 0;
   while (mc_model_checker->process().running()) {
     if (!mc_server->handle_events())