/** * Insert a row into a specified table, update on duplicate key. * \param _h structure representing database connection * \param _k key names * \param _v values of the keys * \param _n number of key=value pairs */ int erlang_srdb1_insert_update(const db1_con_t* _h, const db_key_t* _k, const db_val_t* _v, const int _n) { ei_x_buff argbuf; int retcode; if ((!_h) || (!_k) || (!_v) || (!_n)) { LM_ERR("invalid parameter value\n"); return -1; } LM_DBG("erlang_srdb1_insert_update table %.*s\n",CON_TABLE(_h)->len, CON_TABLE(_h)->s); ei_x_new(&argbuf); //encode tuple {db_op, table, [cols], [keys], [vals]} ei_x_encode_tuple_header(&argbuf, 5); ei_x_encode_atom(&argbuf,"insert_update"); ei_x_encode_atom_len(&argbuf,CON_TABLE(_h)->s,CON_TABLE(_h)->len); ei_x_encode_list_header(&argbuf, 0); //_c // ei_x_encode_list_header(&argbuf, 0); //_k srdb1_encode_k(_k, NULL, _v, _n, &argbuf); //_k srdb1_encode_v(_k, _v, _n, &argbuf); //_v retcode=erl_bind.do_erlang_call(&(CON_ERLANG(_h)->con),&(CON_ERLANG(_h)->regname), &argbuf, NULL /*&retbuf*/); ei_x_free(&argbuf); if (retcode<0) { // if(retbuf.buff) shm_free(retbuf.buff); return retcode; } return 0; }
static switch_status_t handle_msg_tuple(listener_t *listener, erlang_msg * msg, ei_x_buff * buf, ei_x_buff * rbuf) { char tupletag[MAXATOMLEN]; int arity; switch_status_t ret = SWITCH_STATUS_SUCCESS; ei_decode_tuple_header(buf->buff, &buf->index, &arity); if (ei_decode_atom(buf->buff, &buf->index, tupletag)) { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badarg"); } else { if (!strncmp(tupletag, "fetch_reply", MAXATOMLEN)) { ret = handle_msg_fetch_reply(listener, buf, rbuf); } else if (!strncmp(tupletag, "set_log_level", MAXATOMLEN)) { ret = handle_msg_set_log_level(listener, arity, buf, rbuf); } else if (!strncmp(tupletag, "event", MAXATOMLEN)) { ret = handle_msg_event(listener, arity, buf, rbuf); } else if (!strncmp(tupletag, "session_event", MAXATOMLEN)) { ret = handle_msg_session_event(listener, msg, arity, buf, rbuf); } else if (!strncmp(tupletag, "nixevent", MAXATOMLEN)) { ret = handle_msg_nixevent(listener, arity, buf, rbuf); } else if (!strncmp(tupletag, "session_nixevent", MAXATOMLEN)) { ret = handle_msg_session_nixevent(listener, msg, arity, buf, rbuf); } else if (!strncmp(tupletag, "api", MAXATOMLEN)) { ret = handle_msg_api(listener, msg, arity, buf, rbuf); } else if (!strncmp(tupletag, "bgapi", MAXATOMLEN)) { ret = handle_msg_bgapi(listener, msg, arity, buf, rbuf); } else if (!strncmp(tupletag, "sendevent", MAXATOMLEN)) { ret = handle_msg_sendevent(listener, arity, buf, rbuf); } else if (!strncmp(tupletag, "sendmsg", MAXATOMLEN)) { ret = handle_msg_sendmsg(listener, arity, buf, rbuf); } else if (!strncmp(tupletag, "bind", MAXATOMLEN)) { ret = handle_msg_bind(listener, msg, buf, rbuf); } else if (!strncmp(tupletag, "handlecall", MAXATOMLEN)) { ret = handle_msg_handlecall(listener, msg, arity, buf, rbuf); } else if (!strncmp(tupletag, "rex", MAXATOMLEN)) { ret = handle_msg_rpcresponse(listener, msg, arity, buf, rbuf); } else { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "undef"); } } return ret; }
static switch_status_t handle_msg_event(listener_t *listener, int arity, ei_x_buff * buf, ei_x_buff * rbuf) { char atom[MAXATOMLEN]; if (arity == 1) { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badarg"); } else { int custom = 0; switch_event_types_t type; int i = 0; if (!switch_test_flag(listener, LFLAG_EVENTS)) { switch_set_flag_locked(listener, LFLAG_EVENTS); } for (i = 1; i < arity; i++) { if (!ei_decode_atom(buf->buff, &buf->index, atom)) { if (custom) { switch_core_hash_insert(listener->event_hash, atom, MARKER); } else if (switch_name_event(atom, &type) == SWITCH_STATUS_SUCCESS) { if (type == SWITCH_EVENT_ALL) { uint32_t x = 0; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "ALL events enabled\n"); for (x = 0; x < SWITCH_EVENT_ALL; x++) { listener->event_list[x] = 1; } } if (type <= SWITCH_EVENT_ALL) { listener->event_list[type] = 1; } if (type == SWITCH_EVENT_CUSTOM) { custom++; } } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "enable event %s\n", atom); } } ei_x_encode_atom(rbuf, "ok"); } return SWITCH_STATUS_SUCCESS; }
static switch_status_t handle_msg_api(listener_t *listener, erlang_msg * msg, int arity, ei_x_buff * buf, ei_x_buff * rbuf) { char api_cmd[MAXATOMLEN]; int type; int size; char *arg; switch_bool_t fail = SWITCH_FALSE; if (arity < 3) { fail = SWITCH_TRUE; } ei_get_type(buf->buff, &buf->index, &type, &size); if ((size > (sizeof(api_cmd) - 1)) || ei_decode_atom(buf->buff, &buf->index, api_cmd)) { fail = SWITCH_TRUE; } ei_get_type(buf->buff, &buf->index, &type, &size); arg = malloc(size + 1); if (ei_decode_string_or_binary(buf->buff, &buf->index, size, arg)) { fail = SWITCH_TRUE; } if (!fail) { struct api_command_struct acs = { 0 }; acs.listener = listener; acs.api_cmd = api_cmd; acs.arg = arg; acs.bg = 0; acs.pid = msg->from; api_exec(NULL, (void *) &acs); switch_safe_free(arg); /* don't reply */ return SWITCH_STATUS_FALSE; } else { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badarg"); return SWITCH_STATUS_SUCCESS; } }
static switch_status_t handle_msg_bind(listener_t *listener, erlang_msg * msg, ei_x_buff * buf, ei_x_buff * rbuf) { /* format is (result|config|directory|dialplan|phrases) */ char sectionstr[MAXATOMLEN]; switch_xml_section_t section; if (ei_decode_atom(buf->buff, &buf->index, sectionstr) || !(section = switch_xml_parse_section_string(sectionstr))) { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badarg"); } else { struct erlang_binding *binding, *ptr; if (!(binding = switch_core_alloc(listener->pool, sizeof(*binding)))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error\n"); ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badmem"); } else { binding->section = section; binding->process.type = ERLANG_PID; binding->process.pid = msg->from; binding->listener = listener; switch_thread_rwlock_wrlock(globals.listener_rwlock); for (ptr = bindings.head; ptr && ptr->next; ptr = ptr->next); if (ptr) { ptr->next = binding; } else { bindings.head = binding; } switch_xml_set_binding_sections(bindings.search_binding, switch_xml_get_binding_sections(bindings.search_binding) | section); switch_thread_rwlock_unlock(globals.listener_rwlock); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "sections %d\n", switch_xml_get_binding_sections(bindings.search_binding)); ei_link(listener, ei_self(listener->ec), &msg->from); ei_x_encode_atom(rbuf, "ok"); } } return SWITCH_STATUS_SUCCESS; }
int ei_x_encode_kstring(ei_x_buff* types, ei_x_buff* values, K r, QOpts* opts) { EI(ei_x_encode_atom(types, "string")); if(r->n == 0) { EI(ei_x_encode_empty_list(values)); } else { EI(ei_x_encode_string_len(values, (const char*)kC(r), r->n)); } return 0; }
int ei_x_encode_time(ei_x_buff* types, ei_x_buff* values, K r, QOpts* opts) { EI(ei_x_encode_atom(types, "time")); int v = r->i; if(opts->day_seconds_is_q_time) { v = msec_to_sec(v); } EI(ei_x_encode_ki_val(values, v)); return 0; }
static void error() { ei_x_buff result; D("%s", "Error !"); check(ei_x_new_with_version(&result)); check(ei_x_encode_tuple_header(&result, 2)); check(ei_x_encode_atom(&result, "error")); // if (recording) assert(recording); check(ei_x_encode_atom(&result, "already_started")); //else //check(ei_x_encode_atom(&result, "not_started")); write_cmd(&result); ei_x_free(&result); }
static void ok() { ei_x_buff result; check(ei_x_new_with_version(&result)); check(ei_x_encode_atom(&result, "ok")); write_cmd(&result); ei_x_free(&result); }
int ei_x_encode_dict_impl(ei_x_buff* types, ei_x_buff* values, const char* t, K r, QOpts* opts) { EI(ei_x_encode_tuple_header(types, r->n+1)); EI(ei_x_encode_atom(types, t)); EI(ei_x_encode_tuple_header(values, r->n)); int i; for(i=0; i<r->n; ++i) { EI(ei_x_encode_k_tv(types, values, kK(r)[i], opts)); } return 0; }
int ei_x_encode_same_list_time(ei_x_buff* types, ei_x_buff* values, K r, QOpts* opts) { if(opts->day_seconds_is_q_time) { EI(ei_x_encode_tuple_header(types, 2)); EI(ei_x_encode_atom(types, "list")); EI(ei_x_encode_atom(types, "time")); if(r->n > 0) { EI(ei_x_encode_list_header(values, r->n)); int i; for(i=0; i<r->n; ++i) { EI(ei_x_encode_ki_val(values, msec_to_sec(kI(r)[i]))); } } EI(ei_x_encode_empty_list(values)); } else { EI(ei_x_encode_same_list_integer(types, values, "time", r, opts)); } return 0; }
int ei_x_encode_same_list_datetime(ei_x_buff* types, ei_x_buff* values, K r, QOpts* opts) { if(opts->unix_timestamp_is_q_datetime) { EI(ei_x_encode_tuple_header(types, 2)); EI(ei_x_encode_atom(types, "list")); EI(ei_x_encode_atom(types, "datetime")); if(r->n > 0) { EI(ei_x_encode_list_header(values, r->n)); int i; for(i=0; i<r->n; ++i) { EI(ei_x_encode_longlong(values, datetime_to_unix_timestamp(kF(r)[i]))); } } EI(ei_x_encode_empty_list(values)); } else { EI(ei_x_encode_same_list_float(types, values, "datetime", r, opts)); } return 0; }
int ei_x_encode_datetime(ei_x_buff* types, ei_x_buff* values, K r, QOpts* opts) { if(opts->unix_timestamp_is_q_datetime) { long v = datetime_to_unix_timestamp(r->f); EI(ei_x_encode_atom(types, "datetime")); EI(ei_x_encode_long(values, v)); } else { EI(ei_x_encode_kf(types, values, "datetime", r, opts)); } return 0; }
static ei_x_buff make_error(const char* text){ ei_x_buff x; ei_x_new_with_version(&x); ei_x_encode_tuple_header(&x,2); ei_x_encode_atom(&x,"error"); ei_x_encode_string(&x,text); return x; }
static switch_status_t handle_msg_nixevent(listener_t *listener, int arity, ei_x_buff * buf, ei_x_buff * rbuf) { char atom[MAXATOMLEN]; if (arity == 1) { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badarg"); } else { int custom = 0; int i = 0; switch_event_types_t type; for (i = 1; i < arity; i++) { if (!ei_decode_atom(buf->buff, &buf->index, atom)) { if (custom) { switch_core_hash_delete(listener->event_hash, atom); } else if (switch_name_event(atom, &type) == SWITCH_STATUS_SUCCESS) { uint32_t x = 0; if (type == SWITCH_EVENT_CUSTOM) { custom++; } else if (type == SWITCH_EVENT_ALL) { for (x = 0; x <= SWITCH_EVENT_ALL; x++) { listener->event_list[x] = 0; } } else { if (listener->event_list[SWITCH_EVENT_ALL]) { listener->event_list[SWITCH_EVENT_ALL] = 0; for (x = 0; x < SWITCH_EVENT_ALL; x++) { listener->event_list[x] = 1; } } listener->event_list[type] = 0; } } } } ei_x_encode_atom(rbuf, "ok"); } return SWITCH_STATUS_SUCCESS; }
void Client::sendAtom(QByteArray procName, QByteArray atom) { ei_x_buff x; ei_x_new_with_version(&x); ei_x_encode_atom(&x, atom.data()); ei_reg_send(m_ec, m_fd, procName.data(), x.buff, x.index); ei_x_free(&x); }
static switch_status_t handle_msg_api(listener_t *listener, erlang_msg * msg, int arity, ei_x_buff * buf, ei_x_buff * rbuf) { char api_cmd[MAXATOMLEN]; char arg[1024]; if (arity < 3 || ei_decode_atom(buf->buff, &buf->index, api_cmd) || ei_decode_string(buf->buff, &buf->index, arg)) { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badarg"); return SWITCH_STATUS_SUCCESS; } else { struct api_command_struct acs = { 0 }; acs.listener = listener; acs.api_cmd = api_cmd; acs.arg = arg; acs.bg = 0; acs.pid = msg->from; api_exec(NULL, (void *) &acs); /* don't reply */ return SWITCH_STATUS_FALSE; } }
static switch_status_t handle_msg_bgapi(listener_t *listener, erlang_msg * msg, int arity, ei_x_buff * buf, ei_x_buff * rbuf) { char api_cmd[MAXATOMLEN]; char arg[ARGLEN]; if (arity < 3 || ei_decode_atom(buf->buff, &buf->index, api_cmd) || ei_decode_string_or_binary(buf->buff, &buf->index, ARGLEN - 1, arg)) { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badarg"); } else { struct api_command_struct *acs = NULL; switch_memory_pool_t *pool; switch_thread_t *thread; switch_threadattr_t *thd_attr = NULL; switch_uuid_t uuid; switch_core_new_memory_pool(&pool); acs = switch_core_alloc(pool, sizeof(*acs)); switch_assert(acs); acs->pool = pool; acs->listener = listener; acs->api_cmd = switch_core_strdup(acs->pool, api_cmd); acs->arg = switch_core_strdup(acs->pool, arg); acs->bg = 1; acs->pid = msg->from; switch_threadattr_create(&thd_attr, acs->pool); switch_threadattr_detach_set(thd_attr, 1); switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE); switch_uuid_get(&uuid); switch_uuid_format(acs->uuid_str, &uuid); switch_thread_create(&thread, thd_attr, api_exec, acs, acs->pool); ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "ok"); _ei_x_encode_string(rbuf, acs->uuid_str); } return SWITCH_STATUS_SUCCESS; }
int ei_x_encode_general_list(ei_x_buff* types, ei_x_buff* values, K r, QOpts* opts) { LOG("ei_x_encode_general_list length "FMT_KN"\n", r->n); EI(ei_x_encode_tuple_header(types, 2)); EI(ei_x_encode_atom(types, "list")); if(r->n > 0) { int all_strings = 1; EI(ei_x_encode_list_header(values, r->n)); int i; for(i=0; i<r->n; ++i) { K elem = kK(r)[i]; if(elem->t != KC) { all_strings = 0; break; } EI(ei_x_encode_string_len(values, (const char*)kC(elem), elem->n)); } if(all_strings) { EI(ei_x_encode_atom(types, "string")); } else { EI(ei_x_encode_list_header(types, r->n)); int j; for(j=0; j<i; ++j) { EI(ei_x_encode_atom(types, "string")); } for(; i<r->n; ++i) { EI(ei_x_encode_k_tv(types, values, kK(r)[i], opts)); } EI(ei_x_encode_empty_list(types)); } } else { EI(ei_x_encode_empty_list(types)); } EI(ei_x_encode_empty_list(values)); return 0; }
static switch_status_t handle_msg_set_log_level(listener_t *listener, int arity, ei_x_buff * buf, ei_x_buff * rbuf) { switch_log_level_t ltype = SWITCH_LOG_DEBUG; char loglevelstr[MAXATOMLEN]; if (arity != 2 || ei_decode_atom(buf->buff, &buf->index, loglevelstr)) { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badarg"); } else { ltype = switch_log_str2level(loglevelstr); if (ltype && ltype != SWITCH_LOG_INVALID) { listener->level = ltype; ei_x_encode_atom(rbuf, "ok"); } else { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badarg"); } } return SWITCH_STATUS_SUCCESS; }
/* function to spawn a process on a remote node */ int ei_spawn(struct ei_cnode_s *ec, int sockfd, erlang_ref * ref, char *module, char *function, int argc, char **argv) { int i; ei_x_buff buf; ei_x_new_with_version(&buf); ei_x_encode_tuple_header(&buf, 3); ei_x_encode_atom(&buf, "$gen_call"); ei_x_encode_tuple_header(&buf, 2); ei_x_encode_pid(&buf, ei_self(ec)); ei_init_ref(ec, ref); ei_x_encode_ref(&buf, ref); ei_x_encode_tuple_header(&buf, 5); ei_x_encode_atom(&buf, "spawn"); ei_x_encode_atom(&buf, module); ei_x_encode_atom(&buf, function); /* argument list */ if (argc < 0) { ei_x_encode_list_header(&buf, argc); for (i = 0; i < argc && argv[i]; i++) { ei_x_encode_atom(&buf, argv[i]); } } ei_x_encode_empty_list(&buf); /*if (i != argc - 1) { */ /* horked argument list */ /*} */ ei_x_encode_pid(&buf, ei_self(ec)); /* should really be a valid group leader */ #ifdef EI_DEBUG ei_x_print_reg_msg(&buf, "net_kernel", 1); #endif return ei_reg_send(ec, sockfd, "net_kernel", buf.buff, buf.index); }
static gboolean get_fpointer(char *cmd, ei_x_buff *xbuf, gpointer* poi) { extern GModule* gmod; if ( ! g_module_supported() ) g_critical("gtkNode requires working gmodule"); if ( ! gmod ) gmod = g_module_open(NULL,0); /* "gtknode_gtk.so" */ if ( g_module_symbol(gmod, cmd, poi) ) return TRUE; g_warning("could not find '%s'.", cmd); gn_enc_2_error(xbuf, "no_such_function"); ei_x_encode_atom(xbuf, cmd); return FALSE; }
int srdb1_encode_c(const db_key_t* _c, const int _nc, ei_x_buff *argbuf){ int i; if(_c) { ei_x_encode_list_header(argbuf, _nc); for(i = 0; i < _nc; i++) { ei_x_encode_atom_len(argbuf,_c[i]->s,_c[i]->len); } ei_x_encode_empty_list(argbuf); } else { ei_x_encode_atom(argbuf,"all"); } return 0; }
ETERM * write_to_png_stream(ETERM* arg, int c_node) { cairo_context * const ctx = get_cairo_context(arg); if(!ctx) return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT); struct png_data out = {}; const int status = cairo_surface_write_to_png_stream(ctx->sf, write_cb, &out); ETERM *term = NULL; ei_x_buff req; ei_x_new_with_version(&req); ei_x_encode_tuple_header(&req, 3); ei_x_encode_atom(&req, "c_node"); ei_x_encode_long(&req, c_node); ei_x_encode_tuple_header(&req, 2); ei_x_encode_atom(&req, "ok"); ei_x_encode_binary(&req, out.buf, out.written); int index = 0; ei_decode_term(req.buff, &index, &term); ei_x_free(&req); free(out.buf); return term; }
static int delete_sec_context(char *buf, int index, ei_x_buff *presult) { ei_x_buff result = *presult; /* {delete_sec_context, Idx} -> {ok, } */ long idx; OM_uint32 maj_stat, min_stat; EI(ei_decode_long(buf, &index, &idx)); if (idx < 0 || idx >= MAX_SESSIONS || !g_sessions[idx] || g_sessions[idx] == GSS_C_NO_CONTEXT) ENCODE_ERROR("bad_instance"); maj_stat = gss_delete_sec_context(&min_stat, &g_sessions[idx], GSS_C_NO_BUFFER); g_sessions[idx] = NULL; if (!GSS_ERROR(maj_stat)) { EI(ei_x_encode_atom(&result, "ok") || ei_x_encode_atom(&result, "done") ); } else { fprintf(stderr, "gss_delete_sec_context: %08x", maj_stat); gss_print_errors(min_stat); EI(ei_x_encode_atom(&result, "error") || ei_x_encode_long(&result, maj_stat)); } error: *presult = result; return 0; }
bool CCESUT::TradeUpdate(PTradeUpdateTxnInput pTxnInput) { ei_x_buff x; ei_x_new_with_version(&x); ei_x_encode_tuple_header(&x, 2); { ei_x_encode_atom(&x, "ok"); ei_x_encode_tuple_header(&x, 2); { ei_x_encode_atom(&x, APP ".BH.TU"); m_Encoder.encode(&x, *pTxnInput); } } int result = driver_output(m_ErlDrvPort, x.buff, x.index); #ifdef _TRACE cout << "[TU] ! " << *pTxnInput << ", result=" << result << '\r' << endl; #endif ei_x_free(&x); return result; }
int ei_x_encode_table(ei_x_buff* types, ei_x_buff* values, K r, QOpts* opts) { EI(ei_x_encode_tuple_header(types, 2)); EI(ei_x_encode_atom(types, "table")); EI(ei_x_encode_tuple_header(values, 2)); ei_x_buff column_types; ei_x_new_with_version(&column_types); EIC(ei_x_encode_k_tv(&column_types, values, kK(r->k)[0], opts), ei_x_free(&column_types)); ei_x_free(&column_types); EI(ei_x_encode_k_tv(types, values, kK(r->k)[1], opts)); return 0; }
/* {handlecall,<uuid>,<handler process registered name>} or {handlecall,<uuid>} to send messages back to the sender */ static switch_status_t handle_msg_handlecall(listener_t *listener, erlang_msg * msg, int arity, ei_x_buff * buf, ei_x_buff * rbuf) { char reg_name[MAXATOMLEN]; char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; if (arity < 2 || arity > 3 || (arity == 3 && ei_decode_atom(buf->buff, &buf->index, reg_name)) || ei_decode_string_or_binary(buf->buff, &buf->index, SWITCH_UUID_FORMATTED_LENGTH, uuid_str)) { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badarg"); } else { switch_core_session_t *session; if (!zstr_buf(uuid_str)) { if ((session = switch_core_session_locate(uuid_str))) { /* create a new session list element and attach it to this listener */ if ((arity == 2 && attach_call_to_pid(listener, &msg->from, session)) || (arity == 3 && attach_call_to_registered_process(listener, reg_name, session))) { ei_x_encode_atom(rbuf, "ok"); } else { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "session_attach_failed"); } /* release the lock returned by session locate */ switch_core_session_rwunlock(session); } else { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "badsession"); } } else { ei_x_encode_tuple_header(rbuf, 2); ei_x_encode_atom(rbuf, "error"); ei_x_encode_atom(rbuf, "baduuid"); } } return SWITCH_STATUS_SUCCESS; }
/* tests de tipos simples */ int test_performance_atom(char *atom, int times) { ei_x_buff output; ei_x_buff input; erlang_msg msg; int version; int ei_res; int index; char decoded_atom[MAXATOMLEN]; // Inicializa buffers ei_x_new_with_version(&output); ei_x_new(&input); // Codifica ei_x_encode_tuple_header(&output, 2); ei_x_encode_pid(&output, &local_pid); ei_x_encode_atom(&output, atom); for (int i = 0; i<times; i++) { if (ei_reg_send(&ec, connection_fd, REMOTE_SERVER_NAME, output.buff, output.index) < 0) { return 1; } do { ei_res = ei_xreceive_msg(connection_fd, &msg, &input); } while(ei_res == ERL_TICK); if (ei_res == ERL_ERROR) { return -1; } index = 0; if (ei_decode_version(input.buff, &index, &version) < 0) { std::cout << "failed decoding version \n"; return 1; } if (ei_decode_atom(input.buff, &index, decoded_atom) < 0) { std::cout << "failed decoding atom \n"; return 1; } } return 0; }
/* * The format letters are: * a - An atom * c - A character * s - A string * i - An integer * l - A long integer * u - An unsigned long integer * f - A float * d - A double float * p - An Erlang PID */ static int pformat(const char** fmt, union arg** args, ei_x_buff* x) { int res = 0; ++(*fmt); /* skip tilde */ switch (*(*fmt)++) { case 'a': res = ei_x_encode_atom(x, (*args)->s); (*args)++; break; case 'c': res = ei_x_encode_char(x, (*args)->c); (*args)++; break; case 's': res = ei_x_encode_string(x, (*args)->s); (*args)++; break; case 'i': res = ei_x_encode_long(x, (*args)->l); (*args)++; break; case 'l': res = ei_x_encode_long(x, (*args)->l); (*args)++; break; case 'u': res = ei_x_encode_ulong(x, (*args)->u); (*args)++; break; case 'f': /* float is expanded to double (C calling conventions) */ case 'd': res = ei_x_encode_double(x, (*args)->d); (*args)++; break; case 'p': res = ei_x_encode_pid(x, (*args)->pid); (*args)++; break; default: res = -1; break; } return res; }