Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
props dict does not always exists anymore. Check if not NULL before
[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 {
14   //Nothing to do !!?
15 }
16
17 // New Method : return a Host
18 VALUE rb_host_get_by_name(VALUE class, VALUE name)
19 {
20
21   const char *h_name = RSTRING_PTR(name);
22   m_host_t host = MSG_get_host_by_name(h_name);
23
24   if (!host){
25     char *message = bprintf("No host called '%s' found", h_name);
26     VALUE errorobj = rb_exc_new2(rb_eRuntimeError, message);
27     xbt_free(message);
28     rb_exc_raise(errorobj);
29   }
30   return Data_Wrap_Struct(class, 0, rb_host_free, host);
31 }
32
33 //Get Name
34 VALUE rb_host_name(VALUE class, VALUE host)
35 {
36
37   // Wrap Ruby Value to m_host_t struct
38   m_host_t ht;
39   Data_Get_Struct(host, s_m_host_t, ht);
40   return rb_str_new2(MSG_host_get_name(ht));
41
42 }
43
44 // Get Number
45 VALUE rb_host_number(VALUE class)
46 {
47   return INT2NUM(MSG_get_host_number());
48 }
49
50 // Host Speed ( Double )
51 VALUE rb_host_speed(VALUE class, VALUE host)
52 {
53   m_host_t ht;
54   Data_Get_Struct(host, s_m_host_t, ht);
55   return MSG_get_host_speed(ht);
56 }
57
58 // Host is Avail
59 VALUE rb_host_is_avail(VALUE class, VALUE host)
60 {
61   m_host_t ht;
62   Data_Get_Struct(host, s_m_host_t, ht);
63   if (!ht) {
64     rb_raise(rb_eRuntimeError, "Host not Bound");
65     return Qnil;
66   }
67
68   if (MSG_host_is_avail(ht))
69     return Qtrue;
70
71   return Qfalse;
72 }
73
74 // getHost from process
75 VALUE rb_host_process(VALUE class, VALUE ruby_process)
76 {
77
78   m_process_t process = rb_process_to_native(ruby_process);
79   m_host_t host;
80
81
82   if (!process) {
83     rb_raise(rb_eRuntimeError, "Process Not Bound...while getting Host");
84     return Qnil;                // NULL
85   }
86
87   host = MSG_process_get_host(process);
88
89   return Data_Wrap_Struct(class, 0, rb_host_free, host);
90
91 }
92
93 // get all hosts
94 VALUE rb_host_get_all_hosts(VALUE class)
95 {
96   int nb, index;
97   m_host_t *hosts;
98   VALUE rb_hosts;
99   nb = MSG_get_host_number();
100   hosts = MSG_get_host_table();
101   rb_hosts = rb_ary_new2(nb);
102
103   for (index = 0; index < nb; index++)
104     rb_ary_push(rb_hosts,
105                 Data_Wrap_Struct(class, 0, rb_host_free, hosts[index]));
106
107   return rb_hosts;
108 }