Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5f7b902494baf4846851ce6b3c448d9d1624733d
[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 "src/msg/msg_private.h"
9 #include "src/simix/smx_process_private.h"
10
11 #include "simgrid/s4u/host.hpp"
12 #include "simgrid/s4u/storage.hpp"
13
14 namespace simgrid {
15 namespace s4u {
16
17 boost::unordered_map<std::string, Host*> *Host::hosts
18                 = new boost::unordered_map<std::string, Host*>();
19
20 Host::Host(const char*name)
21 {
22         p_inferior = sg_host_by_name(name);
23         if (p_inferior==NULL)
24                 xbt_die("No such host: %s",name); //FIXME: raise an exception
25 }
26 Host::~Host() {
27         if (mounts != NULL)
28                 delete mounts;
29 }
30
31 Host *Host::byName(std::string name) {
32         Host * res = NULL;
33         try {
34                 res = hosts->at(name);
35         } catch (std::out_of_range& e) {}
36
37         if (res==NULL) {
38                 res = new Host(name.c_str());
39                 hosts->insert({name,res});
40         }
41         return res;
42 }
43 Host *Host::current(){
44         smx_process_t smx_proc = SIMIX_process_self();
45         if (smx_proc == NULL)
46                 xbt_die("Cannot call Host::current() from the maestro context");
47
48         return Host::byName(sg_host_get_name(SIMIX_process_get_host(smx_proc)));
49 }
50
51 const char* Host::name() {
52         return sg_host_get_name(p_inferior);
53 }
54
55 void Host::turnOn() {
56         p_inferior->on();
57 }
58 void Host::turnOff() {
59         p_inferior->off();
60 }
61 bool Host::isOn() {
62         return sg_host_is_on(p_inferior);
63 }
64
65 boost::unordered_map<std::string, Storage&> &Host::mountedStorages() {
66         if (mounts == NULL) {
67                 mounts = new boost::unordered_map<std::string, Storage&> ();
68
69                 xbt_dict_t dict = p_inferior->getMountedStorageList();
70
71                 xbt_dict_cursor_t cursor;
72                 char *mountname;
73                 char *storagename;
74                 xbt_dict_foreach(dict, cursor, mountname, storagename) {
75                         mounts->insert({mountname, Storage::byName(storagename)});
76                 }
77                 xbt_dict_free(&dict);
78         }
79
80         return *mounts;
81 }
82
83
84 } // namespace simgrid
85 } // namespace s4u