基于JAINSIP协议栈的一个简单SIP服务器实现流程Word文档下载推荐.docx
- 文档编号:19362294
- 上传时间:2023-01-05
- 格式:DOCX
- 页数:25
- 大小:19.64KB
基于JAINSIP协议栈的一个简单SIP服务器实现流程Word文档下载推荐.docx
《基于JAINSIP协议栈的一个简单SIP服务器实现流程Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《基于JAINSIP协议栈的一个简单SIP服务器实现流程Word文档下载推荐.docx(25页珍藏版)》请在冰豆网上搜索。
CANCEL的处理流程:
向主叫响应CANCEL200OK。
根据客户端事务得到发送给主叫的INVITE请求中的URI,CALLID,FROM,TO,VIA头域来创建CANCEL请求。
注:
非200OK的ACK响应是由协议栈来实现的,不需要应用层来发送。
也就是说,ACK消息不会经过应用层。
附源代码:
packagecom.voip.sipphone;
importgov.nist.javax.sip.address.SipUri;
importgov.nist.javax.sip.header.CSeq;
importgov.nist.javax.sip.header.Contact;
importgov.nist.javax.sip.header.ContentLength;
importgov.nist.javax.sip.header.ContentType;
importgov.nist.javax.sip.header.From;
importgov.nist.javax.sip.header.Via;
importjava.text.ParseException;
importjava.util.ArrayList;
importjava.util.Hashtable;
importjava.util.List;
importjava.util.Properties;
importjava.util.Timer;
importjavax.sip.*;
importjavax.sip.address.Address;
importjavax.sip.address.AddressFactory;
importjavax.sip.address.URI;
importjavax.sip.header.CallIdHeader;
importjavax.sip.header.ContactHeader;
importjavax.sip.header.ExpiresHeader;
importjavax.sip.header.FromHeader;
importjavax.sip.header.Header;
importjavax.sip.header.HeaderFactory;
importjavax.sip.header.MaxForwardsHeader;
importjavax.sip.header.RecordRouteHeader;
importjavax.sip.header.ToHeader;
importjavax.sip.header.ViaHeader;
importjavax.sip.message.MessageFactory;
importjavax.sip.message.Request;
importjavax.sip.message.Response;
publicclassSipPhoneimplementsSipListener{
publicvoidprocessDialogTerminated(DialogTerminatedEventarg0){
//TODOAuto-generatedmethodstub
System.out.println("
processDialogTerminated"
+arg0.toString());
}
publicvoidprocessIOException(IOExceptionEventarg0){
processIOException"
/**
*保存当前注册的用户
*/
privatestaticHashtable<
URI,URI>
currUser=newHashtable();
*@authorsoftware
*注册定时器
classTimerTaskextendsTimer
{
*defaultconstructor
publicTimerTask()
*如果定时任务到,则删除该用户的注册信息
publicvoidrun()
*服务器侦听IP地址
privateStringipAddr="
192.168.0.20"
;
*服务器侦听端口
privateintport=5060;
*处理register请求
*@paramrequest请求消息
privatevoidprocessRegister(Requestrequest,RequestEventrequestEvent)
if(null==request)
processInviterequestisnull."
);
return;
//System.out.println("
Request"
+request.toString());
ServerTransactionserverTransactionId=requestEvent.getServerTransaction();
try
Responseresponse=null;
ToHeaderhead=(ToHeader)request.getHeader(ToHeader.NAME);
AddresstoAddress=head.getAddress();
URItoURI=toAddress.getURI();
ContactHeadercontactHeader=(ContactHeader)request.getHeader("
Contact"
AddresscontactAddr=contactHeader.getAddress();
URIcontactURI=contactAddr.getURI();
processRegisterfrom:
"
+toURI+"
requeststr:
+contactURI);
intexpires=request.getExpires().getExpires();
//如果expires不等于0,则为注册,否则为注销。
if(expires!
=0||contactHeader.getExpires()!
=0)
currUser.put(toURI,contactURI);
registeruser"
+toURI);
else
currUser.remove(toURI);
unregisteruser"
response=msgFactory.createResponse(200,request);
sendregisterresponse:
+response.toString());
if(serverTransactionId==null)
serverTransactionId=sipProvider.getNewServerTransaction(request);
serverTransactionId.sendResponse(response);
//serverTransactionId.terminate();
registerserverTransaction:
+serverTransactionId);
processRequestserverTransactionIdisnull."
catch(ParseExceptione)
//TODOAuto-generatedcatchblock
e.printStackTrace();
catch(SipExceptione)
catch(InvalidArgumentExceptione)
*处理invite请求
privatevoidprocessInvite(Requestrequest,RequestEventrequestEvent)
//发送100Trying
serverTransactionId=requestEvent.getServerTransaction();
if(serverTransactionId==null)
callerDialog=serverTransactionId.getDialog();
Responseresponse=msgFactory.createResponse(Response.TRYING,request);
//查询目标地址
URIreqUri=request.getRequestURI();
URIcontactURI=currUser.get(reqUri);
processInviterqStr="
+reqUri+"
contact="
//根据Requesturi来路由,后续的响应消息通过VIA来路由
RequestcliReq=msgFactory.createRequest(request.toString());
cliReq.setRequestURI(contactURI);
ViacallerVia=(Via)request.getHeader(Via.NAME);
Viavia=(Via)headerFactory.createViaHeader(ipAddr,port,"
UDP"
callerVia.getBranch()+"
sipphone"
//FIXME需要测试是否能够通过设置VIA头域来修改VIA头域值
cliReq.removeHeader(Via.NAME);
cliReq.addHeader(via);
//更新contact的地址
ContactHeadercontactHeader=headerFactory.createContactHeader();
Addressaddress=addressFactory.createAddress("
sip:
sipsoft@"
+ipAddr+"
:
"
+port);
contactHeader.setAddress(address);
contactHeader.setExpires(3600);
cliReq.setHeader(contactHeader);
clientTransactionId=sipProvider.getNewClientTransaction(cliReq);
clientTransactionId.sendRequest();
processInviteclientTransactionId="
+clientTransactionId.toString());
sendinvitetocallee:
+cliReq);
catch(TransactionUnavailableExceptione1)
e1.printStackTrace();
catch(Exceptione)
*处理SUBSCRIBE请求
privatevoidprocessSubscribe(Requestrequest)
processSubscriberequestisnull."
ServerTransactionserverTransactionId=null;
catch(TransactionAlreadyExistsExceptione1)
}catch(TransactionUnavailableExceptione1)
if(response!
=null)
ExpiresHeaderexpireHeader=headerFactory.createExpiresHeader(30);
response.setExpires(expireHeader);
response:
if(serverTransactionId!
serverTransactionId.terminate();
*处理BYE请求
privatevoidprocessBye(Requestrequest,RequestEventrequestEvent)
if(null==request||null==requestEvent)
processByerequestisnull."
RequestbyeReq=null;
Dialogdialog=requestEvent.getDialog();
calleeDialog:
+calleeDialog);
callerDialog:
+callerDialog);
if(dialog.equals(calleeDialog))
byeReq=callerDialog.createRequest(request.getMethod());
ClientTransactionclientTran=sipProvider.getNewClientTransaction(byeReq);
callerDialog.sendRequest(clientTran);
calleeDialog.setApplicationData(requestEvent.getServerTransaction());
elseif(dialog.equals(callerDialog))
byeReq=calleeDialog.createRequest(request.getMethod());
calleeDialog.sendRequest(clientTran);
callerDialog.setApplicationData(requestEvent.getServerTransaction());
sendbyetopeer:
+byeReq.toString());
*处理CANCEL请求
privatevoidprocessCancel(Requestrequest)
processCancelrequestisnull."
*处理INFO请求
privatevoidprocessInfo(Requestrequest)
processInforequestisnull."
*处理ACK请求
privatevoidprocessAck(Requestrequest,RequestEventrequestEvent)
processAckrequestisnull."
RequestackRequest=null;
CSeqcsReq=(CSeq)request.getHeader(CSeq.NAME);
ackRequest=calleeDialog.createAck(csReq.getSeqNumber());
calleeDialog.sendAck(ackRequest);
sendack
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 JAINSIP 协议 一个 简单 SIP 服务器 实现 流程