5 #define REMNODE argv[1]
6 #define REMHOST argv[2]
9 #define NODE_NAME argv[5]
10 #define ERL_DIST_VSN atoi(argv[6])
12 static int fd
; /* fd to remote Erlang node */
13 static gchar
* rem_regname
; /* process name on remote Erlang node */
15 int gn_start_cnode(char **argv
) {
17 char rem_node_name
[MAXATOMLEN
] = ""; /* other node name */
19 erlang_pid
*self
= ei_self(&ec
);
21 strcat(rem_node_name
,REMNODE
);
22 strcat(rem_node_name
,"@");
23 strcat(rem_node_name
,REMHOST
);
24 rem_regname
= g_strdup(REMREG
);
25 g_print("I am %s, you are %s\n", NODE_NAME
, rem_node_name
);
27 ei_set_compat_rel(ERL_DIST_VSN
); /* erlnode version of dist. protocol */
29 if ( ei_connect_init(&ec
, NODE_NAME
, COOKIE
, CREATION
) < 0 )
30 g_critical("ei_connect_init");
32 if ( (fd
= ei_connect(&ec
, rem_node_name
)) < 0 )
33 g_critical("ei_connect failed.\nwrong cookie? erl-dist version mismatch?");
35 self
->num
= fd
; /* bug?? in ei_reg_send_tmo */
37 ei_x_new_with_version(&xbuf
);
38 gn_wrap_ans("handshake", &xbuf
);
39 ei_x_encode_empty_list(&xbuf
);
46 void gn_send(ei_x_buff
*xbuf
) {
49 if ( ei_reg_send(&ec
, fd
, rem_regname
, xbuf
->buff
, xbuf
->index
) )
50 g_critical("bad send");
53 static gboolean
get_fpointer(char *cmd
, ei_x_buff
*xbuf
, gpointer
* poi
) {
56 if ( ! g_module_supported() ) g_critical("gtkNode requires working gmodule");
58 if ( ! gmod
) gmod
= g_module_open(NULL
,0); /* "gtkNode_gtk.so" */
60 if ( g_module_symbol(gmod
, cmd
, poi
) ) return TRUE
;
62 g_warning("could not find '%s'.", cmd
);
63 gn_enc_2_error(xbuf
, "no_such_function");
64 ei_x_encode_atom(xbuf
, cmd
);
68 static gboolean
make_reply(ei_x_buff
*xbuf
, char *buff
, int *index
) {
71 gchar cmd
[MAXATOMLEN
+1];
72 gboolean (*funcp
)(int, ei_x_buff
*, char *, int *);
74 if ( ! ((arity
= gn_get_tuple(xbuf
, buff
, index
)) > -1) ||
75 ! gn_check_arity(xbuf
,2,arity
) ||
76 ! gn_get_arg_gchar_fix(xbuf
, buff
, index
, cmd
) ||
77 ! ((arity
= gn_get_list(xbuf
, buff
, index
)) > -1) )
80 if ( get_fpointer(cmd
, xbuf
, (gpointer
*)&funcp
) &&
81 (*funcp
)(arity
, xbuf
, buff
, index
) ) {
82 g_assert( ei_decode_list_header(buff
,index
,&k
) == 0 );
90 static void make_reply_list(ei_x_buff
*xbuf
, char *buff
, int *index
) {
94 if ( ! ((arity
= gn_get_list(xbuf
, buff
, index
)) > -1) )
97 gn_wrap_ans("reply",xbuf
);
98 for (i
= 0; i
< arity
; i
++) {
99 ei_x_encode_list_header(xbuf
, 1);
100 if ( ! make_reply(xbuf
, buff
, index
) )
103 ei_x_encode_empty_list(xbuf
);
107 static void reply(erlang_msg
*msg
, ei_x_buff
*recv_x_buf
) {
112 ei_decode_version(recv_x_buf
->buff
, &i
, &version
);
113 ei_x_new_with_version(&xbuf
);
114 make_reply_list(&xbuf
, recv_x_buf
->buff
, &i
);
119 /* called from gtk main loop when there's data on the cnode socket */
120 gboolean
gn_handle_read(GIOChannel
*source
,GIOCondition cond
,gpointer data
){
124 /* fd = g_io_channel_unix_get_fd(source); */
125 ei_x_new_with_version(&xbuf
);
126 switch ( ei_xreceive_msg(fd
, &msg
, &xbuf
) ){
130 switch (msg
.msgtype
) {
137 case ERL_GROUP_LEADER
:
142 gtk_main_quit(); /* crash and burn */
147 gtk_main_quit(); /* crash and burn */