Implementing a SIP Back-To-Back User Agent using javax.servlet.sip.B2buaHelper
|This blog explains how to implement a Back-To-Back User Agent (i.e., B2BUA) in SailFin using javax.servlet.sip.B2buaHelper, where B2BUA is essentially a SipServlet.
Let us take the following Back-To-Back UA scenario which is typically used in the call setup. Keep this scenario in mind (or refer to it) as you go through the code in the rest of this blog.
Let us assume the following sip addresses for each SIP entities :
The initial INVITE request will be sent from web/sip/ejb component depending on the type of the application. The code below shows how to create such a request and send it out to the server. I assume that the sipFactory is injected using @Resource SipFactory sipFactory;
When the server responds with 200 OK with its SDP (lets call it "sdp1") for the above INVITE request, then we should create the second INVITE request and send it to the client. The second INVITE request should contain the "sdp1" of the server.
This is done in the doSuccessResponse method of B2BUA like this:
Now when the client responds with 200 OK with its SDP (lets call it "sdp2"), then we should send ACK to both the parties. The ACK to the server should contain the "sdp2" of the client.
This is done again in the doSuccessResponse method of the B2BUA like this:
With this, we are done with successfully setting up the "Media session" between the client and the server.
Once the media session/conversation is finished, any one party will "hang up" the phone. This will result in a BYE request being sent to the SailFin server. Lets assume that the client hangs up and hence sends the BYE request. Now it is the responsibility of the B2BUA to take further action. As a first step the B2BUA should create the second BYE request and send it to the server, as shown in the code below:
This is done again in the doSuccessResponse method of B2BUA like this:
Once the media session is terminated, we should invalidate the SipApplicationSession (which also invalidates all the protocol sessions associated with it).
The complete source code of the B2BUA.java explained in this blog is available as part of the OnlineBank sample which is installed under samples/sipservlet/OnlineBank directory of your SailFin installation directory. The SailFin is available for download here.