[轉]Introduction to JAIN SIP API part2
[本文轉自: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 the
Request
interface in appendix.
Lastly, you send the message using the
SipProvider
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 of
Dialog
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 a
Transaction
and then get the Dialog
from it. You keep the
Dialog
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 the
Dialog
object from before to create a new request. You can then massage the request and, lastly, use the
Transaction
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()
and
processTransactionTerminated()
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 the
Dialog
and Transaction
instances). You'll leave these two methods empty as you don't need them in TextClient.
相關文章
- Visitor Pattern Introduction (轉)
- [SIP00]SIP 概念總結
- VoIP Protocols > SIPProtocol
- [SIP01]SIP Header Fields裡面各欄位用途Header
- iOS App間相互跳轉漫談 part2iOSAPP
- sip是什麼?Mac電腦如何關閉sip?關閉系統完整性保護SIP的方法教程Mac
- SIP系統怎麼禁用?Mac上 sip系統完整性關閉方法,怎麼看sip是否關閉Mac
- Introduction to AlgorithmGo
- Introduction to GitGit
- RL Introduction
- SIP (Session Initiation Protocol) 協議SessionProtocol協議
- sip與openser的關係
- TLS 1.3 IntroductionTLS
- nodejs introductionNodeJS
- Chapter 1:IntroductionAPT
- Introduction to Partitioning
- No Sql Db IntroductionSQL
- Hadoop IntroductionHadoop
- Introduction to Databases and MySQLDatabaseMySql
- Oracle RAC introductionOracle
- HTML 01 - IntroductionHTML
- Introduction to Vetors
- VoIP通話之sip協議協議
- 關於SIP的原始碼地址原始碼
- 《SIP揭祕》讀書筆記筆記
- Introduction of DataGuard protection mode
- A re-introduction to JavaScriptJavaScript
- [zz]android introductionAndroid
- Introduction to Index Segments (24)Index
- FFmpeg Filtering IntroductionFilter
- SIP穿越NAT的rport機制
- 第四章 SIP協議協議
- Linux 音訊ALSA技術說明--part2[轉&學習]Linux音訊
- C與C++中的異常處理2(part2) (轉)C++
- 核心編譯part2編譯
- 蘋果M1晶片的Mac怎麼關閉SIP?M1 mac關閉Sip方法教程!蘋果晶片Mac
- 1 Introduction to the Multitenant ArchitectureNaN
- Machine Learning-IntroductionMac