erlix 0.5.0a

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,47 @@
1
+ /*
2
+ * project : erlix
3
+ * author : kdr2
4
+ *
5
+ */
6
+
7
+ #ifndef ERLIX_CONNECTION_H_
8
+ #define ERLIX_CONNECTION_H_
9
+
10
+ #include "ruby.h"
11
+
12
+ #include "ei.h"
13
+ #include "erl_interface.h"
14
+
15
+ #include "erlix_term.h"
16
+ #include "erlix_node.h"
17
+
18
+ #define ERLIX_CONNECTION_ALIVE(cp) ((cp->status&0x01)==0x01)
19
+ #define ERLIX_CONNECTION_DEAD(cp) (cp->status&=0xfe)
20
+ #define ERLIX_CONNECTION_BORN(cp) (cp->status|=0x01)
21
+
22
+ typedef struct _erlix_connection{
23
+ unsigned char status;
24
+ VALUE peer_node;
25
+ int sock_fd;
26
+ }ErlixConnection;
27
+
28
+
29
+ ErlixConnection* new_erlix_connection();
30
+ void free_erlix_connection(void *econ);
31
+
32
+ static VALUE erlix_connection_alloc(VALUE klass);
33
+ static VALUE erlix_connection_init(VALUE self,VALUE pnode);
34
+ static VALUE erlix_connection_init_copy(VALUE copy,VALUE orig);
35
+ static VALUE erlix_connection_to_str(VALUE self);
36
+ static VALUE erlix_connection_peer(VALUE self);
37
+ static VALUE erlix_connection_etype(VALUE self);
38
+ static VALUE erlix_connection_mkpid(VALUE self);
39
+ static VALUE erlix_connection_send(VALUE self,VALUE pid,VALUE term);
40
+ static VALUE erlix_connection_recv(VALUE self);
41
+ static VALUE erlix_connection_rpc(VALUE self,VALUE module,VALUE func,VALUE args);
42
+ static VALUE erlix_connection_close(VALUE self);
43
+
44
+
45
+ void init_erlix_connection();
46
+
47
+ #endif /* ERLIX_CONNECTION_H_ */
@@ -0,0 +1,65 @@
1
+ /*
2
+ * project : erlix
3
+ * author : kdr2
4
+ *
5
+ */
6
+
7
+ #include <stdio.h>
8
+ #include <string.h>
9
+
10
+ #include "erlix_term.h"
11
+
12
+ VALUE erlix_cErlixFloat;
13
+
14
+ VALUE erlix_float_alloc(VALUE klass){
15
+ ErlixTerm* efloat;
16
+ VALUE obj;
17
+ efloat=new_erlix_term();
18
+ efloat->type=TYPE_FLOAT;
19
+ obj=Data_Wrap_Struct(klass,0,free_erlix_term,efloat);
20
+ return obj;
21
+ }
22
+
23
+ static VALUE erlix_float_init(VALUE self,VALUE fix){
24
+ ErlixTerm* efloat;
25
+ double i=NUM2DBL(fix);
26
+ Data_Get_Struct(self,ErlixTerm,efloat);
27
+ efloat->term=erl_mk_float(i);
28
+ return self;
29
+ }
30
+
31
+
32
+ static VALUE erlix_float_to_fix(VALUE self){
33
+ ErlixTerm *efloat;
34
+ double l;
35
+ Data_Get_Struct(self,ErlixTerm,efloat);
36
+ l=ERL_FLOAT_VALUE(efloat->term);
37
+ return rb_float_new(l);
38
+ }
39
+
40
+ static VALUE erlix_float_inspect(VALUE self){
41
+ VALUE ret=rb_str_new2("#<Erlix::Float:");
42
+ ID concat=rb_intern("concat");
43
+ rb_funcall(ret,concat,1,rb_funcall(self,rb_intern("to_s"),0));
44
+ rb_funcall(ret,concat,1,rb_str_new2(">"));
45
+ return ret;
46
+ }
47
+
48
+ static VALUE erlix_float_etype(VALUE self){
49
+ return rb_str_new2("float");
50
+ }
51
+
52
+ void init_erlix_float(VALUE erlix){
53
+ erlix_cErlixFloat=rb_define_class_under(erlix,"Float",rb_cObject);
54
+
55
+ rb_define_alloc_func(erlix_cErlixFloat,erlix_float_alloc);
56
+ rb_define_method(erlix_cErlixFloat,"initialize",erlix_float_init,1);
57
+ rb_define_method(erlix_cErlixFloat,"to_i",erlix_float_to_fix,0);
58
+ rb_define_method(erlix_cErlixFloat,"inspect",erlix_float_inspect,0);
59
+ rb_define_method(erlix_cErlixFloat,"etype",erlix_float_etype,0);
60
+
61
+ rb_include_module(erlix_cErlixFloat,erlix_mErlixTerm);
62
+ }
63
+
64
+
65
+
@@ -0,0 +1,64 @@
1
+ /*
2
+ * project : erlix
3
+ * author : kdr2
4
+ *
5
+ */
6
+
7
+ #include <stdio.h>
8
+ #include <string.h>
9
+
10
+ #include "erlix_term.h"
11
+
12
+ VALUE erlix_cErlixInt;
13
+
14
+ VALUE erlix_int_alloc(VALUE klass){
15
+ ErlixTerm* eint;
16
+ VALUE obj;
17
+ eint=new_erlix_term();
18
+ eint->type=TYPE_INT;
19
+ obj=Data_Wrap_Struct(klass,0,free_erlix_term,eint);
20
+ return obj;
21
+ }
22
+
23
+ static VALUE erlix_int_init(VALUE self,VALUE fix){
24
+ ErlixTerm* eint;
25
+ int i=FIX2INT(fix);
26
+ Data_Get_Struct(self,ErlixTerm,eint);
27
+ eint->term=erl_mk_int(i);
28
+ return self;
29
+ }
30
+
31
+
32
+ static VALUE erlix_int_to_fix(VALUE self){
33
+ ErlixTerm *eint;
34
+ int l;
35
+ Data_Get_Struct(self,ErlixTerm,eint);
36
+ l=ERL_INT_VALUE(eint->term);
37
+ return INT2FIX(l);
38
+ }
39
+
40
+ static VALUE erlix_int_inspect(VALUE self){
41
+ VALUE ret=rb_str_new2("#<Erlix::Int:");
42
+ ID concat=rb_intern("concat");
43
+ rb_funcall(ret,concat,1,rb_funcall(self,rb_intern("to_s"),0));
44
+ rb_funcall(ret,concat,1,rb_str_new2(">"));
45
+ return ret;
46
+ }
47
+
48
+ static VALUE erlix_int_etype(VALUE self){
49
+ return rb_str_new2("int");
50
+ }
51
+
52
+ void init_erlix_int(VALUE erlix){
53
+ erlix_cErlixInt=rb_define_class_under(erlix,"Int",rb_cObject);
54
+
55
+ rb_define_alloc_func(erlix_cErlixInt,erlix_int_alloc);
56
+ rb_define_method(erlix_cErlixInt,"initialize",erlix_int_init,1);
57
+ rb_define_method(erlix_cErlixInt,"to_i",erlix_int_to_fix,0);
58
+ rb_define_method(erlix_cErlixInt,"inspect",erlix_int_inspect,0);
59
+ rb_define_method(erlix_cErlixInt,"etype",erlix_int_etype,0);
60
+
61
+ rb_include_module(erlix_cErlixInt,erlix_mErlixTerm);
62
+ }
63
+
64
+
@@ -0,0 +1,262 @@
1
+ /*
2
+ * project : erlix
3
+ * author : kdr2
4
+ *
5
+ */
6
+
7
+ #include "erlix_term.h"
8
+
9
+ VALUE erlix_cErlixList;
10
+
11
+ VALUE erlix_list_alloc(VALUE klass){
12
+ ErlixTerm *list;
13
+ VALUE obj;
14
+
15
+ list=new_erlix_term();
16
+ list->type=TYPE_LIST;
17
+ obj=Data_Wrap_Struct(klass,0,free_erlix_term,list);
18
+ return obj;
19
+ }
20
+
21
+ static VALUE erlix_list_init(VALUE self,VALUE ary){
22
+ ErlixTerm *list;
23
+ ETERM **les;
24
+ VALUE e;
25
+ ErlixTerm *ep;
26
+ int i;
27
+ Data_Get_Struct(self,ErlixTerm,list);
28
+
29
+ if(NIL_P(ary)){
30
+ //empty list
31
+ list->term=erl_mk_empty_list();
32
+ return self;
33
+ }
34
+ if(TYPE(ary)==T_ARRAY){
35
+ if(RARRAY_LEN(ary)==0){
36
+ //empty list
37
+ list->term=erl_mk_empty_list();
38
+ }else{
39
+ //check: all elements' must be ErlixTerm or auto-convertable Type
40
+ for(i=0;i<RARRAY_LEN(ary);i++){
41
+ e=RARRAY_PTR(ary)[i];
42
+ if(!IS_ETERM(e) && !CAN_AUTO_CONV(e)){
43
+ rb_raise(rb_eTypeError,"all list's elements must be ErlixTerm or Auto-Convertable-Type!");
44
+ }
45
+ }
46
+ les=(ETERM**)malloc(sizeof(ETERM*)*(RARRAY_LEN(ary)));
47
+ for(i=0;i<RARRAY_LEN(ary);i++){
48
+ e=RARRAY_PTR(ary)[i];
49
+ if(IS_ETERM(e)){
50
+ Data_Get_Struct(e,ErlixTerm,ep);
51
+ *(les+i)=erl_copy_term(ep->term);
52
+ }else{
53
+ *(les+i)=erlix_auto_conv(e);
54
+ }
55
+ }
56
+ list->term=erl_mk_list(les,RARRAY_LEN(ary));
57
+ //for(i=0;i<RARRAY(ary)->len;i++){
58
+ // erl_free_term(*(les+i));
59
+ //}
60
+ free(les);
61
+ }
62
+ }else if(TYPE(ary)==T_STRING){
63
+ list->term=erl_mk_estring(RSTRING_PTR(ary), RSTRING_LEN(ary));
64
+ }
65
+ return self;
66
+ }
67
+
68
+
69
+ static VALUE erlix_list_init2(int argc, VALUE* argv,VALUE self){
70
+ ErlixTerm *list;
71
+ ETERM **les;
72
+ VALUE e;
73
+ ErlixTerm *ep;
74
+ int i;
75
+ Data_Get_Struct(self,ErlixTerm,list);
76
+
77
+ if(argc==0){
78
+ //empty list
79
+ list->term=erl_mk_empty_list();
80
+ return self;
81
+ }
82
+
83
+ if(argc==1 && (ARRAY_P(*argv)||STRING_P(*argv))){
84
+ return erlix_list_init(self, *argv);
85
+ }
86
+
87
+ //check: all elements' must be ErlixTerm or auto-convertable Type
88
+ for(i=0;i<argc;i++){
89
+ e=*(argv+i);
90
+ if(!IS_ETERM(e) && !CAN_AUTO_CONV(e)){
91
+ rb_raise(rb_eTypeError,"all list's elements must be ErlixTerm or Auto-Convertable-Type!");
92
+ }
93
+ }
94
+ les=(ETERM**)malloc(sizeof(ETERM*)*argc);
95
+ for(i=0;i<argc;i++){
96
+ e=*(argv+i);
97
+ if(IS_ETERM(e)){
98
+ Data_Get_Struct(e,ErlixTerm,ep);
99
+ *(les+i)=erl_copy_term(ep->term);
100
+ }else{
101
+ *(les+i)=erlix_auto_conv(e);
102
+ }
103
+ }
104
+ list->term=erl_mk_list(les,argc);
105
+ //for(i=0;i<RARRAY(ary)->len;i++){
106
+ // erl_free_term(*(les+i));
107
+ //}
108
+ free(les);
109
+ return self;
110
+ }
111
+
112
+
113
+ static VALUE erlix_list_create(int argc,VALUE *argv,VALUE klass){
114
+ ETERM *rterm;
115
+ ETERM **les;
116
+ ErlixTerm *ep;
117
+ VALUE e;
118
+ int i;
119
+
120
+ if(argc==0){
121
+ //empty list
122
+ rterm=erl_mk_empty_list();
123
+ }else{
124
+ //check: all elements' must be ErlixTerm or auto-convertable Type
125
+ for(i=0;i<argc;i++){
126
+ e=argv[i];
127
+ if(!IS_ETERM(e) && !CAN_AUTO_CONV(argv[i])){
128
+ rb_raise(rb_eTypeError,"all list's elements must be ErlixTerm or Auto-Convertable-Type!");
129
+ }
130
+ }
131
+ les=(ETERM**)malloc(sizeof(ETERM*)*argc);
132
+ for(i=0;i<argc;i++){
133
+ e=argv[i];
134
+ if(IS_ETERM(e)){
135
+ Data_Get_Struct(e,ErlixTerm,ep);
136
+ *(les+i)=erl_copy_term(ep->term);
137
+ }else{
138
+ *(les+i)=erlix_auto_conv(e);
139
+ }
140
+ }
141
+ rterm=erl_mk_list(les,argc);
142
+ //for(i=0;i<argc;i++){
143
+ // erl_free_term(*(les+i));
144
+ //}
145
+ free(les);
146
+ }
147
+ return erlix_term(rterm);
148
+ }
149
+
150
+ static VALUE erlix_list_to_ary(VALUE self){
151
+ ErlixTerm *list;
152
+ ETERM *hd,*tmp,*ep;
153
+ VALUE ret;
154
+ int len, i;
155
+
156
+ Data_Get_Struct(self,ErlixTerm,list);
157
+ len=erl_length(list->term);
158
+ ret=rb_ary_new2(len);
159
+ ep=list->term;
160
+ i=1;
161
+ while(!ERL_IS_EMPTY_LIST(ep)){
162
+ hd=erl_hd(ep);
163
+ rb_ary_store(ret,i,erlix_term(erl_copy_term(hd)));
164
+ tmp=erl_tl(ep);
165
+ //if(ep!=list->term)erl_free_term(ep);
166
+ ep=tmp;
167
+ //if(ERL_IS_EMPTY_LIST(ep)){
168
+ // erl_free_term(ep);
169
+ //}
170
+ ++i;
171
+ }
172
+ return ret;
173
+ }
174
+
175
+ static VALUE erlix_list_head(VALUE self){
176
+ ErlixTerm *list;
177
+ ETERM *ep;
178
+ Data_Get_Struct(self,ErlixTerm,list);
179
+ if(ERL_IS_EMPTY_LIST(list->term)){
180
+ return Qnil;
181
+ }
182
+ ep=erl_hd(list->term);
183
+ return erlix_term(erl_copy_term(ep));
184
+ }
185
+
186
+ static VALUE erlix_list_tail(VALUE self){
187
+ ErlixTerm *list;
188
+ ETERM *ep;
189
+
190
+ Data_Get_Struct(self,ErlixTerm,list);
191
+ if(ERL_IS_EMPTY_LIST(list->term)){
192
+ return Qnil;
193
+ }
194
+ ep=erl_tl(list->term);
195
+ return erlix_term(erl_copy_term(ep));
196
+ }
197
+
198
+ static VALUE erlix_list_cons(VALUE self,VALUE head){
199
+ ErlixTerm *list;
200
+ ETERM *ep;
201
+ ErlixTerm *hd;
202
+ VALUE ret;
203
+
204
+ if(!IS_ETERM(head) && !CAN_AUTO_CONV(head)){
205
+ rb_raise(rb_eTypeError,"the head must be ErlixTerm or Auto-Convertable-Type!");
206
+ return Qnil;
207
+ }
208
+
209
+ Data_Get_Struct(self,ErlixTerm,list);
210
+ if(IS_ETERM(head)){
211
+ Data_Get_Struct(head,ErlixTerm,hd);
212
+ ep=erl_cons(hd->term,list->term);
213
+ }else{
214
+ ep=erl_cons(erlix_auto_conv(head),list->term);
215
+ }
216
+ ret = erlix_term(erl_copy_term(ep));
217
+ erl_free_term(ep);
218
+ return ret;
219
+ }
220
+
221
+ static VALUE erlix_list_size(VALUE self){
222
+ ErlixTerm *list;
223
+ int len;
224
+ Data_Get_Struct(self,ErlixTerm,list);
225
+ len=erl_length(list->term);
226
+ return INT2FIX(len);
227
+ }
228
+
229
+ static VALUE erlix_list_inspect(VALUE self){
230
+ VALUE ret;
231
+ ID concat;
232
+
233
+ ret=rb_str_new2("#<Erlix::List:");
234
+ concat=rb_intern("concat");
235
+ rb_funcall(ret,concat,1,rb_funcall(self,rb_intern("to_s"),0));
236
+ rb_funcall(ret,concat,1,rb_str_new2(">"));
237
+ return ret;
238
+ }
239
+
240
+ static VALUE erlix_list_etype(VALUE self){
241
+ return rb_str_new2("list");
242
+ }
243
+
244
+ void init_erlix_list(VALUE erlix){
245
+ erlix_cErlixList=rb_define_class_under(erlix,"List",rb_cObject);
246
+
247
+ rb_define_alloc_func(erlix_cErlixList,erlix_list_alloc);
248
+ rb_define_method(erlix_cErlixList,"initialize",erlix_list_init2,-1);
249
+ rb_define_method(erlix_cErlixList,"head",erlix_list_head,0);
250
+ rb_define_method(erlix_cErlixList,"tail",erlix_list_tail,0);
251
+ rb_define_method(erlix_cErlixList,"cons",erlix_list_cons,1);
252
+ rb_define_method(erlix_cErlixList,"to_a",erlix_list_to_ary,0);
253
+ rb_define_method(erlix_cErlixList,"size",erlix_list_size,0);
254
+ rb_define_method(erlix_cErlixList,"inspect",erlix_list_inspect,0);
255
+ rb_define_method(erlix_cErlixList,"etype",erlix_list_etype,0);
256
+
257
+ rb_define_singleton_method(erlix_cErlixList,"[]",erlix_list_create,-1);
258
+
259
+ rb_include_module(erlix_cErlixList,erlix_mErlixTerm);
260
+ }
261
+
262
+
@@ -0,0 +1,122 @@
1
+ /*
2
+ * project : erlix
3
+ * author : kdr2
4
+ *
5
+ */
6
+
7
+ #include <stdlib.h>
8
+
9
+ #include "erlix_term.h"
10
+ #include "erlix_node.h"
11
+
12
+ VALUE erlix_cErlixMessage;
13
+
14
+ static ErlMessage* new_erlix_message(){
15
+ ErlMessage *ret=(ErlMessage*)malloc(sizeof(ErlMessage));
16
+ return ret;
17
+ }
18
+
19
+ static void free_erlix_message(void *omsg){
20
+ ErlMessage *msg=omsg;
21
+ erl_free_term(msg->msg);
22
+ erl_free_term(msg->from);
23
+ erl_free_term(msg->to);
24
+ free(msg);
25
+ }
26
+
27
+ VALUE erlix_message_alloc(VALUE klass){
28
+ ErlMessage *msg=new_erlix_message();
29
+ VALUE obj;
30
+ obj=Data_Wrap_Struct(klass,0,free_erlix_message,msg);
31
+ return obj;
32
+ }
33
+
34
+ static VALUE erlix_message_init_copy(VALUE copy,VALUE orig){
35
+ ErlMessage *tcopy,*tsrc;
36
+ if(copy==orig)return copy;
37
+ if(TYPE(orig)!=T_DATA || RDATA(orig)->dfree==(RUBY_DATA_FUNC)free_erlix_message){
38
+ rb_raise(rb_eTypeError,"wrong argument type!");
39
+ }
40
+ Data_Get_Struct(copy,ErlMessage,tcopy);
41
+ Data_Get_Struct(orig,ErlMessage,tsrc);
42
+ tcopy->type=tsrc->type;
43
+ tcopy->msg=erl_copy_term(tsrc->msg);
44
+ tcopy->from=erl_copy_term(tsrc->from);
45
+ tcopy->to=erl_copy_term(tsrc->to);
46
+ memcpy(tcopy->to_name,tsrc->to_name,MAXREGLEN);
47
+ return copy;
48
+ }
49
+
50
+ static VALUE erlix_message_etype(VALUE self){
51
+ return rb_str_new2("ErlMessage");
52
+ }
53
+
54
+ static VALUE erlix_message_mtype(VALUE self){
55
+ //ERL_SEND, ERL_REG_SEND, ERL_LINK, ERL_UNLINK and ERL_EXIT
56
+ ErlMessage *msg;
57
+ Data_Get_Struct(self,ErlMessage,msg);
58
+ switch(msg->type){
59
+ case ERL_SEND:
60
+ return rb_str_new2("ERL_SEND");
61
+ break;
62
+ case ERL_REG_SEND:
63
+ return rb_str_new2("ERL_REG_SEND");
64
+ break;
65
+ case ERL_LINK:
66
+ return rb_str_new2("ERL_LINK");
67
+ break;
68
+ case ERL_UNLINK:
69
+ return rb_str_new2("ERL_UNLINK");
70
+ break;
71
+ case ERL_EXIT:
72
+ return rb_str_new2("ERL_EXIT");
73
+ break;
74
+ default:
75
+ return rb_str_new2("UNKOWN");
76
+ }
77
+ }
78
+
79
+ static VALUE erlix_message_message(VALUE self){
80
+ ErlMessage *msg;
81
+ Data_Get_Struct(self,ErlMessage,msg);
82
+ if(msg->type==ERL_LINK || msg->type==ERL_UNLINK){
83
+ return Qnil;
84
+ }
85
+ return erlix_term(erl_copy_term(msg->msg));
86
+ }
87
+
88
+ static VALUE erlix_message_from(VALUE self){
89
+ ErlMessage *msg;
90
+ Data_Get_Struct(self,ErlMessage,msg);
91
+
92
+ if(msg->type==ERL_SEND){
93
+ return Qnil;
94
+ }
95
+
96
+ return erlix_term(erl_copy_term(msg->from));
97
+ }
98
+
99
+ static VALUE erlix_message_to(VALUE self){
100
+ ErlMessage *msg;
101
+ Data_Get_Struct(self,ErlMessage,msg);
102
+ if(msg->type==ERL_REG_SEND){
103
+ return Qnil;
104
+ }
105
+ return erlix_term(erl_copy_term(msg->to));
106
+ }
107
+
108
+
109
+
110
+ void init_erlix_message(VALUE erlix){
111
+ erlix_cErlixMessage=rb_define_class_under(erlix,"Message",rb_cObject);
112
+
113
+ rb_define_alloc_func(erlix_cErlixMessage,erlix_message_alloc);
114
+ rb_define_method(erlix_cErlixMessage,"initialize_copy",erlix_message_init_copy,1);
115
+ rb_define_method(erlix_cErlixMessage,"mtype",erlix_message_mtype,0);
116
+ rb_define_method(erlix_cErlixMessage,"etype",erlix_message_etype,0);
117
+ rb_define_method(erlix_cErlixMessage,"message",erlix_message_message,0);
118
+ rb_define_method(erlix_cErlixMessage,"from",erlix_message_from,0);
119
+ rb_define_method(erlix_cErlixMessage,"to",erlix_message_to,0);
120
+ }
121
+
122
+