[erlang-questions] about Erlang C Node monitor

Matt Stancliff sysop@REDACTED
Mon Dec 14 09:48:33 CET 2009


Yu-Teh,

On Dec 13, 2009, at 11:36 PM, Yu-Teh Shen wrote:
> But what I should do if I want to *monitor* the C node like all  
> other Erlang
> node and make erlang *connects*, *net_adm:ping* works?

You can use erlang:monitor_node to monitor C nodes.

Handling net_adm:ping in a C node takes a few steps.  Here is what I  
use:

main processing loop:
=====
   ETERM *gen_call         = erl_format("~a", "$gen_call");

   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) {
         msg = erl_element(1, emsg.msg);
         if (erl_match(_something_i_want_, msg)) {
           /* blah */
         }
         else if (erl_match(gen_call, msg)) {
           process_gen_call(fd, emsg.from, emsg.msg);
         }
       }
     }
     erl_free_term(msg);
     erl_free_term(emsg.msg);
     erl_free_term(emsg.to);
     erl_free_term(emsg.from);
   }
   erl_free_term(gen_call);
=====

Then, process_gen_call is:
=====
/* gen_call with is_auth is for handling net_adm:ping responses.
    The incoming message looks like:
    {'$gen_call', {<bob3@REDACTED>, #Ref<2394.0.0>},
      {is_auth, bob3@REDACTED}}
    You must respond with the reference (element 2 of tuple 2) and "yes"
*/
static void process_gen_call(int fd, ETERM *from, ETERM *msg) {
   ETERM *is_auth  = erl_format("~a", "is_auth");
   ETERM *args = erl_element(3, msg);
   ETERM *arg1 = erl_element(1, args);
   if (erl_match(is_auth, arg1)) {
     ETERM *fromp = erl_element(2, msg);
     ETERM *resp = erl_format("{~w, yes}", erl_element(2, fromp));
     erl_send(fd, from, resp);
     erl_free_compound(resp);
     erl_free_term(fromp);
   }
   erl_free_term(args);
   erl_free_term(arg1);
   erl_free_term(is_auth);
}
=====


Hope that helps,

-Matt
-- 
Matt Stancliff                    San Jose, CA
AIM: seijimr              iPhone: 678-591-9337
"The best way to predict the future is to invent it." --Alan Kay



More information about the erlang-questions mailing list