ITeye博客

epmd源码学习

coderplay · 2007-07-26 10:14 · erlang · Markdown

注: 此处节点是指分布式中分布在各终端的点, 而结点是指存在数据结构中的元素.

各结点组成一个链表

c 代码

 

  1. typedef struct {
  2. Node *reg;            /*已注册的结点链表*/
  3. Node *unreg;          /*注销的结点链表*/
  4. Node *unreg_tail;     /*注销链表尾*/
  5. int unreg_count;      /*注销结点个数*/
  6. } Nodes;

节点注册过程

1. 先看看注销链表中有没有些结点已经用过, 如果有则复用此结点,但结点的creation要改变, 范围从1到3.

cpp 代码

 

  1. for (node = g->nodes.unreg; node; prev = node, node = node->next)
  2. if (strcmp(node->symname, name) == 0)
  3. {
  4. dbg_tty_printf(g,1,"reusing slot with same name '%s'", node->symname);
  5. if (prev == NULL)   /* 链表第一个结点就匹配 */
  6. {
  7. if (node->next == NULL) /* 链表只有一个元素 */
  8. g->nodes.unreg = g->nodes.unreg_tail = NULL;
  9. else
  10. g->nodes.unreg = node->next;
  11. }
  12. else
  13. {
  14. if (node->next == NULL) /*  链表的最后元素匹配 */
  15. {
  16. g->nodes.unreg_tail = prev; /* Point to new last */
  17. prev->next = NULL; /* New last has no next */
  18. }
  19. else
  20. prev->next = node->next; /* 从链表去除 */
  21. }
  22. g->nodes.unreg_count--; /* 注销结点个数减一 */
  23. /* 当复用时,我们改变结点的creation值1..3 */
  24. node->creation = node->creation % 3 + 1;
  25. break;
  26. }

2. 如果没有,看看注销结点链表是否过长.
    如果过长则使用最老的那个注销结点
    否则新建一个结点

cpp 代码

 

  1. if (node == NULL)
  2. {
  3. /* A new name. If the "unreg" list is too long we steal the
  4. oldest node structure and use it for the new node, else
  5. we allocate a new node structure */
  6. if ((g->nodes.unreg_count > MAX_UNREG_COUNT) ||
  7. (g->debug && (g->nodes.unreg_count > DEBUG_MAX_UNREG_COUNT)))
  8. {
  9. /* MAX_UNREG_COUNT > 1 so no need to check unreg_tail */
  10. node = g->nodes.unreg;  /* Take first == oldest */
  11. g->nodes.unreg = node->next; /* Link out */
  12. g->nodes.unreg_count--;
  13. }
  14. else
  15. {
  16. if ((node = (Node *)malloc(sizeof(Node))) == NULL)
  17. {
  18. dbg_printf(g,0,"empd: Insufficient memory");
  19. exit(1);
  20. }
  21. node->creation = (current_time(g) % 3) + 1; /* "random" 1-3 */
  22. }
  23. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。