Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into fix/execute_benched
[simgrid.git] / src / mc / remote / Channel.hpp
1 /* Copyright (c) 2015-2017. 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 #ifndef SIMGRID_MC_CHANNEL_HPP
8 #define SIMGRID_MC_CHANNEL_HPP
9
10 #include <unistd.h>
11
12 #include <type_traits>
13
14 #include "src/mc/remote/mc_protocol.h"
15
16 namespace simgrid {
17 namespace mc {
18
19 /** A channel for exchanging messages between model-checker and model-checked
20  *
21  *  This abstracts away the way the messages are transferred. Currently, they
22  *  are sent over a (connected) `SOCK_SEQPACKET` socket.
23  */
24 class Channel {
25   int socket_ = -1;
26   template <class M> static constexpr bool messageType()
27   {
28     return std::is_class<M>::value && std::is_trivial<M>::value;
29   }
30
31 public:
32   Channel() = default;
33   explicit Channel(int sock) : socket_(sock) {}
34   ~Channel();
35
36   // No copy:
37   Channel(Channel const&) = delete;
38   Channel& operator=(Channel const&) = delete;
39
40   // Move:
41   Channel(Channel&& that) : socket_(that.socket_) { that.socket_ = -1; }
42   Channel& operator=(Channel&& that)
43   {
44     this->socket_ = that.socket_;
45     that.socket_  = -1;
46     return *this;
47   }
48
49   // Send
50   int send(const void* message, size_t size) const;
51   int send(e_mc_message_type type) const
52   {
53     s_mc_message_t message = {type};
54     return this->send(&message, sizeof(message));
55   }
56   /** @brief Send a message; returns 0 on success or errno on failure */
57   template <class M> typename std::enable_if<messageType<M>(), int>::type send(M const& m) const
58   {
59     return this->send(&m, sizeof(M));
60   }
61
62   // Receive
63   ssize_t receive(void* message, size_t size, bool block = true) const;
64   template <class M> typename std::enable_if<messageType<M>(), ssize_t>::type receive(M& m) const
65   {
66     return this->receive(&m, sizeof(M));
67   }
68
69   int getSocket() const { return socket_; }
70 };
71 }
72 }
73
74 #endif