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

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