首页 > 代码库 > Sipdroid实现SIP(四): 传输层和应用层之间的枢纽SipProvider
Sipdroid实现SIP(四): 传输层和应用层之间的枢纽SipProvider
I. 概述
在整套Sipdroid源码中, 类SipProvider是最靠近TCP/UDP的一层, 在Sipdroid实现的SIP协议栈中扮演底层枢纽的角色(前一篇文章中介绍的SipdroidEngine扮演的是上层枢纽), 这一角色也可以从该类是Configurable, TransportListener, TcpServerListener的继承体现出来.
向下层, SipProvider和通信传输层进行交互, 包括接收Transport的回调, 向Transport传递要发送的msg
向上层, SipProvider和UI交互, 包括根据msg触发指定Transaction回调, 从而通知Call再到UserAgent
II. 主要变量
2.1 监听标识之一: ANY
如果接收到的msg, 没有与其匹配的method_id, transaction_id或dialog_id, 就用ANY标识
public static final Identifier ANY = new Identifier("ANY");
2.2 监听标识之二: PROMISQUE
public static final Identifier PROMISQUE = new Identifier("PROMISQUE");
2.3 哈希表存放的接口的对象
在SipProviderListener中, 简单的接口定义和回调方法声明, 接口的对象listeners将会在SipProvider类中发挥重要的作用.
public void onReceivedMessage(SipProvider sip_provider, Message message);
listeners对象很关键, SipProvider提供的上下层枢纽功能都是要经过对应listener的被动触发和任务派发, Indentifier的值会在下面介绍.
Hashtable<Identifier, SipProviderListener> listeners = null;
2.4 通信协议相关
/*TCP/UDP相关*/ String[] transport_protocols = null; boolean transport_udp = false; boolean transport_tcp = false; static UdpTransport udp = null; TcpServer tcp_server = null; /*Socket相关*/ SocketAddress outbound_proxy = null; private String outbound_addr = null; private int outbound_port = -1;
III. 主要方法
3.1 初始化SipProvider
从宏观上把握下SipProvider类, 只需要明确以下问题:
- 初始化SipProvider的位置:
- 初始化SipProvider需要的参数:
- 初始化SipProvider后程序发生的变化:
3.2 应用启动时SipProvider监听者列表的变化过程(注册成功):
我删掉了和SipProviderListener无关的代码, 这样就能够显著看到Listener HashTable的变化情况: 在注册前逐个增加listeners, 当注册成功后, 和Register有关的listeners都被删除.
I/System.out: SipProvider: adding SipProviderListener: 412269425175@10.15.3.73-1-REGISTER-null-z9hG4bK26975 I/System.out: SipProvider: SipProviderListener: size=1, listeners=412269425175@10.15.3.73-1-REGISTER-null-z9hG4bK26975, I/System.out: SipProvider: adding SipProviderListener: INVITE I/System.out: SipProvider: SipProviderListener: size=2, listeners=INVITE, 412269425175@10.15.3.73-1-REGISTER-null-z9hG4bK26975, I/System.out: SipProvider: adding SipProviderListener: OPTIONS I/System.out: SipProvider: SipProviderListener: size=3, listeners=INVITE, OPTIONS, 412269425175@10.15.3.73-1-REGISTER-null-z9hG4bK26975, I/System.out: SipProvider: adding SipProviderListener: 412269425175@10.15.3.73-2-REGISTER-null-z9hG4bK84870 I/System.out: SipProvider: SipProviderListener: size=4, listeners=INVITE, OPTIONS, 412269425175@10.15.3.73-1-REGISTER-null-z9hG4bK26975, 412269425175@10.15.3.73-2-REGISTER-null-z9hG4bK84870,
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- I/System.out: SipProvider: removing SipProviderListener: NOTIFY I/System.out: SipProvider: WARNING: trying to remove a missed SipProviderListener. I/System.out: SipProvider: SipProviderListener: size=4, listeners=INVITE, OPTIONS, 412269425175@10.15.3.73-1-REGISTER-null-z9hG4bK26975, 412269425175@10.15.3.73-2-REGISTER-null-z9hG4bK84870, I/System.out: SipProvider: adding SipProviderListener: NOTIFY I/System.out: SipProvider: SipProviderListener: size=5, listeners=INVITE, OPTIONS, NOTIFY, 412269425175@10.15.3.73-1-REGISTER-null-z9hG4bK26975, 412269425175@10.15.3.73-2-REGISTER-null-z9hG4bK84870, I/System.out: SipProvider: removing SipProviderListener: 412269425175@10.15.3.73-1-REGISTER-null-z9hG4bK26975 I/System.out: SipProvider: SipProviderListener: size=4, listeners=INVITE, OPTIONS, NOTIFY, 412269425175@10.15.3.73-2-REGISTER-null-z9hG4bK84870, I/System.out: SipProvider: removing SipProviderListener: 412269425175@10.15.3.73-2-REGISTER-null-z9hG4bK84870 I/System.out: SipProvider: SipProviderListener: size=3, listeners=INVITE, OPTIONS, NOTIFY,
3.3 呼叫请求时监听者列表的变化过程(呼叫失败)
I/System.out: SipProvider: removing SipProviderListener: INVITE I/System.out: SipProvider: SipProviderListener: size=2, listeners=OPTIONS, NOTIFY, I/System.out: SipProvider: adding SipProviderListener: 847642009035@10.15.3.73-z9hG4bK06452429-null I/System.out: SipProvider: SipProviderListener: size=3, listeners=OPTIONS, NOTIFY, 847642009035@10.15.3.73-z9hG4bK06452429-null, I/System.out: SipProvider: adding SipProviderListener: 847642009035@10.15.3.73-1-INVITE-null-z9hG4bK26053 I/System.out: SipProvider: SipProviderListener: size=4, listeners=OPTIONS, NOTIFY, 847642009035@10.15.3.73-z9hG4bK06452429-null, 847642009035@10.15.3.73-1-INVITE-null-z9hG4bK26053, I/System.out: SipProvider: adding SipProviderListener: 847642009035@10.15.3.73-2-INVITE-null-z9hG4bK50097 I/System.out: SipProvider: SipProviderListener: size=5, listeners=OPTIONS, NOTIFY, 847642009035@10.15.3.73-z9hG4bK06452429-null, 847642009035@10.15.3.73-1-INVITE-null-z9hG4bK26053, 847642009035@10.15.3.73-2-INVITE-null-z9hG4bK50097,
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- I/System.out: SipProvider: removing SipProviderListener: 847642009035@10.15.3.73-z9hG4bK06452429-null I/System.out: SipProvider: SipProviderListener: size=4, listeners=OPTIONS, NOTIFY, 847642009035@10.15.3.73-1-INVITE-null-z9hG4bK26053, 847642009035@10.15.3.73-2-INVITE-null-z9hG4bK50097, I/System.out: SipProvider: adding SipProviderListener: INVITE I/System.out: SipProvider: SipProviderListener: size=5, listeners=INVITE, OPTIONS, NOTIFY, 847642009035@10.15.3.73-1-INVITE-null-z9hG4bK26053, 847642009035@10.15.3.73-2-INVITE-null-z9hG4bK50097, I/System.out: SipProvider: adding SipProviderListener: OPTIONS I/System.out: SipProvider: WARNING: trying to add a SipProviderListener with a id that is already in use. I/System.out: SipProvider: SipProviderListener: size=5, listeners=INVITE, OPTIONS, NOTIFY, 847642009035@10.15.3.73-1-INVITE-null-z9hG4bK26053, 847642009035@10.15.3.73-2-INVITE-null-z9hG4bK50097, I/System.out: SipProvider: removing SipProviderListener: 847642009035@10.15.3.73-1-INVITE-null-z9hG4bK26053 I/System.out: SipProvider: SipProviderListener: size=4, listeners=INVITE, OPTIONS, NOTIFY, 847642009035@10.15.3.73-2-INVITE-null-z9hG4bK50097, I/System.out: SipProvider: removing SipProviderListener: 847642009035@10.15.3.73-2-INVITE-null-z9hG4bK50097 I/System.out: SipProvider: SipProviderListener: size=3, listeners=INVITE, OPTIONS, NOTIFY,
3.4 从"哈希监听者列表"删除一个SipProvider监听
Sipdroid实现SIP(四): 传输层和应用层之间的枢纽SipProvider