void eNotifyCallback(int watchID, int action, const void* rootPath, int rootPathLength, const void* filePath, int filePathLength) { // MAX_FILE_PATHNAME_LENGTH * 2 because we are passing 2 paths byte buf[MAX_FILE_PATHNAME_LENGTH*2]; ETERM *tuplep; // Build response ETERM *tupleArray[4]; tupleArray[0] = erl_mk_int(watchID); tupleArray[1] = erl_mk_int(action); tupleArray[2] = erl_mk_binary(rootPath, rootPathLength); tupleArray[3] = erl_mk_binary(filePath, filePathLength); tuplep = erl_mk_tuple(tupleArray, 4); erl_encode(tuplep, buf); write_cmd(buf, erl_term_len(tuplep)); // free contents from tupleArray erl_free_array(tupleArray, 4); }
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 }
void * handle_command(void *buf){ ETERM *tuplep, *file; ETERM *fnp; tuplep = erl_decode(buf); fnp = erl_element(1, tuplep); char * path = "./priv/sample_images/ABST.00000000.tif"; IplImage *source = cvLoadImage( path, CV_LOAD_IMAGE_GRAYSCALE ); CvMat* pngImage = img(source); //if (strncmp(ERL_ATOM_PTR(fnp), "img", 3) == 0) { // no-op for now? //} file = erl_mk_binary((char *)pngImage->data.ptr, pngImage->cols); erl_encode(file, buf); write_cmd(buf, erl_term_len(file)); erl_free_compound(tuplep); erl_free_term(fnp); erl_free_term(file); }
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; }
int main(int argc, char* argv[]) { ETERM *tuplep, *fnp, *pathp, *binp, *widthp, *heightp; ETERM *res_tuplep; ETERM *res_arrp[2]; ETERM *res_ok_atomp, *res_error_atomp, *res_error_cause_atomp; int idx = 1; byte *buf = malloc(INIT_BUF_SIZE); int buf_len = INIT_BUF_SIZE; int res_len; erl_init(NULL, 0); //Create common atoms(ok, error) res_ok_atomp = erl_mk_atom("ok"); res_error_atomp = erl_mk_atom("error"); res_error_cause_atomp = erl_mk_atom("resize failed"); while (read_cmd(&buf, &buf_len) > 0) { tuplep = erl_decode(buf); fnp = erl_element(idx++, tuplep); pathp = erl_element(idx++, tuplep); binp = erl_element(idx++, tuplep); widthp = erl_element(idx++, tuplep); heightp = erl_element(idx++, tuplep); if (strncmp(ERL_ATOM_PTR(fnp), "resize", 6) == 0) { char* algop = ERL_ATOM_PTR(fnp) + 6; resizerl_handler* rhp = (strncmp(algop, "_opencv", 7) == 0) ? resizerl_impls[0] : NULL; if (rhp == NULL) { exit(1); } // ERL_INT_VALUE, ERL_BIN_PTR, ERL_BIN_SIZE void* dp = NULL; rhp->exec(ERL_BIN_PTR(binp), ERL_BIN_SIZE(binp), ERL_BIN_PTR(pathp), ERL_BIN_SIZE(pathp), ERL_INT_VALUE(widthp), ERL_INT_VALUE(heightp), &dp); if (dp == NULL) { exit(2); } unsigned char* dst; int dst_size; rhp->get_result(dp, &dst, &dst_size); res_arrp[0] = res_ok_atomp; res_arrp[1] = erl_mk_binary((const char*)dst, dst_size); res_tuplep = erl_mk_tuple(res_arrp, 2); res_len = erl_term_len(res_tuplep); if (res_len > buf_len) { byte* new_buf = (byte*)realloc((void*)buf, res_len); if (new_buf == NULL) { exit(3); } buf = new_buf; buf_len = res_len; } erl_encode(res_tuplep, buf); write_cmd(buf, erl_term_len(res_tuplep)); erl_free_term(res_arrp[1]); erl_free_term(res_tuplep); rhp->release(&dp); } else { res_arrp[0] = res_error_atomp; res_arrp[1] = res_error_cause_atomp; res_tuplep = erl_mk_tuple(res_arrp, 2); if (res_len > buf_len) { byte* new_buf = (byte*)realloc((void*)buf, res_len); if (new_buf == NULL) { exit(4); } buf = new_buf; buf_len = res_len; } erl_encode(res_tuplep, buf); write_cmd(buf, erl_term_len(res_tuplep)); erl_free_term(res_tuplep); } erl_free_compound(tuplep); erl_free_term(fnp); erl_free_term(pathp); erl_free_term(binp); erl_free_term(widthp); erl_free_term(heightp); idx = 1; } // normal return 0; }
/** * Read an integer giving the maximum size of a fragment. */ int main( int argc, char *argv[] ) { int fail = 0; erl_init(NULL,0); #ifdef _DEBUG openlog( "erlang-port", 0, LOG_USER ); syslog( LOG_INFO, "%s is alive\n", argv[0] ); #endif while( ! fail ) { ETERM *fragsize_term = NULL; byte buf[100 /*WIRE_SIZEOF_INT*/]; if( read_lpm( buf, sizeof(buf) ) <= 0 ) { #ifdef _DEBUG syslog( LOG_ERR, "failed reading fragment size:\n" ); #endif break; } fragsize_term = erl_decode( buf ); if( fragsize_term ) { ETERM **array; unsigned int count; unsigned int SIZEOF_FRAGMENT = ERL_INT_UVALUE(fragsize_term); erl_free_term( fragsize_term ); if( SIZEOF_FRAGMENT == 0 /* the explicit signal to shutdown */ ) break; #ifdef _DEBUG syslog( LOG_INFO, "received sizeof fragment: %d\n", SIZEOF_FRAGMENT ); #endif count = min_packet_count( SIZEOF_IMAGE, SIZEOF_FRAGMENT ); array = calloc( count, sizeof(ETERM*) ); if( array ) { ETERM *frags; for(int i = 0; i < count; i++ ) { const size_t S = i+1 < count ? SIZEOF_FRAGMENT : (SIZEOF_IMAGE % SIZEOF_FRAGMENT); array[i] = erl_mk_binary( TEST_IMAGE + i*SIZEOF_FRAGMENT, S ); if( array[i] == NULL ) { count = i; // ...for erl_free_array below. fail = 1; break; } } if( fail ) goto cleanup; frags = erl_mk_list( array, count ); if( frags ) { const int nbyte = erl_term_len( frags ); byte *buf = calloc( nbyte, sizeof(byte) ); if( buf ) { if( erl_encode( frags, buf ) == nbyte ) { if( write_lpm( buf, nbyte ) != nbyte ) fail = 1; } else fail = 1; free( buf ); } else fail = 1; } else fail = 1; cleanup: erl_free_array( array, count ); free( array ); } } else break; } #ifdef _DEBUG closelog(); #endif return 0; }