Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / src / mc / remote / Channel.hpp
1 /* Copyright (c) 2015-2020. 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 {
14 namespace mc {
15
16 /** A channel for exchanging messages between model-checker and model-checked app
17  *
18  *  This abstracts away the way the messages are transferred. Currently, they
19  *  are sent over a (connected) `SOCK_SEQPACKET` socket.
20  */
21 class Channel {
22   int socket_ = -1;
23   template <class M> static constexpr bool messageType()
24   {
25     return std::is_class<M>::value && std::is_trivial<M>::value;
26   }
27
28 public:
29   explicit Channel(int sock) : socket_(sock) {}
30   ~Channel();
31
32   // No copy:
33   Channel(Channel const&) = delete;
34   Channel& operator=(Channel const&) = delete;
35
36   // Send
37   int send(const void* message, size_t size) const;
38   int send(e_mc_message_type type) const
39   {
40     s_mc_message_t message = {type};
41     return this->send(&message, sizeof(message));
42   }
43   /** @brief Send a message; returns 0 on success or errno on failure */
44   template <class M> typename std::enable_if<messageType<M>(), int>::type send(M const& m) const
45   {
46     return this->send(&m, sizeof(M));
47   }
48
49   // Receive
50   ssize_t receive(void* message, size_t size, bool block = true) const;
51   template <class M> typename std::enable_if<messageType<M>(), ssize_t>::type receive(M& m) const
52   {
53     return this->receive(&m, sizeof(M));
54   }
55
56   int get_socket() const { return socket_; }
57 };
58 }
59 }
60
61 #endif