Erlang C节点相关问题

时间:2009-12-23 20:04:58

标签: erlang

在以下提供的教程中: http://www.erlang.org/doc/tutorial/cnode.html

有以下示例:


/* cnode_s.c */
#include 
#include 
#include 
#include 
#include "erl_interface.h"
#include "ei.h"
#define BUFSIZE 1000
int main(int argc, char **argv) {
  int port;                                /* Listen port number */
  int listen;                              /* Listen socket */
  int fd;                                  /* fd to Erlang node */
  ErlConnect conn;                         /* Connection data */
  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;
  port = atoi(argv[1]);
  erl_init(NULL, 0);
  if (erl_connect_init(1, "secretcookie", 0) == -1)
    erl_err_quit("erl_connect_init");
  /* Make a listen socket */
  if ((listen = my_listen(port)) 

I suspect that erl_receive_msg is a blocking call, and I don't know how to overcome this. In C network programming there is the "select" statement but in the Erlang EI API I don't know whether there is such a statement.

Basically I want to build a C node, that continuously sends messages to Erlang nodes. For simplicity suppose there is only one Erlang node.

The Erlang node has to process the messages it receives from the C node. The Erlang node is not supposed to ensure that it has received the message, not does it have to reply with the result of processing. Therefore once the message is sent I don't care about it faith.

One might think that one could modify the code as:

...
if (emsg.type == ERL_REG_SEND) {
    ...
    while(1) { 
        //generate tuple
        erl_send(fd, fromp, tuple);
        //free alloc resources
    }
    ...
}

这将产生一个无限循环,我们在其中生成和使用(发送)消息。 但是有一个重要问题:如果我这样做,那么C节点可能会向Erlang节点发送太多消息(因此应该有一种方法将消息从Erlang节点发送到C节点以减慢速度),或者Erlang节点可能认为C节点已关闭。

我知道这些问题必须简短(这很长很难看),但总结一下:

人们可能会使用什么机制(过程调用,算法)来为Erlang中的懒惰消费者在C中开发一个渴望的生成器,这样双方都知道底层的上下文?

3 个答案:

答案 0 :(得分:2)

我自己使用端口驱动程序来处理你正在描述的情况(没有触及C节点,因为我宁愿有更多的解耦)。

查看Erlang的端口驱动程序库:EPAPI。有一个利用此库的项目:Erland DBus

答案 1 :(得分:1)

您检查了ei_receive_msg_tmo功能吗?我认为它的工作方式类似于Erlang的receive after构造,因此如果将timeout设置为0,它将是非阻塞的。

我相信不推荐使用erl_interface,而应该使用ei。这可能是一个完整的错误信息......

答案 2 :(得分:1)

您需要仔细查看您发布的教程链接。 (搜索“最后我们有C节点客户端的代码。”)您将看到作者提供了客户端cnode实现。它看起来很合理。