Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Better handling of test any
[simgrid.git] / src / mc / remote / Channel.hpp
1 /* Copyright (c) 2015-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_CHANNEL_HPP
7 #define SIMGRID_MC_CHANNEL_HPP
8
9 #include "src/mc/remote/mc_protocol.h"
10
11 #include <type_traits>
12
13 namespace simgrid::mc {
14
15 /** A channel for exchanging messages between model-checker and model-checked app
16  *
17  *  This abstracts away the way the messages are transferred. Currently, they
18  *  are sent over a (connected) `SOCK_SEQPACKET` socket.
19  */
20 class Channel {
21   int socket_ = -1;
22   template <class M> static constexpr bool messageType() { return std::is_class_v<M> && std::is_trivial_v<M>; }
23
24 public:
25   Channel() = default;
26   explicit Channel(int sock) : socket_(sock) {}
27   ~Channel();
28
29   // No copy:
30   Channel(Channel const&) = delete;
31   Channel& operator=(Channel const&) = delete;
32
33   // Send
34   int send(const void* message, size_t size) const;
35   int send(MessageType type) const
36   {
37     s_mc_message_t message = {type};
38     return this->send(&message, sizeof(message));
39   }
40   /** @brief Send a message; returns 0 on success or errno on failure */
41   template <class M> typename std::enable_if_t<messageType<M>(), int> send(M const& m) const
42   {
43     return this->send(&m, sizeof(M));
44   }
45
46   // Receive
47   ssize_t receive(void* message, size_t size) const;
48   template <class M> typename std::enable_if_t<messageType<M>(), ssize_t> receive(M& m) const
49   {
50     return this->receive(&m, sizeof(M));
51   }
52
53   // Socket handling
54   int get_socket() const { return socket_; }
55   void reset_socket(int socket) { socket_ = socket; }
56 };
57 } // namespace simgrid::mc
58
59 #endif