Logo AND Algorithmique Numérique Distribuée

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