Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Adding test for SURF concurrency feature
[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 #include <stdio.h> // perror
10 #include <cstddef> // std::size_t
11
12 #include <sys/types.h>
13 #include <sys/socket.h>
14
15 #include <xbt/log.h>
16
17 #include "src/mc/mc_protocol.h"
18 #include "src/mc/mc_client.h"
19
20 extern "C" {
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_protocol, mc, "Generic MC protocol logic");
23
24 int MC_protocol_send(int socket, const void* message, std::size_t size)
25 {
26   XBT_DEBUG("Protocol [%s] send %s",
27     MC_mode_name(mc_mode),
28     MC_message_type_name(*(e_mc_message_type*) message));
29
30   while (send(socket, message, size, 0) == -1) {
31     if (errno == EINTR)
32       continue;
33     else
34       return errno;
35   }
36   return 0;
37 }
38
39 int MC_protocol_send_simple_message(int socket, e_mc_message_type type)
40 {
41   s_mc_message_t message;
42   message.type = type;
43   return MC_protocol_send(socket, &message, sizeof(message));
44 }
45
46 ssize_t MC_receive_message(int socket, void* message, size_t size, int options)
47 {
48   int res = recv(socket, message, size, options);
49   if (res != -1) {
50     XBT_DEBUG("Protocol [%s] received %s",
51       MC_mode_name(mc_mode),
52       MC_message_type_name(*(e_mc_message_type*) message));
53   }
54   return res;
55 }
56
57 const char* MC_message_type_name(e_mc_message_type type)
58 {
59   switch(type) {
60   case MC_MESSAGE_NONE:
61     return "NONE";
62   case MC_MESSAGE_CONTINUE:
63     return "CONTINUE";
64   case MC_MESSAGE_IGNORE_HEAP:
65     return "IGNORE_HEAP";
66   case MC_MESSAGE_UNIGNORE_HEAP:
67     return "UNIGNORE_HEAP";
68   case MC_MESSAGE_IGNORE_MEMORY:
69     return "IGNORE_MEMORY";
70   case MC_MESSAGE_STACK_REGION:
71     return "STACK_REGION";
72   case MC_MESSAGE_REGISTER_SYMBOL:
73     return "REGISTER_SYMBOL";
74   case MC_MESSAGE_DEADLOCK_CHECK:
75     return "DEADLOCK_CHECK";
76   case MC_MESSAGE_DEADLOCK_CHECK_REPLY:
77     return "DEADLOCK_CHECK_REPLY";
78   case MC_MESSAGE_WAITING:
79     return "WAITING";
80   case MC_MESSAGE_SIMCALL_HANDLE:
81     return "SIMCALL_HANDLE";
82   case MC_MESSAGE_ASSERTION_FAILED:
83     return "ASSERTION_FAILED";
84   default:
85     return "?";
86   }
87 }
88
89 const char* MC_mode_name(e_mc_mode_t mode)
90 {
91   switch(mode) {
92   case MC_MODE_NONE:
93     return "NONE";
94   case MC_MODE_CLIENT:
95     return "CLIENT";
96   case MC_MODE_SERVER:
97     return "SERVER";
98   default:
99     return "?";
100   }
101 }
102
103 }