Use Sipsession or sipapplicationsession in Sipservlet to handle changes in UA status

Source: Internet
Author: User
Tags ack bool sessions

First of all, it is not related to this article, today finally with x-lite registration, it is necessary to set the outgoing proxy server for "127.0.0.1:5081", of course, this is the machine, if it is other computers, You need to set IP to server. 5081 is the port used by P-CSCF in SDS, if only using IP address, it is not possible!

If it is a b2bua, then you only need a global container hashmap<sipsession,sipsession> map, save All "survive" session, but now I am a proxy servlet and a B2bua Servlet, so that the storage state of the container needs to be able to be shared by two servlet classes. Therefore, the ServletContext objects are created in two servlets, respectively, and initialized in the INIT function respectively ctx= Config.getservletcontext (). as follows:

public void init (ServletConfig config) throws servletexception {super.init (config); ctx = Config.getservletcontext (); . }

And then in the first Servlet--proxyservlet,

public class Proxyservlet extends sipservlet{... ServletContext CTX = null; hashmap<sipsession,sipsession> map = new hashmap<sipsession,sipsession>; ... public void init (ServletConfig config) throws servletexception {super.init (config); ctx = Config.getservletcontext () ; Ctx.setattribute ("Statemap", map); ... } ... }

In a second servlet--b2buaservlet,

public class B2buaservlet extends sipservlet{... ServletContext CTX = null; hashmap<sipsession,sipsession> map = new hashmap<sipsession,sipsession>; ... public void init (ServletConfig config) throws servletexception {super.init (config); ctx = Config.getservletcontext () ; Map = (hashmap<sipsession,sipsession>) ctx.getattribute ("Statemap"); ... } ... }

This solves the problem of using the same map to save the session in two Servlets. But is it appropriate to use the Hashmap<sipsession,sipsession> data structure? For Proxyservlet, He can only get a sipsession--request.getsession (), using list may be more appropriate ...

public class Proxyservlet extends sipservlet{... ServletContext CTX = null; List<sipsession> SSL = new linkedlist<sipsession> (); ... public void init (ServletConfig config) throws servletexception {super.init (config); ctx = Config.getservletcontext () ; Ctx.setattribute ("StateList", SSL); ... } ... } public class B2buaservlet extends sipservlet{... ServletContext CTX = null; List<sipsession> SSL = new linkedlist<sipsession> (); ... public void init (ServletConfig config) throws servletexception {super.init (config); ctx = Config.getservletcontext () ; SSL = (list<sipsession) ctx.getattribute ("StateList"); ... } ... }

However, there is a problem, that is, when the search or deletion of a large number of code, affecting the readability of the code, and decided to wrap the list<sipsession> operation in a class statelist

public class StateList {list<sipsession> SSL = new linkedlist<sipsession> ();//Combine set and add without knowing the index value public Bool Add (sipsession ss) {int index = SSL.INDEXOF (ss), if (Index! =-1) {ssl.set (INDEX,SS); return true;} else {Ssl.add ( Ssl.size (), SS); return true; }}//Recursive call without knowing the index value public Bool remove (sipsession ss) {int index = SSL.INDEXOF (ss); if (Index! =-1) {ssl.remove (index); th Is.remove (ss); } else {return true;}} Iterates through the list, returning the first sipsession public sipsession get (String str) {iterator<sipsession> it = ssl.iterator () of the specified property str to 1; while (It.hasnext ()) {if (It.next (). getattribute (str) = = 1) {return it.next ();}} return null; } };

Think of a problem ... If you really use Sipsession to save the state, does the list or map really need it?... It doesn't seem to be necessary at all!

Sipsession life cycle in general, the sipsession life cycle can be controlled by the following methods: 1. If the parent sipapplicationsession expires or is explicitly invalidated, then the sub-sessions of all protocols are invalidated. 2. The application uses Sipsession's invalidate () API to make explicit invalidation. 3. The application tag sipsession is invalidated and the container then invalidates it when the session enters the Ready-to-invalidate state. Any attempt to fetch or store data on a failed sipsession causes the container to throw a illegalstateexception. When a sipsession is terminated, either because the parent application session timed out or because the session was explicitly invalidated, the window must clear all sessions of that State from memory. In this case, if you receive a subsequent request or response from this session of a dependency, the container will handle the message as follows: 1. Reject this request by sending a 481 error response 2. Routing this request or response is likely to have different life cycles for different application instances in the same application path. The container handles a subsequent request and response to a conversation ...

Hahaha!~ all written nonsense Ah! You only need to set a property value (HELD) when you receive 182, as a token, when you receive an ACK, Judge Ack.getsession (). Getattribut ("Flag") is equal to held. If yes, then proxy this ACK request, and

Sipapplicationsession SAS = Ack.getapplicationsession (TRUE);

Sas.setattribute ("Invite_send", "invite_send");

Sipservletrequest newreq = sf.createrequest (SAS, "INVITE", Ack.getto (), Ack.getfrom ());

Add judgment when you receive the OK

Resp.getmethod () = = "INVITE" && resp.getrequest (). isinitial! = True

If "Yes"

Copy the SDP information for this response

Resp.setcontenttype ("Text/plain");

String content = Resp.getcontent ();

And then

Sipapplicationsession SAS = Resp.getapplicationsession (TRUE);

Judge Sas.getattribute ("invite_send") = = "Invite_send";

If it is

Sipservletrequest newreq = sf.createrequest (SAS, "INVITE", Resp.getfrom (), Resp.getto ());

Newreq.setcontenttype ("Text/plain");

Newreq.setcontent (Content, "Text/plain");

Newreq.setcontenttype ("Sdp/application");

Newreq.setcontentlength (Content.length ());

Newreq.send ();

Sas.removeattribute ("Invite_send");

Resp.createack (). Send ();

Of course, these are the functions that are done in Proxyservlet, and tomorrow, to write the B2buaservlet methods that need to be done.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.