Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of github.com:mquinson/simgrid
[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, 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 int MC_protocol_hello(int socket)
45 {
46   int e;
47   if ((e = MC_protocol_send_simple_message(socket, MC_MESSAGE_HELLO)) != 0) {
48     XBT_ERROR("Could not send HELLO message");
49     return 1;
50   }
51
52   s_mc_message_t message;
53   message.type = MC_MESSAGE_NONE;
54
55   ssize_t s;
56   while ((s = MC_receive_message(socket, &message, sizeof(message), 0)) == -1) {
57     if (errno == EINTR)
58       continue;
59     else {
60       XBT_ERROR("Could not receive HELLO message");
61       return 2;
62     }
63   }
64   if ((size_t) s < sizeof(message) || message.type != MC_MESSAGE_HELLO) {
65     XBT_ERROR("Did not receive suitable HELLO message. Who are you?");
66     return 3;
67   }
68
69   return 0;
70 }
71
72 ssize_t MC_receive_message(int socket, void* message, size_t size, int options)
73 {
74   int res = recv(socket, message, size, options);
75   if (res != -1) {
76     XBT_DEBUG("Protocol [%s] received %s",
77       MC_mode_name(mc_mode),
78       MC_message_type_name(*(e_mc_message_type*) message));
79   }
80   return res;
81 }
82
83 const char* MC_message_type_name(e_mc_message_type type)
84 {
85   switch(type) {
86   case MC_MESSAGE_NONE:
87     return "NONE";
88   case MC_MESSAGE_HELLO:
89     return "HELLO";
90   case MC_MESSAGE_CONTINUE:
91     return "CONTINUE";
92   case MC_MESSAGE_IGNORE_HEAP:
93     return "IGNORE_HEAP";
94   case MC_MESSAGE_UNIGNORE_HEAP:
95     return "UNIGNORE_HEAP";
96   case MC_MESSAGE_IGNORE_MEMORY:
97     return "IGNORE_MEMORY";
98   case MC_MESSAGE_STACK_REGION:
99     return "STACK_REGION";
100   case MC_MESSAGE_REGISTER_SYMBOL:
101     return "REGISTER_SYMBOL";
102   case MC_MESSAGE_DEADLOCK_CHECK:
103     return "DEADLOCK_CHECK";
104   case MC_MESSAGE_DEADLOCK_CHECK_REPLY:
105     return "DEADLOCK_CHECK_REPLY";
106   case MC_MESSAGE_WAITING:
107     return "WAITING";
108   case MC_MESSAGE_SIMCALL_HANDLE:
109     return "SIMCALL_HANDLE";
110   case MC_MESSAGE_ASSERTION_FAILED:
111     return "ASSERTION_FAILED";
112   default:
113     return "?";
114   }
115 }
116
117 const char* MC_mode_name(e_mc_mode_t mode)
118 {
119   switch(mode) {
120   case MC_MODE_NONE:
121     return "NONE";
122   case MC_MODE_CLIENT:
123     return "CLIENT";
124   case MC_MODE_SERVER:
125     return "SERVER";
126   default:
127     return "?";
128   }
129 }
130
131 }