Logo AND Algorithmique Numérique Distribuée

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