[转]Introduction to JAIN SIP API part2
来源:互联网 发布:怎么更改mac管理员名称 编辑:程序博客网 时间:2024/05/17 00:50
[本文转自:http://www.oracle.com/technology/pub/articles/dev2arch/2007/10/introduction-jain-sip.html]
Sending a SIP Request
Let's now write a method to send a SIP message with the JAIN SIP API.
In the prerequisites I suggested that you must know SIP fairly well before you can start using the SIP API. You'll now see what I mean by that! The SIP API is quite a low-level abstraction and, in most cases, doesn't use default values or hide headers, request URIs, or contents of SIP messages. The advantage of this design is that you have complete control over what SIP messages contain.
The following method is a bit lengthy. It prepares and sends a SIP request. It can roughly be split into four subsections:
- Create main elements
- Create message
- Complete message
- Send message
The following main SIP elements are minimally needed to construct a message using the JAIN SIP API:
- Request URI
- Method
- Call-ID header
- CSeq header
- From header
- An array of Via headers
- Max-forwards header
For information about these elements, please see An Introduction to SIP, Part 1 (Dev2Dev, 2006). The following code snippet creates all of these elements:
public void sendMessage(String to, String message) throws ParseException, InvalidArgumentException, SipException {SipURI from = addressFactory.createSipURI(getUsername(), getHost() + ":" + getPort()); Address fromNameAddress = addressFactory.createAddress(from);fromNameAddress.setDisplayName(getUsername());FromHeader fromHeader =headerFactory.createFromHeader(fromNameAddress, "textclientv1.0");String username = to.substring(to.indexOf(":")+1, to.indexOf("@"));String address = to.substring(to.indexOf("@")+1);SipURI toAddress =addressFactory.createSipURI(username, address);Address toNameAddress = addressFactory.createAddress(toAddress);toNameAddress.setDisplayName(username);ToHeader toHeader =headerFactory.createToHeader(toNameAddress, null);SipURI requestURI =addressFactory.createSipURI(username, address);requestURI.setTransportParam("udp");ArrayList viaHeaders = new ArrayList();ViaHeader viaHeader =headerFactory.createViaHeader(getHost(),getPort(),"udp",null);viaHeaders.add(viaHeader);CallIdHeader callIdHeader = sipProvider.getNewCallId();CSeqHeader cSeqHeader =headerFactory.createCSeqHeader(1, Request.MESSAGE);MaxForwardsHeader maxForwards =headerFactory.createMaxForwardsHeader(70);...
I'm using factories that were created in the constructor,HeaderFactory
and AddressFactory
, to instantiate these elements.
Next let's instantiate the actual SIP message itself, passing in all the elements created earlier:
Request request = messageFactory.createRequest( requestURI, Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);...
Note the use of MessageFactory
for this step.
Then let's add other elements to the message: a contact header and the contents of the message (payload). It's possible to add custom headers too at this point.
SipURI contactURI = addressFactory.createSipURI(getUsername(), getHost());contactURI.setPort(getPort());Address contactAddress = addressFactory.createAddress(contactURI);contactAddress.setDisplayName(getUsername());ContactHeader contactHeader =headerFactory.createContactHeader(contactAddress);request.addHeader(contactHeader);ContentTypeHeader contentTypeHeader =headerFactory.createContentTypeHeader("text", "plain");request.setContent(message, contentTypeHeader);...
For more information on how to further massage the request, there's a description of theRequest
interface in appendix.
Lastly, you send the message using theSipProvider
instance:
sipProvider.sendRequest(request);}
Sending Messages Inside a Dialog
You're sending our message outside a dialog. That means messages are not related to each other. This works well for a simple instant-messaging application like the TextClient.
An alternative would be to create a dialog (sometimes called a session) using an INVITE message, and then send messages inside this dialog. The TextClient doesn't use this technique. However, I think it's something worth learning. So as a compromise, this subsection describes how it's done.
Sending a message inside a dialog requires the creation ofDialog
and Transaction
objects. On the initial message (that is, the message that creates the dialog), instead of using the provider to send out the message, you instantiate aTransaction
and then get the Dialog
from it. You keep theDialog
reference for later. You then use the Transaction
to send the message:
ClientTransaction trans = sipProvider.getNewClientTransaction(invite);dialog = trans.getDialog();trans.sendRequest();
Later when you wish to send a new message inside the same dialog, you use theDialog
object from before to create a new request. You can then massage the request and, lastly, use theTransaction
to send out the message.
request = dialog.createRequest(Request.MESSAGE);request.setHeader(contactHeader);request.setContent(message, contentTypeHeader);ClientTransaction trans = sipProvider.getNewClientTransaction(request);trans.sendRequest();
Essentially, you're skipping the "create main elements" step when sending a message inside an existing dialog. When you use an INVITE to create a dialog, don't forget to clean it up by sending an in-dialog BYE message at the end. This technique is also used to refresh registrations and subscriptions.
Previously, you've seen the SipListener
interface, which contains the processDialogTerminated()
andprocessTransactionTerminated()
methods. These are called automatically at the end of a dialog and transaction, respectively. Typically, you implement these methods to clean things up (for example, discard theDialog
and Transaction
instances). You'll leave these two methods empty as you don't need them in TextClient.
- [转]Introduction to JAIN SIP API part2
- [转]Introduction to JAIN SIP API part1
- [转]Introduction to JAIN SIP API part3_FINAL
- An Introduction to the JAIN SIP API
- Jain-sip
- JAIN SIP
- Introduction to RAID,part2
- JAIN SIP instantmessaging学习
- jain-sip 开发文档
- Jain-sip-applet-phone
- JAIN SIP vs SIP Servlet
- An basic introduction to SIP
- JAVA-SIP协议栈 -- JAIN-SIP
- API Guides -> Introduction -> Introduction to Android
- Introduction to the Sockets API
- Introduction to Win32 API 翻译
- 用于呼叫控制的JAIN SIP
- useful URLs of JAIN SIP & Mobicents.
- [转]Introduction to JAIN SIP API part1
- ionic添加cordova插件-SQLite
- HBase put流程主要细节
- 自定义控件之onLayout()
- Spring的自定装配与自动检测
- [转]Introduction to JAIN SIP API part2
- HTML5革命即将爆发
- 认识 ViewPager position
- [转]Introduction to JAIN SIP API part3_FINAL
- vs2015 MFC中动态显示图片
- [IDE]Netbeans 整合 Weblogic10
- HttpClient代理服务器配置
- Composer构建现代PHP帝国(二)——编写自己的Composer包
- dom4j生成xml中文问题