---
title: "epmd源码学习"
author: "coderplay"
date: "2007-07-26T10:14:22Z"
tags: []
slug: "epmd"
draft: false
lastmod: "2007-07-26T10:14:22Z"
id: 105692
url: "https://coderplay.iteye.com/blog/105692"
---

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

各结点组成一个链表

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 代码

 

01. for (node = g-&gt;nodes.unreg; node; prev = node, node = node-&gt;next)
02. if (strcmp(node-&gt;symname, name) == 0)
03. {
04. dbg\_tty\_printf(g,1,"reusing slot with same name '%s'", node-&gt;symname);
05. if (prev == NULL)   /* 链表第一个结点就匹配 \*/
06. {
07. if (node-&gt;next == NULL) /* 链表只有一个元素 \*/
08. g-&gt;nodes.unreg = g-&gt;nodes.unreg\_tail = NULL;
09. else
10. g-&gt;nodes.unreg = node-&gt;next;
11. }
12. else
13. {
14. if (node-&gt;next == NULL) /*  链表的最后元素匹配 \*/
15. {
16. g-&gt;nodes.unreg\_tail = prev; /* Point to new last \*/
17. prev-&gt;next = NULL; /* New last has no next \*/
18. }
19. else
20. prev-&gt;next = node-&gt;next; /* 从链表去除 \*/
21. }
22. g-&gt;nodes.unreg\_count--; /* 注销结点个数减一 \*/
23. /* 当复用时,我们改变结点的creation值1..3 \*/
24. node-&gt;creation = node-&gt;creation % 3 + 1;
25. break;
26. }

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

cpp 代码

 

01. if (node == NULL)
02. {
03. /* A new name. If the "unreg" list is too long we steal the
04. oldest node structure and use it for the new node, else
05. we allocate a new node structure \*/
06. if ((g-&gt;nodes.unreg\_count &gt; MAX\_UNREG\_COUNT) ||
07. (g-&gt;debug && (g-&gt;nodes.unreg\_count &gt; DEBUG\_MAX\_UNREG\_COUNT)))
08. {
09. /* MAX\_UNREG\_COUNT &gt; 1 so no need to check unreg\_tail \*/
10. node = g-&gt;nodes.unreg;  /* Take first == oldest \*/
11. g-&gt;nodes.unreg = node-&gt;next; /* Link out \*/
12. g-&gt;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-&gt;creation = (current\_time(g) % 3) + 1; /* "random" 1-3 \*/
22. }
23. }