1 /* Copyright (c) 2015. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include <sys/types.h>
12 #include <sys/socket.h>
15 #include <xbt/sysdep.h>
17 #include "mc_protocol.h"
18 #include "mc_client.h"
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_client, mc, "MC client logic");
22 s_mc_client_t mc_client;
24 void MC_client_init(void)
28 char* fd_env = getenv(MC_ENV_SOCKET_FD);
30 xbt_die("MC socket not found");
32 int fd = atoi(fd_env);
33 XBT_DEBUG("Model-checked application found socket FD %i", fd);
36 socklen_t socklen = sizeof(type);
37 if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &socklen) != 0)
38 xbt_die("Could not check socket type: %s", strerror(errno));
39 if (type != SOCK_DGRAM)
40 xbt_die("Unexpected socket type %i", type);
41 XBT_DEBUG("Model-checked application found expected socket type");
46 void MC_client_hello(void)
48 XBT_DEBUG("Greeting the MC server");
49 if (MC_protocol_hello(mc_client.fd) != 0)
50 xbt_die("Could not say hello the MC server");
51 XBT_DEBUG("Greeted the MC server");
54 void MC_client_handle_messages(void)
57 XBT_DEBUG("Waiting messages from model-checker");
59 char message_buffer[MC_MESSAGE_LENGTH];
61 if ((s = recv(mc_client.fd, &message_buffer, sizeof(message_buffer), 0)) == -1)
62 xbt_die("Could not receive commands from the model-checker: %s",
65 XBT_DEBUG("Receive message from model-checker");
66 s_mc_message_t message;
67 if (s < sizeof(message))
68 xbt_die("Message is too short");
69 memcpy(&message, message_buffer, sizeof(message));
70 switch (message.type) {
71 case MC_MESSAGE_CONTINUE:
74 xbt_die("Unexpected message from model-checker %i", message.type);