//HsBool HsInt vaskerl_init_connect (char *name, char *ip, char *host, char *dest, char *cookie) { if (fd > -1) return fd; struct in_addr addr; addr.s_addr = inet_addr(ip); // struct hostent *he = erl_gethostbyname("127.0.0.1"); // printf("... %s\n", he->h_name); char fullname[256]; sprintf(fullname, "%s@%s", name, host); erl_init(NULL, 0); if (erl_connect_xinit(host, name, fullname, &addr, cookie, 0) < 0) //if (erl_connect_xinit("McHoovy.rd.shawcable.net", "vaskerl", // "*****@*****.**", &addr, // cookie, 0) < 0) erl_err_quit("erl_connect_xinit failed"); printf("node=%s, host=%s, alive=%s, creation=%d\n", erl_thisnodename(), erl_thishostname(), erl_thisalivename(), erl_thiscreation()); if ((fd = erl_xconnect(&addr, dest)) < 0) erl_err_quit("erl_xconnect failed"); return fd; //return HS_BOOL_TRUE; }
int main(int argc, char **argv) { int fd; /* fd to Erlang node */ int loop = 1; /* Loop flag */ int got; /* Result of receive */ unsigned char buf[BUFSIZE]; /* Buffer for incoming message */ ErlMessage emsg; /* Incoming message */ ETERM *fromp, *tuplep, *fnp, *argp, *resp; int res; erl_init(NULL, 0); if (erl_connect_init(1, "secretcookie", 0) == -1) erl_err_quit("erl_connect_init"); if ((fd = erl_connect("e1@hostname")) < 0) erl_err_quit("erl_connect"); fprintf(stderr, "Connected to e1@hostname\n\r"); while (loop) { got = erl_receive_msg(fd, buf, BUFSIZE, &emsg); if (got == ERL_TICK) { /* ignore */ } else if (got == ERL_ERROR) { loop = 0; } else { if (emsg.type == ERL_REG_SEND) { fromp = erl_element(1, emsg.msg); tuplep = erl_element(2, emsg.msg); fnp = erl_element(1, tuplep); argp = erl_element(2, tuplep); if (strncmp(ERL_ATOM_PTR(fnp), "foo", 3) == 0) { res = foo(ERL_INT_VALUE(argp)); } else if (strncmp(ERL_ATOM_PTR(fnp), "bar", 3) == 0) { res = bar(ERL_INT_VALUE(argp)); } resp = erl_format("{cnode, ~i}", res); erl_send(fd, fromp, resp); erl_free_term(emsg.from); erl_free_term(emsg.msg); erl_free_term(fromp); erl_free_term(tuplep); erl_free_term(fnp); erl_free_term(argp); erl_free_term(resp); } } } }
int main(int argc, char **argv) { int fd; /* file descriptor of Erlang node */ int loop = 1; /* Loop flag */ int got; /* Result of receive */ unsigned char buf[BUFSIZE]; /* Buffer for incoming message */ ErlMessage emsg; /* Incoming message */ ETERM *fromp, *argp, *resp; /* Reps of Erlang terms */ int res; /* Result of the fac call */ /* initialize erl_interface (once only) */ erl_init(NULL, 0); /* initialize the connection mechanism */ if (erl_connect_init(1, "refactorcookie", 0) == -1) erl_err_quit("erl_connect_init"); /* connect to a running Erlang node */ if ((fd = erl_connect("blah@Simon-Thompsons-Computer-2")) < 0) erl_err_quit("erl_connect"); while (loop) { /* message received */ got = erl_receive_msg(fd, buf, BUFSIZE, &emsg); if (got == ERL_TICK) { /* ignore */ } else if (got == ERL_ERROR) { loop = 0; } else { if (emsg.type == ERL_REG_SEND) { /* unpack message fields */ fromp = erl_element(1, emsg.msg); argp = erl_element(2, emsg.msg); /* call fac and send result back */ resp = erl_format("{ok, ~i}", fac(ERL_INT_VALUE(argp))); erl_send(fd, fromp, resp); /* free the term storage used */ erl_free_term(emsg.from); erl_free_term(emsg.msg); erl_free_term(fromp); erl_free_term(argp); erl_free_term(resp); } } } }
void quit_with_error(const char *fmt, ...) { char buf[512]; va_list ap; va_start(ap, fmt); int rv = vsprintf(buf, fmt, ap); va_end(ap); erl_err_quit("Erlycairo FATAL ERROR: %s\n", buf); }
/* -------------------------------------------------- */ int efs_init() { int ret, fd; erl_init(NULL, 0); if (erl_connect_init(1, "secretcookie", 0) == -1) erl_err_quit("erl_connect_init"); if ((fd = erl_connect("e1@uss")) < 0) erl_err_quit("erl_connect"); fprintf(stderr, "Connected to e1@uss\n\r"); erlang_fd = fd; return 0; }
int main(int argc, char *argv[]) { erl_init(NULL, 0); if (erl_connect_init(1, "secretcookie", 0) == -1) erl_err_quit("erl_connect_init"); int fd = -1; if ((fd = erl_connect("nefs@Daniels-MacBook")) < 0) erl_err_quit("erl_connect"); ferl_state FS={ .erlang_fd = fd }; fprintf(stderr, "Connected to nefs@Daniels-MacBook\n\r"); erl_rpc(fd,"io","format",erl_format("[ping]")); int retval = fuse_main(argc, argv, &hello_oper, (void *)&FS); fprintf(stderr, "Finished\n\r"); return retval; }
int setup_connection(int number, char* cookie, short creation, char* nodename) { int erl_fd; erl_init(NULL, 0); if(erl_connect_init(number, cookie, creation) == -1) { erl_err_quit("erl_connect_init failed"); } TRACE(("erl connection initialized")); erl_fd = erl_connect(nodename); if (erl_fd < 0) { erl_err_quit("erl_connect failed"); } TRACE(("connected!")); return erl_fd; }
int main(int argc, char **argv) { int port; // Listen port number int fd; // Socket descriptor int listen; // Listen socket ErlConnect conn; // Connection data char *cookie; // Erlang magic cookie pid_t pid; int x; pthread_t thread; pthread_attr_t thread_attrs; thread_data_t *data; // Per thread data: thread ID, // connection node name static unsigned int tidx = 0; pid = fork(); if (pid < 0) exit(EXIT_FAILURE); if (pid > 0) exit(EXIT_SUCCESS); if (setsid() < 0) exit(EXIT_FAILURE); umask(0); chdir("/"); signal(SIGCHLD, SIG_DFL); signal(SIGHUP, SIG_DFL); if (argc == 3) { port = atoi(argv[1]); cookie = argv[2]; } else { fprintf(stderr, "Usage: %s <port number> <erlang secret cookie>\n\r",argv[0]); exit(EXIT_FAILURE); } for (x = sysconf(_SC_OPEN_MAX); x>0; x--) close (x); setlogmask (LOG_UPTO (LOG_NOTICE)); openlog("nfq_node", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL0); erl_init(NULL, 0); if (erl_connect_init(1, cookie, 0) == -1) erl_err_quit("erl_connect_init"); if ((listen = my_listen(port)) <= 0) erl_err_quit("my_listen"); if (erl_publish(port) == -1) erl_err_quit("erl_publish"); if (pthread_attr_init(&thread_attrs)) { syslog(LOG_ERR, "error while init pthread attr struct\n\r"); exit(EXIT_FAILURE); } if ((pthread_attr_setdetachstate(&thread_attrs, PTHREAD_CREATE_DETACHED))) { syslog(LOG_ERR, "error while set pthread attributes\n\r"); exit(EXIT_FAILURE); } syslog(LOG_NOTICE,"Waiting for connections...\n\r"); for(;;) { while((fd = erl_accept(listen, &conn)) == ERL_ERROR) syslog(LOG_ERR,"%s Connection error\n\r", argv[0]); if((data = (thread_data_t *)malloc(sizeof(thread_data_t))) == NULL) { syslog(LOG_ERR, "Memory allocation error\n\r"); exit(EXIT_FAILURE); } if((data->node = (char *)malloc(strlen(conn.nodename)+1)) == NULL) { syslog(LOG_ERR, "Memory allocation error\n\r"); exit(EXIT_FAILURE); } data->fd = fd; data->idx = tidx; strcpy(data->node, conn.nodename); if (pthread_create(&thread, &thread_attrs, erl_message_read_loop, data)) { syslog(LOG_ERR, "Thread create failure\n\r"); exit(EXIT_FAILURE); } tidx++; } closelog(); return 0; }
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) { /* int fd; /\* fd to Erlang node *\/ */ int loop = 1; /* Loop flag */ int got; /* Result of receive */ unsigned char buf[BUFSIZE]; /* Buffer for incoming message */ ErlMessage emsg; /* Incoming message */ ETERM *msg_type, *fromp, *refp, *tuplep, *fnp, *arg1p, *arg2p, *resp; char hostname[255]; char erlang_nodename[255] = "e1@"; erl_init(NULL, 0); if (erl_connect_init(1, "secretcookie", 0) == -1) erl_err_quit("erl_connect_init"); /* now we figure out where to connect to */ get_hostname(hostname); strcat(erlang_nodename, hostname); strstrip(erlang_nodename); if ((fd_erlang_node = erl_connect(erlang_nodename)) < 0) erl_err_quit("erl_connect"); fprintf(stderr, "Connected to %s\n\r",erlang_nodename); while (loop) { got = erl_receive_msg(fd_erlang_node, buf, BUFSIZE, &emsg); if (got == ERL_TICK) { /* ignore */ } else if (got == ERL_ERROR) { loop = 0; } else { if (emsg.type == ERL_REG_SEND) { msg_type = erl_element(1, emsg.msg); fromp = erl_element(2, emsg.msg); refp = erl_element(3, emsg.msg); tuplep = erl_element(4, emsg.msg); fnp = erl_element(1, tuplep); if (strncmp(ERL_ATOM_PTR(msg_type), "call", 4) == 0) { /* call expects a msg back */ /* always at least one argument so we get that out first*/ arg1p = erl_element(2, tuplep); if (strncmp(ERL_ATOM_PTR(fnp), "init", 4) == 0) { printf("init requested\n"); arg2p = erl_element(3, tuplep); resp = gpio_init(arg1p, arg2p); } else if (strncmp(ERL_ATOM_PTR(fnp), "write", 5) == 0) { arg2p = erl_element(3, tuplep); /* @todo implement the real impl here */ resp = gpio_write(arg1p, arg2p); } else if (strncmp(ERL_ATOM_PTR(fnp), "read", 4) == 0) { resp = gpio_read(arg1p); } else if (strncmp(ERL_ATOM_PTR(fnp), "set_int", 7) == 0) { arg2p = erl_element(3, tuplep); /* @todo implement the real impl here */ resp = gpio_set_int(arg1p, fromp, handle_gpio_interrupt, arg2p); } else if (strncmp(ERL_ATOM_PTR(fnp), "release", 7) == 0) { resp = gpio_release(arg1p); } printf("going to send resp: %s\n", ERL_ATOM_PTR(resp)); erl_send(fd_erlang_node, fromp, erl_format("{~w,~w}", refp, resp)); } else if (strncmp(ERL_ATOM_PTR(msg_type), "cast", 4) == 0) { /* cast does not expect a msg back */ } erl_free_term(emsg.from); erl_free_term(emsg.msg); erl_free_term(fromp); erl_free_term(refp); erl_free_term(tuplep); erl_free_term(fnp); erl_free_term(arg1p); erl_free_term(arg2p); erl_free_term(resp); } } } }
int main(int argc, char **argv) { struct in_addr addr; /* 32-bit IP number of host */ int port; /* Listen port number */ int listen; /* Listen socket */ int fd; /* fd to Erlang node */ ErlConnect conn; /* Connection data */ int loop = 1; /* Lopp flag */ int got; /* Result of receive */ unsigned char buf[BUFSIZE]; /* Buffer for incoming message */ ErlMessage emsg; /* Incoming message */ ETERM *fromp, *tuplep, *fnp, *argp, *resp; int res; port = atoi(argv[1]); erl_init(NULL, 0); addr.s_addr = inet_addr("127.0.0.1"); if (erl_connect_xinit("alpha", "cnode", "*****@*****.**", &addr, "secretcookie", 0) == -1) erl_err_quit("erl_connect_xinit"); /* Make a listen socket */ if ((listen = my_listen(port)) <= 0) erl_err_quit("my_listen"); if (erl_publish(port) == -1) erl_err_quit("erl_publish"); if ((fd = erl_accept(listen, &conn)) == ERL_ERROR) erl_err_quit("erl_accept"); fprintf(stderr, "Connected to %s\n\r", conn.nodename); while (loop) { got = erl_receive_msg(fd, buf, BUFSIZE, &emsg); if (got == ERL_TICK) { /* ignore */ } else if (got == ERL_ERROR) { loop = 0; } else { if (emsg.type == ERL_REG_SEND) { fromp = erl_element(2, emsg.msg); tuplep = erl_element(3, emsg.msg); fnp = erl_element(1, tuplep); argp = erl_element(2, tuplep); if (strncmp(ERL_ATOM_PTR(fnp), "foo", 3) == 0) { res = foo(ERL_INT_VALUE(argp)); } else if (strncmp(ERL_ATOM_PTR(fnp), "bar", 3) == 0) { res = bar(ERL_INT_VALUE(argp)); } resp = erl_format("{cnode, ~i}", res); erl_send(fd, fromp, resp); erl_free_term(emsg.from); erl_free_term(emsg.msg); erl_free_term(fromp); erl_free_term(tuplep); erl_free_term(fnp); erl_free_term(argp); erl_free_term(resp); } } } /* while */ }
void *cnode_run() { int fd; /* fd to Erlang node */ int got; /* Result of receive */ unsigned char buf[BUFSIZE]; /* Buffer for incoming message */ ErlMessage emsg; /* Incoming message */ ETERM *uid, *msg; erl_init(NULL, 0); if (erl_connect_init(1, "secretcookie", 0) == -1) erl_err_quit("erl_connect_init"); if ((fd = erl_connect("httpdmaster@localhost")) < 0) erl_err_quit("erl_connect"); fprintf(stderr, "Connected to httpdmaster@localhost\n\r"); struct evbuffer *evbuf; while (1) { got = erl_receive_msg(fd, buf, BUFSIZE, &emsg); if (got == ERL_TICK) { continue; } else if (got == ERL_ERROR) { fprintf(stderr, "ERL_ERROR from erl_receive_msg.\n"); break; } else { if (emsg.type == ERL_REG_SEND) { fprintf(stderr, "type of emsg is ERL_REG_SEND\n"); // get uid and body data from eg: {123, <<"Hello">>} uid = erl_element(1, emsg.msg); msg = erl_element(2, emsg.msg); char *token = (char *) ERL_BIN_PTR(uid); char *body = (char *) ERL_BIN_PTR(msg); int body_len = ERL_BIN_SIZE(msg); char *cached = fetch_memcached(token); int userid = atoi(cached); fprintf(stderr, "memc: %d\n\r", userid); if(clients[userid]){ fprintf(stderr, "Sending %d bytes to token %s\n", body_len, token); evbuf = evbuffer_new(); evbuffer_add(evbuf, (const void*)body, (size_t) body_len); evhttp_send_reply_chunk(clients[userid], evbuf); evhttp_send_reply_end(clients[userid]); evbuffer_free(evbuf); }else{ fprintf(stderr, "Discarding %d bytes to uid %d - user not connected\n", body_len, userid); } free(cached); erl_free_term(emsg.msg); erl_free_term(uid); erl_free_term(msg); } } fprintf(stderr, "."); } pthread_exit(0); }