您的位置:首页 > 其它

第四章 通知链

2019-12-26 11:00 791 查看

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

通知链是一份函数列表,当给定事件发生时予以执行。列表中的每个函数都让另一个子系统知道,调用此函数的子系统内所发生的一个事件或子系统所侦测到个一个事件。

通知链使用发布-订阅(publish-and-subcribe)模型:

  • 被通知者 ——接收某事件的子系统,提供回调函数予以调用
  • 通知者 ——感受到一个事件并调用回呼函数的子系统 通知

通知链列表元素的类型是notifier_block,定义:

/*
* notifier_call 要执行的函数
* next 链接列表的元素
* priority 优先级
*/
struct notifier_block{
int (*notifier_call)(struct notifier_block *self, unsigned long, void *);
struct notifier_block *next;
int priority;
}

网络子系统通知链:

  • inetaddr_chain ——发送有关本地接口上的IPv4地址的插入、删除以及变更的通知信息
  • inet6addr_chain —— 发送有关本地接口上的IPv6地址的插入、删除以及变更的通知信息
  • netdev_chain —— 发送有关网络设备注册状态的通知信息
/*struct nofitier_block **list 为通知链*/
/*注册*/
int notifier_chain_register(struct nofitier_block **list, struct notifier_block *n);
/*除名*/
int notifier_chain_unregister(struct nofitier_block **list, struct notifier_block *n);

 回调函数:

  注册 除名
inetaddr_chain register_inetaddr_notifier unregister_inetaddr_notifier
inet6addr_chain register_inet6addr_notifier unregister_inet6addr_notifier
netdev_chain register_netdevice_notifier unregister_netdevice_notifier

通知:

/*
* n 通知链
* val事件类型
* v 输入参数
* 返回值: NOTIFY_OK-通知信息被正确处理
NOTIFY_DONE - 对通知信息不敢兴趣
NOTIFY_BAD - 有些事情出错,停止调用此事件的回调函数
NOTIFY_STOP - 函数被正确调用,此事件不需要进一步调用其他回调函数
NOTIFY_STOP_MASK - 由notifier_call_chain检查,以了解是否停止调用回调函数,或者继续调用下去
*/
int notifier_call_chain(struct notifier_block **n, unsigned long val, void *v)
{
int ret = NOTIFY_DONE;
struct notifier_block *nb = *n;
while(nb)
{
ret = nb->notifier_call(list,val,v);
if(ret&NOTIFY_STOP_MASK)
{
return ret;
}
nb = nb->next;
}
return ret;
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iNet