Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remote support for MC_deadlock_check() using MC_MESSAGE_DEADLOCK_CHECK IPC message
[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   while (send(socket, message, size, 0) == -1) {
23     if (errno == EINTR)
24       continue;
25     else
26       return errno;
27   }
28   return 0;
29 }
30
31 int MC_protocol_send_simple_message(int socket, int type)
32 {
33   s_mc_message_t message;
34   message.type = type;
35   return MC_protocol_send(socket, &message, sizeof(message));
36 }
37
38 int MC_protocol_hello(int socket)
39 {
40   int e;
41   if ((e = MC_protocol_send_simple_message(socket, MC_MESSAGE_HELLO)) != 0) {
42     XBT_ERROR("Could not send HELLO message: %s", strerror(e));
43     return 1;
44   }
45
46   s_mc_message_t message;
47   message.type = MC_MESSAGE_NONE;
48
49   size_t s;
50   while ((s = recv(socket, &message, sizeof(message), 0)) == -1) {
51     if (errno == EINTR)
52       continue;
53     else {
54       XBT_ERROR("Could not receive HELLO message: %s", strerror(errno));
55       return 2;
56     }
57   }
58   if (s < sizeof(message) || message.type != MC_MESSAGE_HELLO) {
59     XBT_ERROR("Did not receive suitable HELLO message. Who are you?");
60     return 3;
61   }
62
63   return 0;
64 }
65
66 ssize_t MC_receive_message(int socket, void* message, size_t size)
67 {
68   return recv(socket, message, size, 0);
69 }