Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0de3ffa898360398ec343ecef91bfd661bb13eb9
[simgrid.git] / src / bindings / ruby / rb_msg_task.c
1 /*
2  * Copyright 2010. The SimGrid Team. All right reserved.
3  *
4  * This program is free software; you can redistribute 
5  * it and/or modify it under the terms of the license 
6  *(GNU LGPL) which comes with this package. 
7  */
8 #include "bindings/ruby_bindings.h"
9
10 // Free Method
11 void rb_task_free(m_task_t tk) {
12   MSG_task_destroy(tk);
13 }
14
15 // New Method
16 VALUE rb_task_new(VALUE class, VALUE name,VALUE comp_size,VALUE comm_size) {
17   //char * t_name = RSTRING(name)->ptr;
18   m_task_t task = MSG_task_create(RSTRING(name)->ptr,NUM2INT(comp_size),NUM2INT(comm_size),NULL);
19   // Wrap m_task_t to a Ruby Value
20   return Data_Wrap_Struct(class, 0, rb_task_free, task);
21
22 }
23
24 //Get Computation Size
25 VALUE rb_task_comp(VALUE class,VALUE task) {
26   double size;
27   m_task_t tk;
28   // Wrap Ruby Value to m_task_t struct
29   Data_Get_Struct(task, s_m_task_t, tk);
30   size = MSG_task_get_compute_duration(tk);
31   return rb_float_new(size);
32 }
33
34 //Get Name
35 VALUE rb_task_name(VALUE class,VALUE task) {
36   
37   // Wrap Ruby Value to m_task_t struct
38   m_task_t tk;
39   Data_Get_Struct(task, s_m_task_t, tk);
40   return rb_str_new2(MSG_task_get_name(tk));
41 }
42
43 // Execute Task
44 VALUE rb_task_execute(VALUE class,VALUE task) {
45   
46   // Wrap Ruby Value to m_task_t struct
47   m_task_t tk;
48   Data_Get_Struct(task, s_m_task_t, tk);
49   return INT2NUM(MSG_task_execute(tk));
50 }
51
52 // Sending Task
53 void rb_task_send(VALUE class,VALUE task,VALUE mailbox) {
54   
55   // Wrap Ruby Value to m_task_t struct
56   m_task_t tk;
57   Data_Get_Struct(task, s_m_task_t, tk);
58   int res = MSG_task_send(tk,RSTRING(mailbox)->ptr);
59   if(res != MSG_OK)
60    rb_raise(rb_eRuntimeError,"MSG_task_send failed");
61 }
62
63 // Receiving Task (returns a Task)
64 VALUE rb_task_receive(VALUE class,VALUE mailbox) {
65   // Task
66   m_task_t task = NULL;
67   MSG_task_receive(&task,RSTRING(mailbox)->ptr);
68   return Data_Wrap_Struct(class, 0, rb_task_free, task);
69 }
70
71 // Recieve Task 2
72 // Not Appreciated 
73 void rb_task_receive2(VALUE class,VALUE task,VALUE mailbox) {
74   m_task_t tk;
75   Data_Get_Struct(task, s_m_task_t, tk);
76   MSG_task_receive(&tk,RSTRING(mailbox)->ptr);
77 }
78
79 // It Return a Native Process ( m_process_t )
80 VALUE rb_task_sender(VALUE class,VALUE task) {
81   m_task_t tk;
82   Data_Get_Struct(task,s_m_task_t,tk);
83   THROW_UNIMPLEMENTED;
84   return 0;//MSG_task_get_sender(tk);
85 }
86
87 // it return a Host 
88 VALUE rb_task_source(VALUE class,VALUE task) {
89   m_task_t tk;
90   Data_Get_Struct(task,s_m_task_t,tk);
91   
92   m_host_t host = MSG_task_get_source(tk);
93   if(!host->data) {
94     rb_raise(rb_eRuntimeError,"MSG_task_get_source() failed");
95     return Qnil;
96   }
97   THROW_UNIMPLEMENTED;
98   return 0;//host;
99 }
100
101 // Return Boolean
102 VALUE rb_task_listen(VALUE class,VALUE task,VALUE alias) {
103  m_task_t tk;
104  const char *p_alias;
105  int rv;
106  
107  Data_Get_Struct(task,s_m_task_t,tk);
108  p_alias = RSTRING(alias)->ptr;
109  
110  rv = MSG_task_listen(p_alias);
111  
112  if(rv) return Qtrue;
113  
114  return Qfalse;
115 }
116
117 // return Boolean
118 VALUE rb_task_listen_host(VALUE class,VALUE task,VALUE alias,VALUE host) {
119   
120  m_task_t tk;
121  m_host_t ht;
122  const char *p_alias;
123  int rv;
124  
125  Data_Get_Struct(task,s_m_task_t,tk);
126  Data_Get_Struct(host,s_m_host_t,ht);
127  p_alias = RSTRING(alias)->ptr;
128  
129  rv = MSG_task_listen_from_host(p_alias,ht);
130  
131  if (rv)
132    return Qtrue;
133  return Qfalse;
134 }
135
136