Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c51979787b38602f34a2672e5dd10d129d4559d5
[simgrid.git] / src / mc / mc_protocol.c
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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_protocol, mc, "Generic MC protocol logic");
19
20 int MC_protocol_send(int socket, void* message, size_t size)
21 {
22   XBT_DEBUG("Protocol [%s] send %s",
23     MC_mode_name(mc_mode),
24     MC_message_type_name(*(e_mc_message_type*) message));
25
26   while (send(socket, message, size, 0) == -1) {
27     if (errno == EINTR)
28       continue;
29     else
30       return errno;
31   }
32   return 0;
33 }
34
35 int MC_protocol_send_simple_message(int socket, int type)
36 {
37   s_mc_message_t message;
38   message.type = type;
39   return MC_protocol_send(socket, &message, sizeof(message));
40 }
41
42 int MC_protocol_hello(int socket)
43 {
44   int e;
45   if ((e = MC_protocol_send_simple_message(socket, MC_MESSAGE_HELLO)) != 0) {
46     XBT_ERROR("Could not send HELLO message");
47     return 1;
48   }
49
50   s_mc_message_t message;
51   message.type = MC_MESSAGE_NONE;
52
53   size_t s;
54   while ((s = MC_receive_message(socket, &message, sizeof(message), 0)) == -1) {
55     if (errno == EINTR)
56       continue;
57     else {
58       XBT_ERROR("Could not receive HELLO message");
59       return 2;
60     }
61   }
62   if (s < sizeof(message) || message.type != MC_MESSAGE_HELLO) {
63     XBT_ERROR("Did not receive suitable HELLO message. Who are you?");
64     return 3;
65   }
66
67   return 0;
68 }
69
70 ssize_t MC_receive_message(int socket, void* message, size_t size, int options)
71 {
72   int res = recv(socket, message, size, options);
73   if (res != -1) {
74     XBT_DEBUG("Protocol [%s] received %s",
75       MC_mode_name(mc_mode),
76       MC_message_type_name(*(e_mc_message_type*) message));
77   }
78   return res;
79 }
80
81 const char* MC_message_type_name(e_mc_message_type type)
82 {
83   switch(type) {
84   case MC_MESSAGE_NONE:
85     return "NONE";
86   case MC_MESSAGE_HELLO:
87     return "HELLO";
88   case MC_MESSAGE_CONTINUE:
89     return "CONTINUE";
90   case MC_MESSAGE_IGNORE_HEAP:
91     return "IGNORE_HEAP";
92   case MC_MESSAGE_UNIGNORE_HEAP:
93     return "UNIGNORE_HEAP";
94   case MC_MESSAGE_IGNORE_MEMORY:
95     return "IGNORE_MEMORY";
96   case MC_MESSAGE_STACK_REGION:
97     return "STACK_REGION";
98   case MC_MESSAGE_REGISTER_SYMBOL:
99     return "REGISTER_SYMBOL";
100   case MC_MESSAGE_DEADLOCK_CHECK:
101     return "DEADLOCK_CHECK";
102   case MC_MESSAGE_DEADLOCK_CHECK_REPLY:
103     return "DEADLOCK_CHECK_REPLY";
104   case MC_MESSAGE_WAITING:
105     return "WAITING";
106   case MC_MESSAGE_SIMCALL_HANDLE:
107     return "SIMCALL_HANDLE";
108   case MC_MESSAGE_ASSERTION_FAILED:
109     return "ASSERTION_FAILED";
110   default:
111     return "?";
112   }
113 }
114
115 const char* MC_mode_name(e_mc_mode_t mode)
116 {
117   switch(mode) {
118   case MC_MODE_NONE:
119     return "NONE";
120   case MC_MODE_STANDALONE:
121     return "STANDALONE";
122   case MC_MODE_CLIENT:
123     return "CLIENT";
124   case MC_MODE_SERVER:
125     return "SERVER";
126   default:
127     return "?";
128   }
129 }