Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
some more cleanups in ruby bindings. Still not working damnit (but getting clearer)
[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   int res = MSG_task_send(tk,RSTRING(mailbox)->ptr);
62   if(res != MSG_OK)
63     rb_raise(rb_eRuntimeError,"MSG_task_send failed");
64 }
65
66 // Receiving Task (returns a Task)
67 VALUE rb_task_receive(VALUE class, VALUE mailbox) {
68   // Task
69   m_task_t task = NULL;
70   INFO1("Receiving a task on mailbox '%s'",RSTRING(mailbox)->ptr);
71   MSG_task_receive(&task,RSTRING(mailbox)->ptr);
72   INFO2("XXXXXXXXReceived a task %p %s",task,task->name);
73   return Data_Wrap_Struct(class, 0, rb_task_free, task);
74 }
75
76 // It Return a Native Process ( m_process_t )
77 VALUE rb_task_sender(VALUE class,VALUE task) {
78   m_task_t tk;
79   Data_Get_Struct(task,s_m_task_t,tk);
80   THROW_UNIMPLEMENTED;
81   return 0;//MSG_task_get_sender(tk);
82 }
83
84 // it return a Host 
85 VALUE rb_task_source(VALUE class,VALUE task) {
86   m_task_t tk;
87   Data_Get_Struct(task,s_m_task_t,tk);
88
89   m_host_t host = MSG_task_get_source(tk);
90   if(!host->data) {
91     rb_raise(rb_eRuntimeError,"MSG_task_get_source() failed");
92     return Qnil;
93   }
94   THROW_UNIMPLEMENTED;
95   return 0;//host;
96 }
97
98 // Return Boolean
99 VALUE rb_task_listen(VALUE class,VALUE task,VALUE alias) {
100   m_task_t tk;
101   const char *p_alias;
102   int rv;
103
104   Data_Get_Struct(task,s_m_task_t,tk);
105   p_alias = RSTRING(alias)->ptr;
106
107   rv = MSG_task_listen(p_alias);
108
109   if(rv) return Qtrue;
110
111   return Qfalse;
112 }
113
114 // return Boolean
115 VALUE rb_task_listen_host(VALUE class,VALUE task,VALUE alias,VALUE host) {
116
117   m_task_t tk;
118   m_host_t ht;
119   const char *p_alias;
120   int rv;
121
122   Data_Get_Struct(task,s_m_task_t,tk);
123   Data_Get_Struct(host,s_m_host_t,ht);
124   p_alias = RSTRING(alias)->ptr;
125
126   rv = MSG_task_listen_from_host(p_alias,ht);
127
128   if (rv)
129     return Qtrue;
130   return Qfalse;
131 }