Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
eddie murphy committed Dec 28, 2024
1 parent bb16768 commit 0c7ac3f
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 32 deletions.
6 changes: 5 additions & 1 deletion content/kamailio/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ toc: true
# 文章列表

- 1. 文档
- [第一节 如何熟悉Kamailio文档的文档结构](/kamailio/docs-index)
- [熟悉Kamailio文档的文档结构](/kamailio/docs-index)
- [路由执行顺序](./msg-flow/)
- [kamailio 启动参数控制](./kamailio-cli-params)
- [DMQ模块源码学习笔记](./dmq-note)
- [源码笔记 - 自定义事件路由(上)](./customer-event-route)
174 changes: 174 additions & 0 deletions content/kamailio/route-list-action/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
---
title: "源码笔记 - 自定义事件路由(中)"
date: "2024-12-28 09:43:00"
draft: false
type: posts
tags:
- all
categories:
- all
---



[[_TOC_]]



# route_list

route.h定义了几个函数分别用来获取、查找、新增route

```c
// src/core/route.h
int route_get(struct route_list *rt, char *name);
int route_lookup(struct route_list *rt, char *name);
void push(struct action *a, struct action **head);

struct route_list
{
struct action **rlist;
int idx; /* first empty entry */
int entries; /* total number of entries */
struct str_hash_table names; /* name to route index mappings */
};
```
## rlist
我们对route_list数据模型进行简化:
rlist是一个固定长度的一维数组,通过索引来访问对应的值。如果数组的空间不足,那么就创建一个两倍大的空数据,然后先把原始数据复制过去。这种复制方式保持的原始数据的索引位置。有点像golang的切片扩容机制。
这里最为重要的就是保持数组元素的索引位置在扩容后不变。
```c
static inline int route_new_list(struct route_list *rt)
{
int ret;
struct action **tmp;
ret = -1;
if(rt->idx >= rt->entries) {
// 两倍扩容
tmp = pkg_realloc(rt->rlist, 2 * rt->entries * sizeof(struct action *));
if(tmp == 0) {
LM_CRIT("out of memory\n");
goto end;
}
/* init the newly allocated memory chunk */
memset(&tmp[rt->entries], 0, rt->entries * sizeof(struct action *));
rt->rlist = tmp;
rt->entries *= 2;
}
if(rt->idx < rt->entries) {
ret = rt->idx;
rt->idx++;
}
end:
return ret;
}
```



## str_hash_table

我们对hash_table的数据模型进行简化,它其实就是一hash表,key是路由的名,值是一个正数,正数代表了路由执行单元的索引位置。



如果我们用js对route_list来表示

1. dispatcher:dst-down对应的是索引0
2. 索引0在rlist里对应的是func1
3. dispatcher:dst-down事件发生后,调用func1

```js
let rlist = [func1, func2, func3]
let names = {
'dispatcher:dst-down': 0,
'rtpengine:dtmf-event': 1
}
```

其实这里完全可以不用数组,如果在js里, 可以直接用函数作为hash的key

```js
let names = {
'dispatcher:dst-down': func1,
'rtpengine:dtmf-event': func2
}
```



## action

```c
// src/core/route_struct.h
struct action
{
int cline;
char *cfile;
char *rname;
enum action_type type; /* forward, drop, log, send ...*/
int count;
struct action *next;
action_u_t val[MAX_ACTIONS];
};
```



## str_hash_table

```c
struct str_hash_table
{
struct str_hash_head *table;
int size;
};

struct str_hash_head
{
struct str_hash_entry *next;
struct str_hash_entry *prev;
};

struct str_hash_entry
{
struct str_hash_entry *next;
struct str_hash_entry *prev;
str key;
unsigned int flags;
union
{
void *p;
char *s;
int n;
char data[sizeof(void *)];
} u;
};
```





在src/core/route.c中定义如下几个变量,分别管理着6钟不同的路由。

```c
// src/core/route.c
/* main routing script table */
struct route_list main_rt;
struct route_list onreply_rt;
struct route_list failure_rt;
struct route_list branch_rt;
struct route_list onsend_rt;
struct route_list event_rt;
```


51 changes: 20 additions & 31 deletions content/posts/2022/05/02-hugo-add-mermaid.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,47 @@ categories:
- hugo
---

# 增加mermaid shortcodes
# step1

在themes/YourTheme/layouts/shortcodes/mermaid.html 增加如下内容
在themes/YourTheme/layouts/partials/footer.html 的最后追加如下内容

```html

<script async type="application/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mermaid.min.js">
var config = {
startOnLoad:true,
theme:'{{ if .Get "theme" }}{{ .Get "theme" }}{{ else }}dark{{ end }}',
align:'{{ if .Get "align" }}{{ .Get "align" }}{{ else }}center{{ end }}'
};
mermaid.initialize(config);
{{if .Store.Get "hasMermaid" }}
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
mermaid.initialize({ startOnLoad: true });
</script>
<div class="mermaid">
{{.Inner}}
</div>
{{end }}
```

# step2:

在layouts/_default/_markup/render-codeblock-mermaid.html

```html
<pre class="mermaid">
{{- .Inner | htmlEscape | safeHTML }}
</pre>
{{ .Page.Store.Set "hasMermaid" true }}
```

# 在blog中增加如下代码

{{< notice warning >}}
注意下面的代码,你在实际写的时候,要把 /**/ 删除
{{< /notice >}}

```go
{{/*< mermaid align="left" theme="neutral" */>}}
```mermaid
pie
title French Words I Know
"Merde" : 50
"Oui" : 35
"Alors" : 10
"Non" : 5
{{/*< /mermaid >*/}}
```

{{< mermaid align="left" theme="neutral" >}}
pie
title French Words I Know
"Merde" : 50
"Oui" : 35
"Alors" : 10
"Non" : 5
{{< /mermaid >}}

{{< mermaid align="left" theme="neutral" >}}
```mermaid
sequenceDiagram
title French Words I Know
autonumber
Alice->>Bob: hello
Bob-->>Alice: hi
Alice->Bob: talking
{{< /mermaid >}}
```
4 changes: 4 additions & 0 deletions layouts/_default/_markup/render-codeblock-mermaid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<pre class="mermaid">
{{- .Inner | htmlEscape | safeHTML }}
</pre>
{{ .Page.Store.Set "hasMermaid" true }}
7 changes: 7 additions & 0 deletions themes/PaperMod/layouts/partials/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,10 @@
});
</script>
{{- end }}

{{if .Store.Get "hasMermaid" }}
<script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
mermaid.initialize({ startOnLoad: true });
</script>
{{end }}

0 comments on commit 0c7ac3f

Please sign in to comment.