Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
implement s4u::Comm::recv_async()
[simgrid.git] / src / s4u / s4u_host.cpp
1 /* Copyright (c) 2006-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "xbt/log.h"
8 #include "msg/msg_private.h"
9 #include "simix/smx_process_private.h"
10
11 #include "simgrid/s4u/host.hpp"
12
13 using namespace simgrid;
14
15 boost::unordered_map<std::string, simgrid::s4u::Host*> *simgrid::s4u::Host::hosts
16                 = new boost::unordered_map<std::string, simgrid::s4u::Host*>();
17
18 s4u::Host::Host(const char*name) {
19         p_sghost = sg_host_by_name(name);
20         if (p_sghost==NULL)
21                 xbt_die("No such host: %s",name); //FIXME: raise an exception
22 }
23
24 s4u::Host *s4u::Host::byName(std::string name) {
25         s4u::Host * res = NULL;
26         try {
27                 res = hosts->at(name);
28         } catch (std::out_of_range& e) {}
29
30         if (res==NULL) {
31                 res = new s4u::Host(name.c_str());
32                 hosts->insert({name,res});
33         }
34         return res;
35 }
36 s4u::Host *s4u::Host::current(){
37         smx_process_t smx_proc = SIMIX_process_self();
38         if (smx_proc == NULL)
39                 xbt_die("Cannot call Host::current() from the maestro context");
40
41         return s4u::Host::byName(SIMIX_host_get_name(SIMIX_process_get_host(smx_proc)));
42 }
43
44 const char* s4u::Host::getName() {
45         return sg_host_name(p_sghost);
46 }
47
48 void s4u::Host::turnOn() {
49         simcall_host_on(p_sghost);
50 }
51 void s4u::Host::turnOff() {
52         simcall_host_off(p_sghost);
53 }
54 bool s4u::Host::isOn() {
55         return simcall_host_get_state(p_sghost);
56 }