Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix copyright headers
[simgrid.git] / src / bindings / ruby / rb_msg_host.c
1 /* Host-related bindings to ruby  */
2
3 /* Copyright (c) 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "bindings/ruby_bindings.h"
10
11 // Free Method
12 void rb_host_free(m_host_t ht) {
13   //Nothing to do !!?
14 }
15
16 // New Method : return a Host
17 VALUE rb_host_get_by_name(VALUE class, VALUE name) {
18
19   const char * h_name = RSTRING(name)->ptr;
20   m_host_t host = MSG_get_host_by_name(h_name);
21   if(!host)
22     rb_raise(rb_eRuntimeError,bprintf("No host called '%s' found",h_name));
23
24   return Data_Wrap_Struct(class,0,rb_host_free,host);
25 }
26
27 //Get Name
28 VALUE rb_host_name(VALUE class,VALUE host) {
29
30   // Wrap Ruby Value to m_host_t struct
31   m_host_t ht;
32   Data_Get_Struct(host, s_m_host_t, ht);
33   return rb_str_new2(MSG_host_get_name(ht));
34
35 }
36
37 // Get Number
38 VALUE rb_host_number(VALUE class) {
39   return INT2NUM(MSG_get_host_number());
40 }
41
42 // Host Speed ( Double )
43 VALUE rb_host_speed(VALUE class,VALUE host) {
44   m_host_t ht ;
45   Data_Get_Struct(host,s_m_host_t,ht);
46   return MSG_get_host_speed(ht);
47 }
48
49 // Host is Avail
50 VALUE rb_host_is_avail(VALUE class,VALUE host) {
51   m_host_t ht;
52   Data_Get_Struct(host,s_m_host_t,ht);
53   if (!ht) {
54     rb_raise(rb_eRuntimeError,"Host not Bound");
55     return Qnil;
56   }
57
58   if(MSG_host_is_avail(ht))
59     return Qtrue;
60
61   return Qfalse;
62 }
63
64 // getHost from process
65 VALUE rb_host_process(VALUE class,VALUE ruby_process)
66 {
67   
68  m_process_t process = rb_process_to_native(ruby_process);
69   m_host_t host;
70   
71
72   if (!process) {
73     rb_raise(rb_eRuntimeError,"Process Not Bound...while getting Host");
74     return Qnil; // NULL
75   }
76   
77   host = MSG_process_get_host(process);
78   
79   return Data_Wrap_Struct(class, 0, rb_host_free, host); 
80
81 }
82
83 // get all hosts
84 VALUE rb_host_get_all_hosts(VALUE class)
85 {
86         int nb,index;
87         m_host_t *hosts;
88         VALUE rb_hosts;
89         nb = MSG_get_host_number();
90         hosts  = MSG_get_host_table();
91     rb_hosts = rb_ary_new2(nb);
92
93     for(index=0 ; index<nb; index++)
94         rb_ary_push(rb_hosts,Data_Wrap_Struct(class, 0, rb_host_free, hosts[index]));
95
96     return rb_hosts;
97 }
98