Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
e56e12108bb21390519646ca065a78d72bf8619b
[simgrid.git] / src / mc / mc_protocol.cpp
1 /* Copyright (c) 2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <errno.h>
8 #include <string.h>
9
10 #include <sys/types.h>
11 #include <sys/socket.h>
12
13 #include <xbt/log.h>
14
15 #include "mc_protocol.h"
16 #include "mc_client.h"
17
18 extern "C" {
19
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_protocol, mc, "Generic MC protocol logic");
21
22 int MC_protocol_send(int socket, const void* message, std::size_t size)
23 {
24   XBT_DEBUG("Protocol [%s] send %s",
25     MC_mode_name(mc_mode),
26     MC_message_type_name(*(e_mc_message_type*) message));
27
28   while (send(socket, message, size, 0) == -1) {
29     if (errno == EINTR)
30       continue;
31     else
32       return errno;
33   }
34   return 0;
35 }
36
37 int MC_protocol_send_simple_message(int socket, e_mc_message_type type)
38 {
39   s_mc_message_t message;
40   message.type = type;
41   return MC_protocol_send(socket, &message, sizeof(message));
42 }
43
44 ssize_t MC_receive_message(int socket, void* message, size_t size, int options)
45 {
46   int res = recv(socket, message, size, options);
47   if (res != -1) {
48     XBT_DEBUG("Protocol [%s] received %s",
49       MC_mode_name(mc_mode),
50       MC_message_type_name(*(e_mc_message_type*) message));
51   }
52   return res;
53 }
54
55 const char* MC_message_type_name(e_mc_message_type type)
56 {
57   switch(type) {
58   case MC_MESSAGE_NONE:
59     return "NONE";
60   case MC_MESSAGE_CONTINUE:
61     return "CONTINUE";
62   case MC_MESSAGE_IGNORE_HEAP:
63     return "IGNORE_HEAP";
64   case MC_MESSAGE_UNIGNORE_HEAP:
65     return "UNIGNORE_HEAP";
66   case MC_MESSAGE_IGNORE_MEMORY:
67     return "IGNORE_MEMORY";
68   case MC_MESSAGE_STACK_REGION:
69     return "STACK_REGION";
70   case MC_MESSAGE_REGISTER_SYMBOL:
71     return "REGISTER_SYMBOL";
72   case MC_MESSAGE_DEADLOCK_CHECK:
73     return "DEADLOCK_CHECK";
74   case MC_MESSAGE_DEADLOCK_CHECK_REPLY:
75     return "DEADLOCK_CHECK_REPLY";
76   case MC_MESSAGE_WAITING:
77     return "WAITING";
78   case MC_MESSAGE_SIMCALL_HANDLE:
79     return "SIMCALL_HANDLE";
80   case MC_MESSAGE_ASSERTION_FAILED:
81     return "ASSERTION_FAILED";
82   default:
83     return "?";
84   }
85 }
86
87 const char* MC_mode_name(e_mc_mode_t mode)
88 {
89   switch(mode) {
90   case MC_MODE_NONE:
91     return "NONE";
92   case MC_MODE_CLIENT:
93     return "CLIENT";
94   case MC_MODE_SERVER:
95     return "SERVER";
96   default:
97     return "?";
98   }
99 }
100
101 }