static int mn_send_delete(int fd, erlang_pid *mnesia, const char *key) { char sbuf[EISMALLBUF]; char *dbuf = NULL; char *msgbuf; int index = 0; int len = strlen(key) + 32; /* 32 is a slight overestimate */ if (len > EISMALLBUF) if (!(dbuf = malloc(len))) return -1; msgbuf = (dbuf ? dbuf : sbuf); /* set up delete message { delete, Key } */ ei_encode_version(msgbuf,&index); ei_encode_tuple_header(msgbuf,&index,2); ei_encode_atom(msgbuf,&index,EI_MNESIA_DELETE); ei_encode_string(msgbuf,&index,key); /* send it */ if (ei_send_encoded(fd,mnesia,msgbuf,index)) { if (dbuf) free(dbuf); return -1; } if (dbuf) free(dbuf); return 0; }
static int mn_send_write(int fd, erlang_pid *mnesia, const char *key, ei_reg_obj *obj) { char sbuf[EISMALLBUF]; char *dbuf = NULL; char *msgbuf; int index = 0; int keylen = strlen(key) + 1; int len = 32 + keylen + obj->size; if (len > EISMALLBUF) if (!(dbuf = malloc(len))) return -1; msgbuf = (dbuf ? dbuf : sbuf); ei_encode_version(msgbuf,&index); ei_encode_tuple_header(msgbuf,&index,6); ei_encode_atom(msgbuf,&index,EI_MNESIA_WRITE); ei_encode_string(msgbuf,&index,key); ei_encode_long(msgbuf,&index,keylen); ei_encode_long(msgbuf,&index,obj->attr); ei_encode_long(msgbuf,&index,obj->size); switch (ei_reg_typeof(obj)) { case EI_INT: ei_encode_long(msgbuf,&index,obj->val.i); break; case EI_FLT: ei_encode_double(msgbuf,&index,obj->val.f); break; case EI_STR: if (obj->size > 0) ei_encode_string(msgbuf,&index,obj->val.s); else ei_encode_long(msgbuf,&index, (long)NULL); /* just the NULL pointer */ break; case EI_BIN: if (obj->size > 0) ei_encode_binary(msgbuf,&index,obj->val.p,obj->size); else ei_encode_long(msgbuf,&index,(long)(obj->val.p)); /* just the pointer */ break; default: if (dbuf) free(dbuf); return -1; } /* send it */ if (ei_send_encoded(fd,mnesia,msgbuf,index)) { if (dbuf) free(dbuf); return -1; } if (dbuf) free(dbuf); return 0; }
int oe_server_receive(CORBA_Environment *env, oe_map_t *map) { int res = 0, loop = 1; erlang_msg msg; while (res >= 0 && loop > 0) { res = ei_receive_encoded(env->_fd, &env->_inbuf, &env->_inbufsz, &msg, &env->_iin); switch(res) { case ERL_SEND: case ERL_REG_SEND: oe_exec_switch(NULL, env, map); switch(env->_major) { case CORBA_NO_EXCEPTION: break; case CORBA_SYSTEM_EXCEPTION: /* XXX stderr */ fprintf(stderr, "Request failure, reason : %s\n", (char *) CORBA_exception_value(env)); CORBA_exception_free(env); break; default: /* Should not happen */ CORBA_exception_free(env); break; } /* send reply */ /* XXX We are required to set env->_iout = 0 if oneway?? */ if (env->_iout > 0) ei_send_encoded(env->_fd, &env->_caller, env->_outbuf, env->_iout); loop = 0; break; case ERL_TICK: break; default: /* XXX */ if (res < 0) { fprintf(stderr, "Result negative: %d\n", res); loop = 0; } break; } } return 0; }
static int mn_send_commit(int fd, erlang_pid *mnesia, erlang_pid *self) { char buf[EISMALLBUF]; char *bufp=buf; char string[256]; int index = 0; int version,arity; int msglen; erlang_msg msg; int i; /* set up commit message { commit, self() } */ ei_encode_version(buf,&index); ei_encode_tuple_header(buf,&index,2); ei_encode_atom(buf,&index,EI_MNESIA_COMMIT); ei_encode_pid(buf,&index,self); /* send it */ if (ei_send_encoded(fd,mnesia,buf,index)) return -1; /* get reply */ while (1) { index = EISMALLBUF; if (!(i=ei_recv_internal(fd,&bufp,&index,&msg,&msglen,1,0))) continue; else if (i < 0) return -1; else break; } if (i == ERL_SEND) { index = 0; if (ei_decode_version(buf,&index,&version) || ei_decode_tuple_header(buf,&index,&arity) || ei_decode_atom(buf,&index,string)) return -1; if (!strcmp(string,"ok")) return 0; } /* wrong message type */ return -1; }
/* Client send message (Erlang distribution protocol) */ static int oe_send(CORBA_Environment *env) { if (strlen(env->_regname) == 0) { if (ei_send_encoded(env->_fd, env->_to_pid, env->_outbuf, env->_iout) < 0) { /* XXX Cannot send to peer? */ CORBA_exc_set(env, CORBA_SYSTEM_EXCEPTION, NO_RESPONSE, "Cannot connect to server"); return -1; } } else { if (ei_send_reg_encoded(env->_fd, env->_from_pid, env->_regname, env->_outbuf, env->_iout) < 0) { /* XXX Cannot send to peer? */ CORBA_exc_set(env, CORBA_SYSTEM_EXCEPTION, NO_RESPONSE, "Cannot connect to server"); return -1; } } return 0; }
int main(void) #endif { ErlConnect conp; Erl_IpAddr thisipaddr = (Erl_IpAddr)0; FILE *fp = (FILE *)0; char* charp = "foo"; double *doublep = NULL; double doublex = 0.0; ei_cnode xec; ei_reg *ei_regp = NULL; ei_term eterm; ei_x_buff eix; erlang_big *bigp = NULL; erlang_fun efun; erlang_msg *msgp = NULL; erlang_msg emsg; erlang_pid *pidp = NULL; erlang_pid epid; erlang_port eport; erlang_ref eref; erlang_trace etrace; int *intp = NULL; int intx = 0; long *longp = NULL; long longx = 0; short creation = 0; struct ei_reg_stat *ei_reg_statp = NULL; struct ei_reg_tabstat *ei_reg_tabstatp = NULL; struct hostent *hostp = NULL; unsigned char * ucharp = (unsigned char *)"foo"; unsigned long *ulongp = NULL; unsigned long ulongx = 0; void *voidp = NULL; #ifndef VXWORKS EI_LONGLONG *longlongp = (EI_LONGLONG*)NULL; EI_LONGLONG longlongx = 0; EI_ULONGLONG *ulonglongp = (EI_ULONGLONG*)NULL; EI_ULONGLONG ulonglongx = 0; #endif enum erlang_char_encoding enc; intx = erl_errno; ei_connect_init(&xec, charp, charp, creation); ei_connect_xinit (&xec, charp, charp, charp, thisipaddr, charp, creation); ei_connect(&xec, charp); ei_xconnect (&xec, thisipaddr, charp); ei_receive(intx, ucharp, intx); ei_receive_msg(intx, &emsg, &eix); ei_xreceive_msg(intx, &emsg, &eix); ei_send(intx, &epid, charp, intx); ei_reg_send(&xec, intx, charp, charp, intx); ei_rpc(&xec, intx, charp, charp, charp, intx, &eix); ei_rpc_to(&xec, intx, charp, charp, charp, intx); ei_rpc_from(&xec, intx, intx, &emsg, &eix); ei_publish(&xec, intx); ei_accept(&xec, intx, &conp); ei_unpublish(&xec); ei_thisnodename(&xec); ei_thishostname(&xec); ei_thisalivename(&xec); ei_self(&xec); ei_gethostbyname(charp); ei_gethostbyaddr(charp, intx, intx); ei_gethostbyname_r(charp, hostp, charp, intx, intp); ei_gethostbyaddr_r(charp, intx, intx, hostp, charp, intx, intp); ei_encode_version(charp, intp); ei_x_encode_version(&eix); ei_encode_long(charp, intp, longx); ei_x_encode_long(&eix, longx); ei_encode_ulong(charp, intp, ulongx); ei_x_encode_ulong(&eix, ulongx); ei_encode_double(charp, intp, doublex); ei_x_encode_double(&eix, doublex); ei_encode_boolean(charp, intp, intx); ei_x_encode_boolean(&eix, intx); ei_encode_char(charp, intp, 'a'); ei_x_encode_char(&eix, 'a'); ei_encode_string(charp, intp, charp); ei_encode_string_len(charp, intp, charp, intx); ei_x_encode_string(&eix, charp); ei_x_encode_string_len(&eix, charp, intx); ei_encode_atom(charp, intp, charp); ei_encode_atom_as(charp, intp, charp, ERLANG_LATIN1, ERLANG_UTF8); ei_encode_atom_len(charp, intp, charp, intx); ei_encode_atom_len_as(charp, intp, charp, intx, ERLANG_ASCII, ERLANG_LATIN1); ei_x_encode_atom(&eix, charp); ei_x_encode_atom_as(&eix, charp, ERLANG_LATIN1, ERLANG_UTF8); ei_x_encode_atom_len(&eix, charp, intx); ei_x_encode_atom_len_as(&eix, charp, intx, ERLANG_LATIN1, ERLANG_UTF8); ei_encode_binary(charp, intp, (void *)0, longx); ei_x_encode_binary(&eix, (void*)0, intx); ei_encode_pid(charp, intp, &epid); ei_x_encode_pid(&eix, &epid); ei_encode_fun(charp, intp, &efun); ei_x_encode_fun(&eix, &efun); ei_encode_port(charp, intp, &eport); ei_x_encode_port(&eix, &eport); ei_encode_ref(charp, intp, &eref); ei_x_encode_ref(&eix, &eref); ei_encode_trace(charp, intp, &etrace); ei_x_encode_trace(&eix, &etrace); ei_encode_tuple_header(charp, intp, intx); ei_x_encode_tuple_header(&eix, longx); ei_encode_list_header(charp, intp, intx); ei_x_encode_list_header(&eix, longx); /* #define ei_encode_empty_list(buf,i) ei_encode_list_header(buf,i,0) */ ei_x_encode_empty_list(&eix); ei_get_type(charp, intp, intp, intp); ei_get_type_internal(charp, intp, intp, intp); ei_decode_version(charp, intp, intp); ei_decode_long(charp, intp, longp); ei_decode_ulong(charp, intp, ulongp); ei_decode_double(charp, intp, doublep); ei_decode_boolean(charp, intp, intp); ei_decode_char(charp, intp, charp); ei_decode_string(charp, intp, charp); ei_decode_atom(charp, intp, charp); ei_decode_atom_as(charp, intp, charp, MAXATOMLEN_UTF8, ERLANG_WHATEVER, &enc, &enc); ei_decode_binary(charp, intp, (void *)0, longp); ei_decode_fun(charp, intp, &efun); free_fun(&efun); ei_decode_pid(charp, intp, &epid); ei_decode_port(charp, intp, &eport); ei_decode_ref(charp, intp, &eref); ei_decode_trace(charp, intp, &etrace); ei_decode_tuple_header(charp, intp, intp); ei_decode_list_header(charp, intp, intp); ei_decode_ei_term(charp, intp, &eterm); ei_print_term(fp, charp, intp); ei_s_print_term(&charp, charp, intp); ei_x_format(&eix, charp); ei_x_format_wo_ver(&eix, charp); ei_x_new(&eix); ei_x_new_with_version(&eix); ei_x_free(&eix); ei_x_append(&eix, &eix); ei_x_append_buf(&eix, charp, intx); ei_skip_term(charp, intp); ei_reg_open(intx); ei_reg_resize(ei_regp, intx); ei_reg_close(ei_regp); ei_reg_setival(ei_regp, charp, longx); ei_reg_setfval(ei_regp, charp, doublex); ei_reg_setsval(ei_regp, charp, charp); ei_reg_setpval(ei_regp, charp, voidp, intx); ei_reg_setval(ei_regp, charp, intx); ei_reg_getival(ei_regp, charp); ei_reg_getfval(ei_regp, charp); ei_reg_getsval(ei_regp, charp); ei_reg_getpval(ei_regp, charp, intp); ei_reg_getval(ei_regp, charp, intx); ei_reg_markdirty(ei_regp, charp); ei_reg_delete(ei_regp, charp); ei_reg_stat(ei_regp, charp, ei_reg_statp); ei_reg_tabstat(ei_regp, ei_reg_tabstatp); ei_reg_dump(intx, ei_regp, charp, intx); ei_reg_restore(intx, ei_regp, charp); ei_reg_purge(ei_regp); #if defined(HAVE_GMP_H) && defined(HAVE_LIBGMP) { mpz_t obj; ei_decode_bignum(charp, intp, obj); ei_encode_bignum(charp, intp, obj); ei_x_encode_bignum(&eix, obj); } #endif /* HAVE_GMP_H && HAVE_LIBGMP */ #ifndef VXWORKS ei_decode_longlong(charp, intp, longlongp); ei_decode_ulonglong(charp, intp, ulonglongp); ei_encode_longlong(charp, intp, longlongx); ei_encode_ulonglong(charp, intp, ulonglongx); ei_x_encode_longlong(&eix, longlongx); ei_x_encode_ulonglong(&eix, ulonglongx); #endif #ifdef USE_EI_UNDOCUMENTED ei_decode_intlist(charp, intp, longp, intp); ei_receive_encoded(intx, &charp, intp, msgp, intp); ei_send_encoded(intx, pidp, charp, intx); ei_send_reg_encoded(intx, pidp, charp, charp, intx); ei_decode_big(charp, intp, bigp); ei_big_comp(bigp, bigp); ei_big_to_double(bigp, doublep); ei_small_to_big(intx, bigp); ei_alloc_big(intx); ei_free_big(bigp); #endif /* USE_EI_UNDOCUMENTED */ return BUFSIZ + EAGAIN + EHOSTUNREACH + EIO + EI_BIN + EI_DELET + EI_DIRTY + EI_FLT + EI_FORCE + EI_INT + EI_NOPURGE + EI_STR + EMSGSIZE + ENOMEM + ERL_ERROR + ERL_EXIT + ERL_LINK + ERL_MSG + ERL_NO_TIMEOUT + ERL_REG_SEND + ERL_SEND + ERL_TICK + ERL_TIMEOUT + ERL_UNLINK + ETIMEDOUT + MAXATOMLEN; }
int ei_reg_restore(int fd, ei_reg *reg, const char *mntab) { int i,j; char tag[32]; char sbuf[EISMALLBUF]; char *dbuf = NULL; char *msgbuf = NULL; char *keybuf = NULL; erlang_pid self; erlang_pid mnesia = {"",0,0,0}; erlang_msg msg; int index = 0; int len = 0; int msglen; int version = 0; int arity = 0; long count = 0; long maxkey = 0; long maxobj = 0; ei_cnode *ec; if (!reg || !mntab) return -1; /* return EI_BADARG; */ /* make a self pid */ if ((ec = ei_fd_to_cnode(fd)) == NULL) { return -1; } strcpy(self.node,ei_thisnodename(ec)); self.num = fd; self.serial = 0; self.creation = ei_thiscreation(ec); if (mn_start_restore(fd,&self,&mnesia,mntab,&count,&maxkey,&maxobj)) { /* send exit *only* if we have pid */ if (mnesia.node[0]) ei_send_exit(fd,&self,&mnesia,"bad response from rpc start"); return -1; } if (count <= 0) { ei_send_exit(fd,&self,&mnesia,"nothing to do"); return 0; } /* make sure receive buffer can handle largest expected message */ len = maxkey + maxobj + 512; if (len > EISMALLBUF) if (!(dbuf = malloc(len))) { ei_send_exit(fd,&self,&mnesia,"cannot allocate space for incoming data"); return -1; } msgbuf = (dbuf ? dbuf : sbuf); /* allocate space for largest key */ if (!(keybuf = malloc(maxkey+1))) goto restore_failure; /* get this ball rolling */ index = 0; ei_encode_version(msgbuf,&index); ei_encode_tuple_header(msgbuf,&index,2); ei_encode_atom(msgbuf,&index,"send_records"); ei_encode_pid(msgbuf,&index,&self); if (ei_send_encoded(fd,&mnesia,msgbuf,index)) goto restore_failure; /* read as much as possible, until count or EXIT */ for (i=0; i<count; i++) { index = len; while ((j = ei_recv_internal(fd,&msgbuf,&index,&msg,&msglen,1,0)) == 0) index = len; if (j<0) goto restore_failure; /* decode the first part of the message */ index = 0; if ((msg.msgtype != ERL_SEND) || ei_decode_version(msgbuf,&index,&version) || ei_decode_tuple_header(msgbuf,&index,&arity) || (arity != 6) || ei_decode_atom(msgbuf,&index,tag) || strcmp(tag,EI_MNESIA_RECV)) goto restore_failure; /* decode the rest of the message and insert data into table */ if (mn_decode_insert(reg,msgbuf,&index,keybuf)) goto restore_failure; } if (keybuf) free(keybuf); if (dbuf) free(dbuf); /* wait for unlink */ if (mn_unlink(fd)) return -1; /* clear all the dirty bits */ ei_hash_foreach(reg->tab,clean_obj); /* success */ return 0; restore_failure: ei_send_exit(fd,&self,&mnesia,"restore failure"); if (keybuf) free(keybuf); if (dbuf) free(dbuf); return -1; }
int main(int argc, char **argv) #endif { struct hostent *hp; MyTimeval start, stop; int i, fd, ires, tries; CORBA_Environment *env; char *this_node_name = NULL; char *peer_node = NULL; char *cookie = NULL; char host[HOSTNAMESZ + 1]; char this_node[NODENAMESZ + 1]; erlang_msg msg; int status, loop; #ifdef __WIN32__ WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(2, 0); if (WSAStartup(wVersionRequested, &wsaData) != 0) { fprintf(stderr, "Could not load winsock2 v2.0 compatible DLL"); exit(1); } #endif progname = argv[0]; host[HOSTNAMESZ] = '\0'; if (gethostname(host, HOSTNAMESZ) < 0) { fprintf(stderr, "Can't find own hostname\n"); done(1); } if ((hp = gethostbyname(host)) == 0) { fprintf(stderr, "Can't get ip address for host %s\n", host); done(1); } for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-help") == 0) { usage(); done(0); } else if (strcmp(argv[i], "-this-node-name") == 0) { i++; this_node_name = argv[i]; } else if (strcmp(argv[i], "-peer-node") == 0) { i++; peer_node = argv[i]; } else if (strcmp(argv[i], "-cookie") == 0) { i++; cookie = argv[i]; } else { fprintf(stderr, "Error : invalid argument \"%s\"\n", argv[i]); usage(); done(1); } } if (this_node_name == NULL || peer_node == NULL || cookie == NULL) { fprintf(stderr, "Error: missing option\n"); usage(); done(1); } /* Behead hostname at first dot */ for (i=0; host[i] != '\0'; i++) { if (host[i] == '.') { host[i] = '\0'; break; } } sprintf(this_node, "%s@%s", this_node_name, host); fprintf(stderr, "c_server: this node: \"%s\"\n", this_node); fprintf(stderr, "c_server: peer node: \"%s\"\n", peer_node); /* initialize erl_interface */ erl_init(NULL, 0); for (tries = 0; tries < MAXTRIES; tries++) { /* connect to peer node */ ires = erl_connect_xinit(host, this_node_name, this_node, (struct in_addr *)*hp->h_addr_list, cookie, 0); fprintf(stderr, "c_server: erl_connect_xinit(): %d\n", ires); fd = erl_connect(peer_node); fprintf(stderr, "c_server: erl_connect(): %d\n", fd); if (fd >= 0) break; fprintf(stderr, "c_server: cannot connect, retrying\n"); } if (fd < 0) { fprintf(stderr, "c_server: cannot connect, exiting\n"); done(1); } env = CORBA_Environment_alloc(INBUFSZ, OUTBUFSZ); env->_fd = fd; status = 1; loop = 1; my_gettimeofday(&start); while (status >= 0 && loop > 0) { status = ei_receive_encoded(env->_fd, &env->_inbuf, &env->_inbufsz, &msg, &env->_iin); switch(status) { case ERL_SEND: case ERL_REG_SEND: /* get result */ m_i__switch(NULL, env); switch(env->_major) { case CORBA_NO_EXCEPTION: break; case CORBA_SYSTEM_EXCEPTION: fprintf(stderr, "Request failure, reason : %s\n", (char *) CORBA_exception_value(env)); CORBA_exception_free(env); break; default: /* Should not happen */ CORBA_exception_free(env); break; } /* send back result data */ if (env->_iout > 0) ei_send_encoded(env->_fd, &env->_caller, env->_outbuf, env->_iout); loop = 0; break; case ERL_TICK: break; default: if (status < 0) { fprintf(stderr, "Status negative: %d\n", status); loop = 0; } break; } } my_gettimeofday(&stop); showtime(&start, &stop); erl_close_connection(fd); CORBA_free(env->_inbuf); CORBA_free(env->_outbuf); CORBA_free(env); if (status < 0) done(-status); else done(0); }
static void server_loop(int sd, int epmd_fd) { ErlConnect conn; erlang_msg msg; int status=1; CORBA_Environment *env; /* Create and init CORBA_Environment */ env = CORBA_Environment_alloc(INBUFSZ,OUTBUFSZ); while (status >= 0) { status = 1; if ((env->_fd = erl_accept(sd,&conn)) < 0) { /* error */ fprintf(stderr,"Accept failed: %s\n",strerror(errno)); } else { /* connection */ fprintf(stderr,"Accepted connection from %s\n",conn.nodename); while (status >= 0) { /* write message to buffer */ status = ei_receive_encoded(env->_fd, &env->_inbuf, &env->_inbufsz, &msg, &env->_iin); switch(status) { case ERL_SEND: case ERL_REG_SEND : /* do transaction with fd */ rmod_random__switch(NULL,env); switch(env->_major) { case CORBA_NO_EXCEPTION: /* Success */ break; case CORBA_SYSTEM_EXCEPTION: /* System exception */ printf("Request failure, reason : %s\n",(char *) CORBA_exception_value(env)); CORBA_exception_free(env); break; default: /* Should not come here */ CORBA_exception_free(env); break; } /* send outdata */ if (env->_iout > 0) ei_send_encoded(env->_fd,&env->_caller,env->_outbuf,env->_iout); break; case ERL_TICK : break; default : /* < 0 */ printf("Connection terminated\n"); break; } } } status=0; /* restart */ } /* close file descriptors */ terminate(&env->_fd, &sd, &epmd_fd); /* Free env & buffers */ CORBA_free(env->_inbuf); CORBA_free(env->_outbuf); CORBA_free(env); }