Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9334a12bd411dc55c0d4c404b2a2b86542729c79
[simgrid.git] / src / bindings / ruby / rb_msg_task.c
1 /*
2  * $Id$
3  *
4  * Copyright 2010 Martin Quinson, Mehdi Fekari           
5  * All right reserved. 
6  *
7  * This program is free software; you can redistribute 
8  * it and/or modify it under the terms of the license 
9  *(GNU LGPL) which comes with this package. 
10  */
11 #include "rb_msg_task.h"
12
13 // Free Method
14 static void task_free(m_task_t tk) {
15   MSG_task_destroy(tk);
16 }
17
18 // New Method
19 static VALUE task_new(VALUE class, VALUE name,VALUE comp_size,VALUE comm_size)
20 {
21   
22   //char * t_name = RSTRING(name)->ptr;
23   m_task_t task = MSG_task_create(RSTRING(name)->ptr,NUM2INT(comp_size),NUM2INT(comm_size),NULL);
24   // Wrap m_task_t to a Ruby Value
25   return Data_Wrap_Struct(class, 0, task_free, task);
26
27 }
28
29 //Get Computation Size
30 static VALUE task_comp(VALUE class,VALUE task)
31 {
32   double size;
33   m_task_t tk;
34   // Wrap Ruby Value to m_task_t struct
35   Data_Get_Struct(task, m_task_t, tk);
36   size = MSG_task_get_compute_duration(tk);
37   return rb_float_new(size);
38 }
39
40 //Get Name
41 static VALUE task_name(VALUE class,VALUE task)
42 {
43   
44   // Wrap Ruby Value to m_task_t struct
45   m_task_t tk;
46   Data_Get_Struct(task, m_task_t, tk);
47   return rb_str_new2(MSG_task_get_name(tk));
48    
49 }
50
51 // Execute Task
52 static VALUE task_execute(VALUE class,VALUE task)
53 {
54   
55   // Wrap Ruby Value to m_task_t struct
56   m_task_t tk;
57   Data_Get_Struct(task, m_task_t, tk);
58   return INT2NUM(MSG_task_execute(tk));
59   
60 }
61
62 // Sending Task
63 static void task_send(VALUE class,VALUE task,VALUE mailbox)
64 {
65   
66   // Wrap Ruby Value to m_task_t struct
67   m_task_t tk;
68   Data_Get_Struct(task, m_task_t, tk);
69   int res = MSG_task_send(tk,RSTRING(mailbox)->ptr);
70   if(res != MSG_OK)
71    rb_raise(rb_eRuntimeError,"MSG_task_send failed");
72   
73   return;
74 }
75
76 // Recieving Task 
77
78 /**
79 *It Return a Task 
80 */
81
82 static VALUE task_receive(VALUE class,VALUE mailbox)
83 {
84   // Task
85   m_task_t task = NULL;
86   MSG_task_receive(&task,RSTRING(mailbox)->ptr);
87   return Data_Wrap_Struct(class, 0, task_free, task);
88 }
89
90 // Recieve Task 2
91 // Not Appreciated 
92 static void task_receive2(VALUE class,VALUE task,VALUE mailbox)
93 {
94   m_task_t tk;
95   Data_Get_Struct(task, m_task_t, tk);
96   MSG_task_receive(&tk,RSTRING(mailbox)->ptr);
97   
98 }
99
100 // It Return a Native Process ( m_process_t )
101 static VALUE task_sender(VALUE class,VALUE task)
102 {
103   m_task_t tk;
104   Data_Get_Struct(task,m_task_t,tk);
105   return MSG_task_get_sender(tk);
106   
107 }
108
109 // it return a Host 
110 static VALUE task_source(VALUE class,VALUE task)
111 {
112   m_task_t tk;
113   Data_Get_Struct(task,m_task_t,tk);
114   
115   m_host_t host = MSG_task_get_source(tk);
116   if(!host->data)
117   {
118     rb_raise(rb_eRuntimeError,"MSG_task_get_source() failed");
119     return Qnil;
120   }
121   return host;
122   
123 }
124
125 // Return Boolean
126 static VALUE task_listen(VALUE class,VALUE task,VALUE alias)
127 {
128  m_task_t tk;
129  const char *p_alias;
130  int rv;
131  
132  Data_Get_Struct(task,m_task_t,tk);
133  p_alias = RSTRING(alias)->ptr;
134  
135  rv = MSG_task_listen(p_alias);
136  
137  if(rv) return Qtrue;
138  
139  return Qfalse;
140
141 }
142
143 // return Boolean
144 static VALUE task_listen_host(VALUE class,VALUE task,VALUE alias,VALUE host)
145 {
146   
147  m_task_t tk;
148  m_host_t ht;
149  const char *p_alias;
150  int rv;
151  
152  Data_Get_Struct(task,m_task_t,tk);
153  Data_Get_Struct(host,m_host_t,ht);
154  p_alias = RSTRING(alias)->ptr;
155  
156  rv = MSG_task_listen_from_host(p_alias,ht);
157  
158  if (rv) return Qtrue;
159  
160  return Qfalse;
161  
162 }
163
164
165 // Put
166 static void task_put(VALUE class,VALUE task,VALUE host)
167 {
168   
169  m_task_t tk;
170  m_host_t ht;
171  
172  Data_Get_Struct(task,m_task_t,tk);
173  Data_Get_Struct(host,m_host_t,ht);
174  MSG_task_put(tk,ht,PORT_22);  //Channel set to 0
175  
176 }
177
178 //get 
179 static VALUE task_get(VALUE class)
180 {
181   
182  m_task_t task = NULL;
183  int res = MSG_task_get(&task,PORT_22); // Channel set to 0
184  xbt_assert0(res == MSG_OK, "MSG_task_get failed");
185  return Data_Wrap_Struct(class, 0, task_free, task);
186  
187 }