static ETERM* all_types(void) { ETERM* t; ETERM* terms[3]; int i; static char a_binary[] = "A binary"; #define CONS_AND_FREE(expr, tail) \ do { \ ETERM* term = expr; \ ETERM* nl = erl_cons(term, tail); \ erl_free_term(term); \ erl_free_term(tail); \ tail = nl; \ } while (0) t = erl_mk_empty_list(); CONS_AND_FREE(erl_mk_atom("I am an atom"), t); CONS_AND_FREE(erl_mk_binary("A binary", sizeof(a_binary)-1), t); CONS_AND_FREE(erl_mk_float(3.0), t); CONS_AND_FREE(erl_mk_int(0), t); CONS_AND_FREE(erl_mk_int(-1), t); CONS_AND_FREE(erl_mk_int(1), t); CONS_AND_FREE(erl_mk_string("A string"), t); terms[0] = erl_mk_atom("element1"); terms[1] = erl_mk_int(42); terms[2] = erl_mk_int(767); CONS_AND_FREE(erl_mk_tuple(terms, ASIZE(terms)), t); for (i = 0; i < ASIZE(terms); i++) { erl_free_term(terms[i]); } CONS_AND_FREE(erl_mk_pid("kalle@localhost", 3, 2, 1), t); CONS_AND_FREE(erl_mk_pid("abcdefghijabcdefghij@localhost", 3, 2, 1), t); CONS_AND_FREE(erl_mk_port("kalle@localhost", 4, 1), t); CONS_AND_FREE(erl_mk_port("abcdefghijabcdefghij@localhost", 4, 1), t); CONS_AND_FREE(erl_mk_ref("kalle@localhost", 6, 1), t); CONS_AND_FREE(erl_mk_ref("abcdefghijabcdefghij@localhost", 6, 1), t); return t; #undef CONS_AND_FREE }
static int erl_do_receive_msg(int fd, ei_x_buff* x, ErlMessage* emsg) { erlang_msg msg; int r; msg.from.node[0] = msg.to.node[0] = msg.toname[0] = '\0'; r = ei_do_receive_msg(fd, 0, &msg, x, 0); if (r == ERL_MSG) { int index = 0; emsg->type = msg.msgtype; /* We can't call ei_decode_term for cases where there are no data following the type information. If there are other types added later where there are data this case has to be extended. */ switch (msg.msgtype) { case ERL_SEND: case ERL_REG_SEND: case ERL_EXIT: case ERL_EXIT2: if (ei_decode_term(x->buff, &index, &emsg->msg) < 0) r = ERL_ERROR; break; default: emsg->msg = NULL; /* Not needed but may avoid problems for unsafe caller */ break; } } else emsg->msg = NULL; if (msg.from.node[0] != '\0') emsg->from = erl_mk_pid(msg.from.node, msg.from.num, msg.from.serial, msg.from.creation); else emsg->from = NULL; if (msg.to.node[0] != '\0') emsg->to = erl_mk_pid(msg.to.node, msg.to.num, msg.to.serial, msg.to.creation); else emsg->to = NULL; strcpy(emsg->to_name, msg.toname); return r; }
static VALUE erlix_pid_init(VALUE self,VALUE econn){ ErlixTerm* pid; ErlixConnection* conn; if(erlix_node==NULL){ rb_raise(rb_eException,"call Erlix::Node.init(name,cookie) first please!"); return Qnil; } if((TYPE(econn)!=T_DATA || RDATA(econn)->dfree!=(RUBY_DATA_FUNC)free_erlix_connection)){ rb_raise(rb_eTypeError,"the argument is not an Erlix::Connection"); } Data_Get_Struct(self,ErlixTerm,pid); Data_Get_Struct(econn,ErlixConnection,conn); pid->term=erl_mk_pid(erl_thisnodename(),conn->sock_fd,rand()%16,erl_thiscreation()); return self; }
int main(void) #endif { ei_x_buff eix; int index = 0; ETERM **etermpp = NULL, *etermp = NULL; char *charp = NULL; unsigned char uchar, **ucharpp = NULL, *ucharp = NULL; void *voidp = NULL; Erl_Heap *erl_heapp = NULL; int intx = 0; int *intp = NULL; unsigned int uintx, *uintp; unsigned long *ulongp = NULL; long longx = 0; double doublex = 0.0; short shortx = 42; FILE *filep = NULL; Erl_IpAddr erl_ipaddr = NULL; ErlMessage *erlmessagep = NULL; ErlConnect *erlconnectp = NULL; struct hostent *hostp = NULL; struct in_addr *inaddrp = NULL; /* Converion to erl_interface format is in liberl_interface */ intx = erl_errno; ei_encode_term(charp, &index, voidp); ei_x_encode_term(&eix, voidp); ei_decode_term(charp, &index, voidp); erl_init(voidp, longx); erl_connect_init(intx, charp,shortx); erl_connect_xinit(charp,charp,charp,erl_ipaddr,charp,shortx); erl_connect(charp); erl_xconnect(erl_ipaddr,charp); erl_close_connection(intx); erl_receive(intx, ucharp, intx); erl_receive_msg(intx, ucharp, intx, erlmessagep); erl_xreceive_msg(intx, ucharpp, intp, erlmessagep); erl_send(intx, etermp, etermp); erl_reg_send(intx, charp, etermp); erl_rpc(intx,charp,charp,etermp); erl_rpc_to(intx,charp,charp,etermp); erl_rpc_from(intx,intx,erlmessagep); erl_publish(intx); erl_accept(intx,erlconnectp); erl_thiscookie(); erl_thisnodename(); erl_thishostname(); erl_thisalivename(); erl_thiscreation(); erl_unpublish(charp); erl_err_msg(charp); erl_err_quit(charp); erl_err_ret(charp); erl_err_sys(charp); erl_cons(etermp,etermp); erl_copy_term(etermp); erl_element(intx,etermp); erl_hd(etermp); erl_iolist_to_binary(etermp); erl_iolist_to_string(etermp); erl_iolist_length(etermp); erl_length(etermp); erl_mk_atom(charp); erl_mk_binary(charp,intx); erl_mk_empty_list(); erl_mk_estring(charp, intx); erl_mk_float(doublex); erl_mk_int(intx); erl_mk_list(etermpp,intx); erl_mk_pid(charp,uintx,uintx,uchar); erl_mk_port(charp,uintx,uchar); erl_mk_ref(charp,uintx,uchar); erl_mk_long_ref(charp,uintx,uintx,uintx,uchar); erl_mk_string(charp); erl_mk_tuple(etermpp,intx); erl_mk_uint(uintx); erl_mk_var(charp); erl_print_term(filep,etermp); /* erl_sprint_term(charp,etermp); */ erl_size(etermp); erl_tl(etermp); erl_var_content(etermp, charp); erl_format(charp); erl_match(etermp, etermp); erl_global_names(intx, intp); erl_global_register(intx, charp, etermp); erl_global_unregister(intx, charp); erl_global_whereis(intx, charp, charp); erl_init_malloc(erl_heapp,longx); erl_alloc_eterm(uchar); erl_eterm_release(); erl_eterm_statistics(ulongp,ulongp); erl_free_array(etermpp,intx); erl_free_term(etermp); erl_free_compound(etermp); erl_malloc(longx); erl_free(voidp); erl_compare_ext(ucharp, ucharp); erl_decode(ucharp); erl_decode_buf(ucharpp); erl_encode(etermp,ucharp); erl_encode_buf(etermp,ucharpp); erl_ext_size(ucharp); erl_ext_type(ucharp); erl_peek_ext(ucharp,intx); erl_term_len(etermp); erl_gethostbyname(charp); erl_gethostbyaddr(charp, intx, intx); erl_gethostbyname_r(charp, hostp, charp, intx, intp); erl_gethostbyaddr_r(charp, intx, intx, hostp, charp, intx, intp); erl_init_resolve(); erl_distversion(intx); erl_epmd_connect(inaddrp); erl_epmd_port(inaddrp, charp, intp); charp = ERL_ATOM_PTR(etermp); intx = ERL_ATOM_SIZE(etermp); ucharp = ERL_BIN_PTR(etermp); intx = ERL_BIN_SIZE(etermp); etermp = ERL_CONS_HEAD(etermp); etermp = ERL_CONS_TAIL(etermp); intx = ERL_COUNT(etermp); doublex= ERL_FLOAT_VALUE(etermp); uintx = ERL_INT_UVALUE(etermp); intx = ERL_INT_VALUE(etermp); intx = ERL_IS_ATOM(etermp); intx = ERL_IS_BINARY(etermp); intx = ERL_IS_CONS(etermp); intx = ERL_IS_EMPTY_LIST(etermp); intx = ERL_IS_FLOAT(etermp); intx = ERL_IS_INTEGER(etermp); intx = ERL_IS_LIST(etermp); intx = ERL_IS_PID(etermp); intx = ERL_IS_PORT(etermp); intx = ERL_IS_REF(etermp); intx = ERL_IS_TUPLE(etermp); intx = ERL_IS_UNSIGNED_INTEGER(etermp); uchar = ERL_PID_CREATION(etermp); charp = ERL_PID_NODE(etermp); uintx = ERL_PID_NUMBER(etermp); uintx = ERL_PID_SERIAL(etermp); uchar = ERL_PORT_CREATION(etermp); charp = ERL_PORT_NODE(etermp); uintx = ERL_PORT_NUMBER(etermp); uchar = ERL_REF_CREATION(etermp); intx = ERL_REF_LEN(etermp); charp = ERL_REF_NODE(etermp); uintx = ERL_REF_NUMBER(etermp); uintp = ERL_REF_NUMBERS(etermp); etermp = ERL_TUPLE_ELEMENT(etermp,intx); intx = ERL_TUPLE_SIZE(etermp); return BUFSIZ + EAGAIN + EHOSTUNREACH + EINVAL + EIO + EMSGSIZE + ENOMEM + ERL_ATOM + ERL_BINARY + ERL_ERROR + ERL_EXIT + ERL_FLOAT + ERL_INTEGER + ERL_LINK + ERL_LIST + ERL_MSG + ERL_NO_TIMEOUT + ERL_PID + ERL_PORT + ERL_REF + ERL_REG_SEND + ERL_SEND + ERL_SMALL_BIG + ERL_TICK + ERL_TIMEOUT + ERL_TUPLE + ERL_UNLINK + ERL_U_INTEGER + ERL_U_SMALL_BIG + ERL_VARIABLE + ETIMEDOUT + MAXNODELEN + MAXREGLEN; }
PyObject *py_erlang_send_message(PyObject * self, PyObject * args) { ETERM *pymessage; ETERM *pid; int erfd; PyObject *ermessage, *erdest, *zero; int er_number, er_serial, er_creation; char *er_node; if (!PyArg_ParseTuple(args, "iOO|i:erlang_send_message", &erfd, &erdest, &ermessage)) { return NULL; } if (erfd < 0) { goto clear; } if (!PyString_Check(erdest) && !PyDict_Check(erdest)) { goto clear; } pymessage = py_to_eterm(ermessage); if (!pymessage) { goto clear; } if (PyString_Check(erdest)) { if (!erl_reg_send(erfd, PyString_AsString(erdest), pymessage)) { erl_err_msg("erl_reg_send()"); goto clear2; } } else if (PyDict_Check(erdest)) { zero = PyDict_GetItemString(erdest, "node"); if (!zero) { goto clear2; } if (!PyString_Check(zero)) { goto clear2; } er_node = PyString_AsString(zero); zero = PyDict_GetItemString(erdest, "number"); if (!zero) { goto clear2; } if (!PyInt_Check(zero)) { goto clear2; } er_number = PyInt_AsLong(zero); zero = PyDict_GetItemString(erdest, "serial"); if (!zero) { goto clear2; } if (!PyInt_Check(zero)) { goto clear2; } er_serial = PyInt_AsLong(zero); zero = PyDict_GetItemString(erdest, "creation"); if (!zero) { goto clear2; } if (!PyInt_Check(zero)) { goto clear2; } er_creation = PyInt_AsLong(zero); pid = erl_mk_pid((const char *) er_node, er_number, er_serial, er_creation); if (!pid) { goto clear2; } if (!erl_send(erfd, pid, pymessage)) { erl_err_msg("erl_send()"); erl_free_term(pid); goto clear2; } erl_free_term(pid); } else { goto clear; } erl_free_compound(pymessage); Py_INCREF(Py_True); return Py_True; clear2: erl_free_compound(pymessage); clear: PyErr_Print(); Py_INCREF(Py_None); return Py_None; }
ETERM *py_to_eterm(PyObject * pobj) { int i; int count; PyObject *pobj2; ETERM *eobj = NULL; ETERM *eobj2 = NULL; ETERM **eobj3; if (pobj == NULL) { return erl_mk_empty_list(); } if (PyString_Check(pobj)) { eobj = erl_mk_atom(PyString_AsString(pobj)); } else if (PyInt_Check(pobj)) { eobj = erl_mk_int(PyInt_AsLong(pobj)); } else if (PyList_Check(pobj)) { eobj = erl_mk_empty_list(); for (i = PyList_Size(pobj) - 1; i >= 0; i--) { pobj2 = PyList_GetItem(pobj, i); eobj2 = py_to_eterm(pobj2); eobj = erl_cons(eobj2, eobj); } } else if (PyDict_Check(pobj)) { // a pid char *er_node; int er_number, er_serial, er_creation; pobj2 = PyDict_GetItemString(pobj, "node"); if (!pobj2) { PyErr_Print(); goto clear; } if (!PyString_Check(pobj2)) { goto clear; } er_node = PyString_AsString(pobj2); pobj2 = PyDict_GetItemString(pobj, "number"); if (!pobj2) { PyErr_Print(); goto clear; } if (!PyInt_Check(pobj2)) { goto clear; } er_number = PyInt_AsLong(pobj2); pobj2 = PyDict_GetItemString(pobj, "serial"); if (!pobj2) { PyErr_Print(); goto clear; } if (!PyInt_Check(pobj2)) { goto clear; } er_serial = PyInt_AsLong(pobj2); pobj2 = PyDict_GetItemString(pobj, "creation"); if (!pobj2) { PyErr_Print(); goto clear; } if (!PyInt_Check(pobj2)) { goto clear; } er_creation = PyInt_AsLong(pobj2); eobj = erl_mk_pid(er_node, er_number, er_serial, er_creation); } else if (PyTuple_Check(pobj)) { count = PyTuple_Size(pobj); eobj3 = malloc(sizeof(ETERM *) * count); for (i = 0; i < count; i++) { pobj2 = PyTuple_GetItem(pobj, i); if (!pobj2) { break; } eobj3[i] = py_to_eterm(pobj2); } eobj = erl_mk_tuple(eobj3, count); free(eobj3); } else { fprintf(stderr, "UNMANAGED PYTHON TYPE: %s\n", pobj->ob_type->tp_name); } clear: if (eobj == NULL) { return erl_mk_empty_list(); } return eobj; }