Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Basic infrastructure for a real model-checker process
[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
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_protocol, mc, "Generic MC protocol logic");
18
19 int MC_protocol_send_simple_message(int socket, int type)
20 {
21   s_mc_message_t message;
22   message.type = type;
23
24   while (send(socket, &message, sizeof(message), 0) == -1) {
25     if (errno == EINTR)
26       continue;
27     else
28       return errno;
29   }
30   return 0;
31 }
32
33 int MC_protocol_hello(int socket)
34 {
35   int e;
36   if ((e = MC_protocol_send_simple_message(socket, MC_MESSAGE_HELLO)) != 0) {
37     XBT_ERROR("Could not send HELLO message: %s", strerror(e));
38     return 1;
39   }
40
41   s_mc_message_t message;
42   message.type = MC_MESSAGE_NONE;
43
44   while (recv(socket, &message, sizeof(message), 0) == -1) {
45     if (errno == EINTR)
46       continue;
47     else {
48       XBT_ERROR("Could not receive HELLO message: %s", strerror(errno));
49       return 2;
50     }
51   }
52   if (message.type != MC_MESSAGE_HELLO) {
53     XBT_ERROR("Did not receive suitable HELLO message. Who are you?");
54     return 3;
55   }
56
57   return 0;
58 }