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

Private GIT Repository
45b52ef73eb1c76bbfecd528bcb89aab8c8a5bc9
[loba.git] / communicator.cpp
1 #include <algorithm>
2 #include <tr1/functional>
3 #include <cstring>
4 #include <msg/msg.h>
5 #include <xbt/log.h>
6 #include "communicator.h"
7 #include "simgrid_features.h"
8 #include "misc.h"
9
10 // XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu);
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(comm, simu,
12                                 "Messages from asynchronous pipes");
13
14 communicator::communicator()
15 {
16     const char* hostname = MSG_host_get_name(MSG_host_self());
17
18     ctrl_mbox = hostname;
19     ctrl_mbox += "_ctrl";
20     ctrl_task = NULL;
21     ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox());
22     ctrl_close_is_last = false;
23
24     data_mbox = hostname;
25     data_mbox += "_data";
26     data_task = NULL;
27     data_comm = MSG_task_irecv(&data_task, get_data_mbox());
28     data_close_is_last = false;
29 }
30
31 communicator::~communicator()
32 {
33     if (ctrl_comm)
34         WARN0("ctrl_comm is pending!");
35     if (data_comm)
36         WARN0("data_comm is pending!");
37     if (!sent_comm.empty())
38         WARN2("lost %ld send communication%s!",
39               (long )sent_comm.size(), ESSE(sent_comm.size()));
40 }
41
42 void communicator::send(const char* dest, message* msg)
43 {
44     double msg_size = sizeof *msg;
45     if (msg->get_type() == message::LOAD)
46         msg_size += msg->get_amount();
47     m_task_t task = MSG_task_create("message", 0.0, msg_size, msg);    
48     sent_comm.push_back(MSG_task_isend(task, dest));
49     flush_sent();
50 }
51
52 bool communicator::recv(message*& msg, m_host_t& from, bool wait)
53 {
54     bool restart;
55     msg = NULL;
56
57     do {
58         if (ctrl_comm && comm_test_n_destroy(ctrl_comm)) {
59             msg = (message* )MSG_task_get_data(ctrl_task);
60             from = MSG_task_get_source(ctrl_task);
61             MSG_task_destroy(ctrl_task);
62             ctrl_task = NULL;
63             ctrl_comm =
64                 (!ctrl_close_is_last || msg->get_type() != message::CTRL_CLOSE)
65                 ? ctrl_comm = MSG_task_irecv(&ctrl_task, get_ctrl_mbox())
66                 : ctrl_comm = NULL;
67
68         } else if (data_comm && comm_test_n_destroy(data_comm)) {
69             msg = (message* )MSG_task_get_data(data_task);
70             from = MSG_task_get_source(data_task);
71             MSG_task_destroy(data_task);
72             data_task = NULL;
73             data_comm =
74                 (!data_close_is_last || msg->get_type() != message::DATA_CLOSE)
75                 ? data_comm = MSG_task_irecv(&data_task, get_data_mbox())
76                 : data_comm = NULL;
77         }
78
79         restart = wait && !msg && (ctrl_comm || data_comm);
80         if (restart) {
81             xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
82             if (ctrl_comm)
83                 xbt_dynar_push(comms, &ctrl_comm);
84             if (data_comm)
85                 xbt_dynar_push(comms, &data_comm);
86             MSG_comm_waitany(comms);
87             xbt_dynar_free(&comms);
88         }
89     } while (restart);
90
91     return msg != NULL;
92 }
93
94 void communicator::wait_for_sent()
95 {
96     xbt_dynar_t comms = xbt_dynar_new(sizeof(msg_comm_t), NULL);
97     while (!sent_comm.empty()) {
98         std::for_each(sent_comm.begin(), sent_comm.end(),
99                       std::tr1::bind(xbt_dynar_push,
100                                      comms, std::tr1::placeholders::_1));
101         MSG_comm_waitany(comms);
102         xbt_dynar_reset(comms);
103         flush_sent();
104     }
105     xbt_dynar_free(&comms);
106 }
107
108 void communicator::next_close_on_ctrl_is_last()
109 {
110     ctrl_close_is_last = true;
111 }
112
113 void communicator::next_close_on_data_is_last()
114 {
115     data_close_is_last = true;
116 }
117
118 int communicator::send_backlog()
119 {
120     flush_sent();
121     return sent_comm.size();
122 }
123
124 bool communicator::comm_test_n_destroy(msg_comm_t& comm)
125 {
126     if (MSG_comm_test(comm)) {
127         MSG_comm_destroy(comm);
128         return true;
129     } else
130         return false;
131 }
132
133 void communicator::flush_sent()
134 {
135     std::remove_if(sent_comm.begin(), sent_comm.end(), comm_test_n_destroy);
136 }
137
138 // Local variables:
139 // mode: c++
140 // End: