#include "hostdata.h"
-#include <xbt/log.h>
+#include <cstring>
+#include <algorithm>
#include <stdexcept>
+#include <xbt/log.h>
+#include "misc.h"
-XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simu);
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(main);
-hostdata* hostdata::instances = NULL;
+std::vector<hostdata> hostdata::hosts;
+
+// used to compare m_host_t's by name
+struct hostdata::m_host_less {
+ bool operator()(const m_host_t& a, const m_host_t& b)
+ {
+ const char* na = MSG_host_get_name(a);
+ const char* nb = MSG_host_get_name(b);
+ return strcmp(na, nb) < 0;
+ }
+};
void hostdata::create()
{
int nhosts = MSG_get_host_number();
m_host_t* host_list = MSG_get_host_table();
- VERB1("Got %d hosts.", nhosts);
+ // fixme: only sort hosts for automatically created deployment
+ // fixme: add an option to disable sorting
+ std::sort(host_list, host_list + nhosts, m_host_less());
+ hosts.assign(host_list, host_list + nhosts);
+ xbt_free(host_list);
+
+ e_xbt_log_priority_t logp = xbt_log_priority_verbose;
+ if (!LOG_ISENABLED(logp))
+ return;
+ LOG1(logp, "Got %lu hosts.", (unsigned long)hosts.size());
for (int i = 0; i < nhosts; i++) {
- hostdata* h = new hostdata(host_list[i]);
- MSG_host_set_data(host_list[i], h);
- VERB2("Host #%d named \"%s\".", i, h->get_name());
- h->next = instances;
- instances = h;
+ LOG2(logp, "Host #%d named \"%s\".", i, hosts[i].get_name());
}
- xbt_free(host_list);
}
void hostdata::destroy()
{
- while (instances) {
- hostdata* h = instances;
- instances = h->next;
- delete h;
- }
+ // hosts are automatically destroyed...
}
hostdata::hostdata(m_host_t host)
- : next(NULL)
- , name(MSG_host_get_name(host))
+ : name(MSG_host_get_name(host))
, ctrl_mbox(std::string(name) + "_ctrl")
, data_mbox(std::string(name) + "_data")
{
+ MSG_host_set_data(host, this);
}
hostdata::~hostdata()