]> AND Private Git Repository - loba.git/blob - communicator.h
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Add copyright, and license files.
[loba.git] / communicator.h
1 // Asynchronous communicator
2
3 #ifndef COMMUNICATOR_H
4 #define COMMUNICATOR_H
5
6 #include <vector>
7 #include <msg/msg.h>
8 #include "hostdata.h"
9 #include "messages.h"
10 #include "msg_thread.h"
11 #include "simgrid_features.h"
12
13 class communicator {
14 public:
15     communicator();
16     ~communicator();
17
18     // Send a message to the "dest" mailbox
19     void ctrl_send(const char* dest, message* msg) {
20         ctrl_sent.push_back(real_send(dest, msg));
21     }
22     void data_send(const char* dest, message* msg) {
23         data_sent.push_back(real_send(dest, msg));
24     }
25
26     // Try to flush pending sending communications.
27     // If "wait" is true, blocks until success.
28     void ctrl_flush(bool wait) {
29         real_flush(ctrl_sent, wait);
30     }
31     void data_flush(bool wait) {
32         real_flush(data_sent, wait);
33     }
34
35     // Flush all sending_communications.  Blocking.
36     void flush_all();
37
38     // Try to get a message.  Returns true on success.
39     // Parameter "timeout" may be 0 for non-blocking operation, -1 for
40     // infinite waiting, or any positive timeout.
41     bool ctrl_recv(message*& msg, msg_host_t& from, double timeout) {
42         return ctrl_received.pop(msg, from, timeout);
43     }
44     bool data_recv(message*& msg, msg_host_t& from, double timeout) {
45         return data_received.pop(msg, from, timeout);
46     }
47
48 private:
49     // Myself
50     const hostdata* host;
51
52     // List of pending send communications
53     typedef std::vector<msg_comm_t> sent_comm_type;
54     sent_comm_type ctrl_sent;
55     sent_comm_type data_sent;
56
57     msg_comm_t real_send(const char* dest, message* msg);
58     void real_flush(sent_comm_type& sent_comm, bool wait);
59
60     // Queues of received messages
61     message_queue ctrl_received;
62     message_queue data_received;
63
64     // Handling of receiving thread
65     msg_thread* receiver_thread;
66     void receiver();
67
68     // Used to chek if a communication is successfull before destroying it
69     static void comm_check_n_destroy(msg_comm_t comm);
70     // If comm is over, call comm_check_n_destroy(comm), and return true
71     static bool comm_test_n_destroy(msg_comm_t comm);
72 };
73
74 #endif // !COMMUNICATOR_H
75
76 // Local variables:
77 // mode: c++
78 // End: