目录
一. 概述
二. 主要变量
三. 主要方法
四. 在Sipdroid中的应用
一. 概述
在整套Sipdroid源码中, 类SipProvider是最靠近TCP/UDP的一层, 在Sipdroid实现的SIP协议栈中扮演底层枢纽的角色(前一篇文章中介绍的SipdroidEngine扮演的是上层枢纽), 这一角色也可以从该类是Configurable, TransportListener, TcpServerListener的继承体现出来.
向下层, SipProvider和通信传输层进行交互, 包括接收Transport的回调, 向Transport传递要发送的msg
向上层, SipProvider和UI交互, 包括根据msg触发指定Transaction回调, 从而通知Call再到UserAgent
二. 主要变量
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;
三. 主要方法
3.1 初始化SipProvider
首先, 从宏观上了解SipProvider类, 需要明确以下三个问题:
1. SipProvider的生命周期伴随Sipdroid核心引擎的全过程
SipdroidEngine.StartEngine(): 在Sipdroid应用"引擎"初始化的时候初始化
SipdroidEngine.halt(): 在Sipdroid应用"引擎"终止的时候终止
2. 初始化SipProvider要完成哪些事情
SipProvider具有连接传输层和UA,RA的功能, 所以需要初始化和传输, 监听相关的变量, 比如传输协议, IP和端口, 服务器地址, 监听对象的哈希表
public SipProvider(String via_addr, int port, String[] protocols, String ifaddr) {
init(via_addr, port, protocols, ifaddr);
initlog();
startTrasport();
}
3. 初始化SipProvider后程序发生的变化
根据全局变量LINES的值, 确定了每个应用中初始化的sip_providers对象数组的大小.
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中的应用
就像每个产品会配一个产品经理一样, 每个UA和RA都会配一个sip_provider, 负责这个UA或RA的消息监听, 接收和处理.