Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Ruby Msg Binding : new Methods and new Example
[simgrid.git] / src / bindings / ruby / rb_msg_process.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
9 #include "msg/private.h" /* s_simdata_process_t */
10 #include "bindings/ruby_bindings.h"
11
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ruby,bindings,"Ruby Bindings");
13
14 /*
15  * Functions for Ruby Process Management (Up Calls)
16  */
17
18 // get Ruby Process Name
19 VALUE rb_process_getName(VALUE ruby_process) {
20   return rb_funcall(ruby_process,rb_intern("getName"),0);
21 }
22
23 // Get  Process ID
24 VALUE rb_process_getID(VALUE ruby_process) {
25   return rb_funcall(ruby_process,rb_intern("getID"),0);
26 }
27
28 // Get Bind
29 VALUE rb_process_getBind(VALUE ruby_process) {
30   return rb_funcall(ruby_process,rb_intern("getBind"),0);
31 }
32
33 // Set Bind
34 void rb_process_setBind(VALUE ruby_process,long bind) {
35   VALUE r_bind = LONG2FIX(bind);
36   rb_funcall(ruby_process,rb_intern("setBind"),1,r_bind);
37 }
38
39 // isAlive
40 VALUE rb_process_isAlive(VALUE ruby_process) {
41   return rb_funcall(ruby_process,rb_intern("alive?"),0);
42 }
43
44 // Kill Process
45 void rb_process_kill_up(VALUE ruby_process) {
46   rb_funcall(ruby_process,rb_intern("kill"),0);
47 }
48
49 // join Process
50 void rb_process_join( VALUE ruby_process ) {
51   rb_funcall(ruby_process,rb_intern("join"),0);
52 }
53
54 // unschedule Process
55 void rb_process_unschedule( VALUE ruby_process ) {
56   rb_funcall(ruby_process,rb_intern("unschedule"),0);
57 }
58
59 // schedule Process
60 void rb_process_schedule( VALUE ruby_process ) {
61   rb_funcall(ruby_process,rb_intern("schedule"),0);
62 }
63
64 /***************************************************
65 Function for Native Process ( Bound ) Management
66
67 Methods Belong to MSG Module
68 ****************************************************/
69
70 // Process To Native
71 m_process_t rb_process_to_native(VALUE ruby_process) {
72   VALUE id = rb_process_getBind(ruby_process);
73   if (!id) {
74     rb_raise(rb_eRuntimeError,"Process Not Bound >>> id_Bind Null");
75     return NULL;
76   }
77   long l_id= FIX2LONG(id);
78   return (m_process_t)l_id;
79 }
80
81 // Bind Process
82 void rb_process_bind(VALUE ruby_process,m_process_t process) {
83   long bind = (long)(process);
84   rb_process_setBind(ruby_process,bind);
85 }
86
87
88 // Process Management
89 void rb_process_suspend(VALUE class,VALUE ruby_process) {
90
91   m_process_t process = rb_process_to_native(ruby_process);
92
93   if (!process) {
94     rb_raise(rb_eRuntimeError,"Process Not Bound...while suspending process");
95     return;  
96   }
97
98   // Trying to suspend The Process
99
100   if ( MSG_OK != MSG_process_suspend(process))
101     rb_raise(rb_eRuntimeError,"MSG_process_suspend() failed");
102 }
103
104 void rb_process_resume(VALUE class,VALUE ruby_process) {
105   m_process_t process = rb_process_to_native(ruby_process);
106   if (!process) {
107     rb_raise(rb_eRuntimeError,"Process not Bound...while resuming process");
108     return ;
109   }
110
111   // Trying to resume the process
112   if ( MSG_OK != MSG_process_resume(process))
113     rb_raise(rb_eRuntimeError,"MSG_process_resume() failed");
114 }
115
116 VALUE rb_process_isSuspended(VALUE class,VALUE ruby_process) {
117   m_process_t process = rb_process_to_native(ruby_process);
118   if (!process) {
119     rb_raise (rb_eRuntimeError,"Process not Bound...while testing if suspended");
120     return Qfalse;
121   }
122   if(MSG_process_is_suspended(process))
123     return Qtrue;
124   return Qfalse;
125 }
126
127 void rb_process_kill_down(VALUE class,VALUE ruby_process) {
128   m_process_t process = rb_process_to_native(ruby_process);
129
130   if(!process) {
131     rb_raise (rb_eRuntimeError,"Process Not Bound...while killing process");
132     return;
133   }
134   // Delete The Global Reference / Ruby Process
135   rb_process_kill_up(ruby_process);
136   // Delete the Native Process
137   MSG_process_kill(process);
138 }
139
140 VALUE rb_process_getHost(VALUE class,VALUE ruby_process) {
141   m_process_t process = rb_process_to_native(ruby_process);
142   m_host_t host;
143   
144
145   if (!process) {
146     rb_raise(rb_eRuntimeError,"Process Not Bound...while getting Host");
147     return Qnil; // NULL
148   }
149   
150   host = MSG_process_get_host(process);
151   
152   return Data_Wrap_Struct(class, 0, rb_host_free, host);
153   /*if(host->data) printf("Ok\n"); 
154  
155   if(!host->data) {
156     rb_raise (rb_eRuntimeError,"MSG_process_get_host() failed");
157     return Qnil;
158   }
159   printf("Houuuuuuuuuuuuuuna3!!\n");
160   return Data_Wrap_Struct(class, 0, rb_host_free, host);*/
161 }
162
163 void rb_process_exit(VALUE class,VALUE ruby_process) {
164   m_process_t process = rb_process_to_native(ruby_process);
165   if(!process) {
166     rb_raise(rb_eRuntimeError,"Process Not Bound...while exiting process");
167     return;
168   }
169   SIMIX_context_stop(SIMIX_process_self()->context);
170 }