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

Private GIT Repository
Wip...
[loba.git] / communicator.cpp
1 #include <algorithm>
2 #include <cstring>
3 #include <msg/msg.h>
4 #include <xbt/log.h>
5 #include "communicator.h"
6
7 // XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu);
8 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(comm, simu,
9                                 "Messages from asynchronous pipes");
10
11 namespace {
12     bool comm_test_n_destroy(msg_comm_t& comm)
13     {
14         if (MSG_comm_test(comm)) {
15             MSG_comm_destroy(comm);
16             return true;
17         } else
18             return false;
19     }
20 }
21
22 communicator::communicator()
23 {
24     const char* hostname = MSG_host_get_name(MSG_host_self());
25     size_t len = std::strlen(hostname);
26     recv_mbox = new char[len + 1];
27     strcpy(recv_mbox, hostname);
28     recv_task = NULL;
29     recv_comm = MSG_task_irecv(&recv_task, recv_mbox);
30 }
31
32 communicator::~communicator()
33 {
34     send_acknowledge();
35     if (!sent_comm.empty())
36         WARN1("Lost %ld send communications!", (long )sent_comm.size());
37     delete[] recv_mbox;
38 }
39
40
41 void communicator::send(m_task_t task, const char *dest)
42 {
43     sent_comm.push_back(MSG_task_isend(task, dest));
44     send_acknowledge();
45 }
46
47 void communicator::send(m_task_t task, const std::string& dest)
48 {
49     send(task, dest.c_str());
50 }
51
52 m_task_t communicator::recv()
53
54    m_task_t task = NULL;
55    if (comm_test_n_destroy(recv_comm)) {
56         task = recv_task;
57         recv_task = NULL;
58         recv_comm = MSG_task_irecv(&recv_task, recv_mbox);
59     }
60     return task;
61 }
62
63 int communicator::sent_count()
64 {
65     send_acknowledge();
66     return sent_comm.size();
67 }
68
69 void communicator::send_acknowledge()
70 {
71     std::remove_if(sent_comm.begin(), sent_comm.end(), comm_test_n_destroy);
72 }
73
74 // Local variables:
75 // mode: c++
76 // End: