Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove the stateful model-checking from the archive. It's not working anymore
[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   std::vector<char> buffer_;
24
25 public:
26   Channel() = default;
27   explicit Channel(int sock) : socket_(sock) {}
28   Channel(int sock, Channel const& other);
29   ~Channel();
30
31   // No copy:
32   Channel(Channel const&) = delete;
33   Channel& operator=(Channel const&) = delete;
34
35   // Send
36   int send(const void* message, size_t size) const;
37   int send(MessageType type) const
38   {
39     s_mc_message_t message = {type};
40     return this->send(&message, sizeof(message));
41   }
42   /** @brief Send a message; returns 0 on success or errno on failure */
43   template <class M> typename std::enable_if_t<messageType<M>(), int> send(M const& m) const
44   {
45     return this->send(&m, sizeof(M));
46   }
47
48   // Receive
49   ssize_t receive(void* message, size_t size, int flags = 0);
50   template <class M> typename std::enable_if_t<messageType<M>(), ssize_t> receive(M& m)
51   {
52     return this->receive(&m, sizeof(M), 0);
53   }
54   void reinject(const char* data, size_t size);
55   bool has_pending_data() const { return not buffer_.empty(); }
56
57   // Socket handling
58   int get_socket() const { return socket_; }
59   void reset_socket(int socket) { socket_ = socket; }
60 };
61 } // namespace simgrid::mc
62
63 #endif