void erl_proc int fd unsigned char buf ErlMessage emsg int res -1 char

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
void erl_proc(int fd, unsigned char* buf, ErlMessage* emsg)
{
int res = -1;
char cmd[64];
strcpy(cmd, "unknown");
unsigned char msg_buf[255];
memset(msg_buf, 0, sizeof(msg_buf));
int len = 0;
// extracting args in case of cmd
int port = -1;
if (!emsg) {
app_trace(TRACE_INFO, "Erlang interface error: ErlMessage = NULL");
return;
}
// hi tiple /3
ETERM *fromp = NULL, *main_tuple = NULL;
// two level
ETERM *cmd_str = NULL, *arg_tuple = NULL;
// coords, args
ETERM *e_port = NULL, *e_arg1 = NULL, *e_arg2 = NULL, *e_arg3 = NULL,
*e_arg4 = NULL, *e_arg5 = NULL, *e_arg6 = NULL;
//{any, c1@tmip} ! {call, self(), {start_ring, {{slot, port}, {arg1, arg2, arg3}}}}.
// first
fromp = erl_element(2, emsg->msg);
main_tuple = erl_element(3, emsg->msg);
if (ERL_TUPLE_SIZE(main_tuple) != 3){
app_trace(TRACE_INFO, "Erlang intreface: bad tuple size!");
goto out;
}
// second
if (main_tuple) {
cmd_str = erl_element(1, main_tuple); // command name
port = ERL_INT_VALUE(erl_element(2, main_tuple)); // second data from tuple {term_id}
arg_tuple = erl_element(3, main_tuple); // third data from tuple {arg1...argN}
}
else{
app_trace(TRACE_INFO, "Erlang intreface: bad main tuple size!");
goto out;
}
if (cmd_str) strcpy(cmd, ERL_ATOM_PTR(cmd_str));
if (!arg_tuple) {
app_trace(TRACE_INFO, "Erlang interface error: bad arg tuple!");
goto out;
}
if (-1 == port) goto out;
if (strcmp(ERL_ATOM_PTR(cmd_str), CMDS_REGISTER) == 0){ // ---- REGISTER ----
app_trace(TRACE_INFO, "# megaco: === REGISTER ERLANG INTERFACE ===");
res = 0;
goto out;
}
else
if (strcmp(ERL_ATOM_PTR(cmd_str), CMDS_START_RING) == 0){// ---- START_RING -----
e_arg1 = erl_element(1, arg_tuple);
if (e_arg1) {
char* callerID = erl_iolist_to_string(e_arg1);
msg_buf[len++] = MEG_CMD_START_RING;
memcpy(&msg_buf[len], &port, sizeof(port));
len += sizeof(port);
msg_buf[len++] = (unsigned char) strlen(callerID);
memcpy(&msg_buf[len], callerID, strlen(callerID));
len += strlen(callerID);
app_snd_msg(MSG_MEGACO, len, msg_buf);
res = 0;
}
}
else { // ---- ERROR, odnako! ----
app_trace(TRACE_INFO, "# megaco: unknown command [%s]", (char*) ERL_ATOM_PTR(cmd_str));
}
out:
// answer
if (res == -1){ // error, bad command or bad param
erl_msg_reply_by_cmd_name(port, cmd, STATE_ERR, to_erlang_node);
}
// free memory
if (fromp) erl_free_term(fromp);
if (main_tuple) erl_free_term(main_tuple);
if (arg_tuple) erl_free_term(arg_tuple);
if (e_port) erl_free_term(e_port);
if (e_arg1) erl_free_term(e_arg1);
if (e_arg2) erl_free_term(e_arg2);
if (e_arg3) erl_free_term(e_arg3);
if (e_arg4) erl_free_term(e_arg4);
if (e_arg5) erl_free_term(e_arg5);
if (e_arg6) erl_free_term(e_arg6);
}