Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
plug a memleak
[simgrid.git] / src / mc / Channel.hpp
1 /* Copyright (c) 2015-2016. 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/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 hides the way the messages are transfered. Currently, they are sent
22  *  over a SOCK_DGRAM socket.
23  */
24 class Channel {
25   int socket_ = -1;
26   template<class M>
27   static constexpr bool messageType()
28   {
29     return std::is_class<M>::value && std::is_trivial<M>::value;
30   }
31 public:
32
33   Channel() {}
34   Channel(int sock) : socket_(sock) {}
35   ~Channel();
36
37   // No copy:
38   Channel(Channel const&) = delete;
39   Channel& operator=(Channel const&) = delete;
40
41   // Move:
42   Channel(Channel&& that) : socket_(that.socket_)
43   {
44     that.socket_ = -1;
45   }
46   Channel& operator=(Channel&& that)
47   {
48     this->socket_ = that.socket_;
49     that.socket_ = -1;
50     return *this;
51   }
52
53   // Send
54   int send(const void* message, size_t size) const;
55   int send(e_mc_message_type type) const
56   {
57     s_mc_message message = { type };
58     return this->send(&message, sizeof(message));
59   }
60   template<class M>
61   typename std::enable_if< messageType<M>(), int >::type
62   send(M const& m) const
63   {
64     return this->send(&m, sizeof(M));
65   }
66
67   // Receive
68   ssize_t receive(void* message, size_t size, bool block = true) const;
69   template<class M>
70   typename std::enable_if< messageType<M>(), ssize_t >::type
71   receive(M& m) const
72   {
73     return this->receive(&m, sizeof(M));
74   }
75
76   int getSocket() const
77   {
78     return socket_;
79   }
80
81 };
82
83 }
84 }
85
86 #endif