java - Implement cancelRequest -
i implementing cancelrequest ongoing call. try send request once session established. cancel function worked first time implemented says transaction exists. how fix it?
else if(currentresponse.equals("session progress")){ system.out.println("------------------- status code: "+ statuscode+ "--------------------------"); system.out.println(responseevent.getresponse()); try { request cancelrequest; cancelrequest = invitetid.createcancel(); clienttransaction canceltid = sipprovider.getnewclienttransaction(cancelrequest); canceltid.sendrequest(); system.out.println("cancel requst:" + cancelrequest); } catch (sipexception e) { // todo auto-generated catch block e.printstacktrace(); } }
response:
-----------statuscode:100---------------------------- -----------currentresponse:trying---------------------------- ------------------- status code: 100-------------------------- sip/2.0 100 trying via: sip/2.0/udp localip:52216;branch=z9hg4bk-353435-03dc537ef691bb00e1e5512b034ea431;received=localip;rport=52216 from: <sip:username@sipip>;tag=-2016245219 to: <sip:86940160@sipip> call-id: fab08b0be8c74b236c9eced419dec3ae@localip cseq: 5 invite server: asterisk pbx 10.5.1 allow: invite,ack,cancel,options,bye,refer,subscribe,notify,info,publish supported: replaces,timer contact: <sip:86940160@sipip:5060> content-length: 0 ------------------- phone ringing hangup -------------------------- request optionsreceived @ stackwith server transaction idnull -----------statuscode:183---------------------------- -----------currentresponse:session progress---------------------------- ------------------- status code: 183-------------------------- sip/2.0 183 session progress via: sip/2.0/udp localip:52216;branch=z9hg4bk-353435-03dc537ef691bb00e1e5512b034ea431;received=localip;rport=52216 from: <sip:username@sipip>;tag=-2016245219 to: <sip:86940160@sipip>;tag=as06d748b5 call-id: fab08b0be8c74b236c9eced419dec3ae@localip cseq: 5 invite server: asterisk pbx 10.5.1 allow: invite,ack,cancel,options,bye,refer,subscribe,notify,info,publish supported: replaces,timer contact: <sip:86940160@sipip:5060> content-type: application/sdp content-length: 254 v=0 o=root 16865513 16865513 in ip4 sipip s=asterisk pbx 10.5.1 c=in ip4 sipip t=0 0 m=audio 16188 rtp/avp 8 101 a=rtpmap:8 pcma/8000 a=rtpmap:101 telephone-event/8000 a=fmtp:101 0-16 a=silencesupp:off - - - - a=ptime:20 a=sendrecv cancel requst:cancel sip:86940160@sipip sip/2.0 call-id: fab08b0be8c74b236c9eced419dec3ae@localip to: <sip:86940160@sipip> cseq: 5 cancel from: <sip:username@sipip>;tag=-2016245219 via: sip/2.0/udp localip:52216;rport;branch=z9hg4bk-353435-32ce00c8a1c2efc96caa44a14bf3b910 max-forwards: 70 content-length: 0 request optionsreceived @ stackwith server transaction idnull -----------statuscode:401---------------------------- -----------currentresponse:unauthorized---------------------------- ---------------unauthorized--------sip/2.0 401 unauthorized via: sip/2.0/udp localip:52216;branch=z9hg4bk-353435-32ce00c8a1c2efc96caa44a14bf3b910;received=localip;rport=52216 from: <sip:username@sipip>;tag=-2016245219 to: <sip:86940160@sipip>;tag=as647be936 call-id: fab08b0be8c74b236c9eced419dec3ae@localip cseq: 5 invite server: asterisk pbx 10.5.1 allow: invite,ack,cancel,options,bye,refer,subscribe,notify,info,publish supported: replaces,timer www-authenticate: digest algorithm=md5,realm="xyz.com",nonce="69e1f4c2" content-length: 0 -----------statuscode:481---------------------------- -----------currentresponse:call/transaction not exist---------------------------- -------------------status code: 481 ---------------------- sip/2.0 481 call/transaction not exist via: sip/2.0/udp 10.99.136.131:52216;branch=z9hg4bk-353435-32ce00c8a1c2efc96caa44a14bf3b910;received=10.99.136.131;rport=52216 from: <sip:username@sipip>;tag=-2016245219 to: <sip:86940160@sipip>;tag=as647be936 call-id: fab08b0be8c74b236c9eced419dec3ae@10.99.136.131 cseq: 5 cancel server: asterisk pbx 10.5.1 allow: invite,ack,cancel,options,bye,refer,subscribe,notify,info,publish supported: replaces,timer content-length: 0 request optionsreceived @ stackwith server transaction idnull
processresponse
public void processresponse(responseevent responseevent) { // todo auto-generated method stub int statuscode = responseevent.getresponse().getstatuscode(); string currentresponse = responseevent.getresponse().getreasonphrase(); // system.out.println("-----------statuscode:"+ statuscode+"----------------------------"); // system.out.println("-----------currentresponse:"+ currentresponse+"----------------------------"); cseqheader cseqtemp = (cseqheader) responseevent.getresponse().getheader(cseqheader.name); // system.out.println(responseevent.getresponse().tostring()); system.out.println("................cseq" + cseqtemp); if(cseqtemp.getmethod().equals(request.register)){ processregisterresponse(responseevent); }else if(cseqtemp.getmethod().equals(request.invite)){ countinviteresponse ++; system.out.println(".....countinviteresponse: "+ countinviteresponse+" ......" ); if(countinviteresponse == 6){ system.out.println(responseevent.getresponse()); // functions.cancel(responseevent.getresponse()); } try { processinviteresponse(responseevent); } catch (sipexception | invalidargumentexception e) { // todo auto-generated catch block e.printstacktrace(); } } else { system.out.println("-------------------status code: "+ statuscode+" ----------------------"); system.out.println(responseevent.getresponse().tostring()); }
processinvite response:
private void processinviteresponse(responseevent responsereceivedevent) throws sipexception, invalidargumentexception { int statuscode = responsereceivedevent.getresponse().getstatuscode(); string currentresponse = responsereceivedevent.getresponse().getreasonphrase(); response response = responsereceivedevent.getresponse(); clienttransaction tid = responsereceivedevent.getclienttransaction(); cseqheader cseqtemp = (cseqheader) response.getheader(cseqheader.name); if(statuscode> 400 && statuscode <410){ if((statuscode ==401 && cseqtemp.getmethod().equals(request.invite)) || response.getstatuscode() == response.proxy_authentication_required){ system.out.println(responsereceivedevent.getresponse().tostring()); // if(functions.cseq<4){ system.out.println("------------------invite 401--------------------\n"); authenticationhelper authenticationhelper = ((sipstackext) functions.sipstack).getauthenticationhelper(new accountmanagerimpl(), functions.headerfactory); try { functions.invitetid = authenticationhelper.handlechallenge(response, functions.invitetid, functions.sipprovider, 5); functions.invitetid.sendrequest(); system.out.println("/n" + functions.invitetid.getrequest()); } catch (nullpointerexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (sipexception e) { // todo auto-generated catch block e.printstacktrace(); } // functions.call(responsereceivedevent.getresponse()); // } } }else if(statuscode == 200){ try { system.out.println(responsereceivedevent.getresponse().tostring()); response = (response) responsereceivedevent.getresponse(); tid = responsereceivedevent.getclienttransaction(); cseqheader cseq = (cseqheader) response.getheader(cseqheader.name); dialog dialog = responsereceivedevent.getdialog(); if (tid == null) { // rfc3261: must respond every 2xx if (ackrequest!=null && dialog!=null) { system.out.println("re-sending ack"); dialog.sendack(ackrequest); } return; } if (response.getstatuscode() == response.ok) { system.out.println("dialog after 200 ok " + dialog); system.out.println("dialog state after 200 ok " + dialog.getstate()); ackrequest = dialog.createack(cseq.getseqnumber() ); system.out.println("sending ack"); dialog.sendack(ackrequest); } } catch (invalidargumentexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (sipexception e) { // todo auto-generated catch block e.printstacktrace(); } }else if(responsereceivedevent.getresponse().getreasonphrase().equals("trying")){ system.out.println("------------------- status code: "+ statuscode+ "--------------------------"); system.out.println(responsereceivedevent.getresponse()); }else if(responsereceivedevent.getresponse().getreasonphrase().equals("ringing")){ system.out.println("------------------- status code: "+ statuscode+ "--------------------------"); system.out.println(responsereceivedevent.getresponse()); system.out.println("------------------- phone ringing --------------------------"); }else if(responsereceivedevent.getresponse().getreasonphrase().equals("session progress")){ system.out.println("------------------- status code: "+ statuscode+ "--------------------------"); system.out.println(responsereceivedevent.getresponse()); // functions.cancel(response); request cancelrequest= responsereceivedevent.getclienttransaction().createcancel(); functions.sipprovider.sendrequest(cancelrequest); system.out.println(cancelrequest.tostring()); }else { system.out.println("------------------- status code: "+ statuscode+ "--------------------------"); system.out.println(responsereceivedevent.getresponse()); } }
invite:
public void call(response response) { try { request request; cseq++; string callee = "86940160"; // string callee = "160"; current_process = cseq + "invite"; arraylist viaheaders = new arraylist(); viaheader viaheader = headerfactory.createviaheader(localip, rport, "udp", null); viaheader.setrport(); viaheaders.add(viaheader); // "max-forwards" header. maxforwardsheader maxforwardsheader = headerfactory.createmaxforwardsheader(70); // "call-id" header. callidheader callidheader; if(!retry){ callidheader = this.sipprovider.getnewcallid(); callidheaderinit = callidheader; }else{ callidheader = callidheaderinit; } // "cseq" header. cseqheader cseqheader = headerfactory.createcseqheader(cseq, request.invite); address fromaddress = addressfactory.createaddress("sip:" + username + '@' + server); address toaddress = addressfactory.createaddress("sip:"+callee+'@'+sipip); fromheader fromheader = headerfactory.createfromheader( fromaddress, string.valueof(this.tag)); // "to" header. toheader toheader = headerfactory.createtoheader(toaddress, null); // create contact address used sip messages. contactaddress = addressfactory.createaddress("sip:" + username + "@"+ localip +":"+rport+ ";"+ "transport=udp"); // create contact header used sip messages. contactheader = headerfactory.createcontactheader(contactaddress); contentlengthheader contentlength = headerfactory.createcontentlengthheader(211); contenttypeheader contenttype = headerfactory.createcontenttypeheader("application", "sdp"); string sdpdata = "v=0\n" + "o=user1 795808818 480847547 in ip4 "+localip+"\n" + "s=-\n" + "c=in ip4 "+localip+"\n" + "t=0 0\n" + "m=audio 8000 rtp/avp 0 8 101\n" + "a=rtpmap:0 pcmu/8000\n" + "a=rtpmap:8 pcma/8000\n" + "a=rtpmap:101 telephone-event/8000\n" + "a=sendrecv"; byte[] contents = sdpdata.getbytes(); // this.contactheader = this.headerfactory // .createcontactheader(contactaddress); uri requesturi = addressfactory.createuri("sip:" +callee+ '@'+ server); request = messagefactory.createrequest(requesturi, request.invite, callidheader,cseqheader, fromheader, toheader, viaheaders, maxforwardsheader); request.setcontent(contents, contenttype); request.addheader(contactheader); request.addheader(contentlength); if (response != null) { authorizationheader authheader = null; if(retry){ authheader = getauthorizationheader(); retry = false; }else{ authheader = makeauthheader(headerfactory, response, request, username, password); retry = true; } request.addheader(authheader); // authenticationhelper authenticationhelper = // ((sipstackext) sipstack).getauthenticationhelper(new accountmanagerimpl(), headerfactory); // try { // invitetid = authenticationhelper.handlechallenge(response, tid, sipprovider, 5); // invitetid.sendrequest(); // } catch (nullpointerexception e) { // // todo auto-generated catch block // e.printstacktrace(); // } catch (sipexception e) { // // todo auto-generated catch block // e.printstacktrace(); // } } // expiresheader expiresheader = headerfactory.createexpiresheader(1); // // request.addheader(expiresheader); // test listener =this; if (response != null) { boolean retry = true; // system.out.println("debug: response: "+response); } invitetid = sipprovider.getnewclienttransaction(request); if(dialog!= null && logger.isdebugenabled()){ logger.debug("obtain dialog clienttransaction: automatic dialog support on"); // system.out.println("obtain dialog clienttransaction: automatic dialog support on"); } if(dialog == null){ //automatic dialog support turned off dialog = sipprovider.getnewdialog(invitetid); } system.out.println("dialog: created" + dialog); // send request out. invitetid.sendrequest(); dialog = invitetid.getdialog(); // system.out.println("dialog:" + dialog); // send request statelessly through sip provider. // this.sipprovider.sendrequest(request); system.out.println(request.tostring()); // display message in text area. logger.debug("request sent:\n" + "\n\n"); } catch (exception e) { // if error occurred, display error. e.printstacktrace(); logger.debug("request sent failed: " + e.getmessage() + "\n"); } }
response:
register sip:sipip sip/2.0 call-id: cdf4ef0fa1a145b1b07b632797f53fdb@localip cseq: 1 register from: <sip:username@sipip>;tag=-1436906198 to: <sip:username@sipip> via: sip/2.0/udp localip:52216;rport;branch=z9hg4bk-3230-3d42d8c86f8aa3d6d0194a4846511464 max-forwards: 70 contact: <sip:username@localip:52216;transport=udp> content-length: 0 log4j:warn no appenders found logger (test). log4j:warn please initialize log4j system properly. log4j:warn see http://logging.apache.org/log4j/1.2/faq.html#noconfig more info. ................cseqcseq: 1 register sip/2.0 401 unauthorized via: sip/2.0/udp localip:52216;branch=z9hg4bk-3230-3d42d8c86f8aa3d6d0194a4846511464;received=localip;rport=52216 from: <sip:username@sipip>;tag=-1436906198 to: <sip:username@sipip>;tag=as24ab6bcf call-id: cdf4ef0fa1a145b1b07b632797f53fdb@localip cseq: 1 register server: asterisk pbx 10.5.1 allow: invite,ack,cancel,options,bye,refer,subscribe,notify,info,publish supported: replaces,timer www-authenticate: digest algorithm=md5,realm="xyz.com",nonce="6ac7ff34" content-length: 0 register sip:sipip sip/2.0 call-id: cdf4ef0fa1a145b1b07b632797f53fdb@localip cseq: 2 register from: <sip:username@sipip>;tag=-1436906198 to: <sip:username@sipip> via: sip/2.0/udp localip:52216;rport;branch=z9hg4bk-3230-a94c094a3aacfc40d0455de777b0ae26 max-forwards: 70 contact: <sip:username@localip:52216;transport=udp> authorization: digest username="username",realm="xyz.com",nonce="6ac7ff34",uri="sip:sipip",algorithm=md5,response="53231e4a2cd26604d8d2bc867b0e43e9" content-length: 0 ................cseqcseq: 2 register ---------------------------registered: 200-------------------- sip/2.0 200 ok via: sip/2.0/udp localip:52216;branch=z9hg4bk-3230-a94c094a3aacfc40d0455de777b0ae26;received=localip;rport=52216 from: <sip:username@sipip>;tag=-1436906198 to: <sip:username@sipip>;tag=as24ab6bcf call-id: cdf4ef0fa1a145b1b07b632797f53fdb@localip cseq: 2 register server: asterisk pbx 10.5.1 allow: invite,ack,cancel,options,bye,refer,subscribe,notify,info,publish supported: replaces,timer expires: 240 contact: <sip:username@localip:52216;transport=udp>;expires=240 date: thu, 30 apr 2015 14:46:46 gmt content-length: 0 dialog: createdgov.nist.javax.sip.stack.sipdialog@79f65165 invite sip:86940160@sipip sip/2.0 call-id: cdf4ef0fa1a145b1b07b632797f53fdb@localip cseq: 3 invite from: <sip:username@sipip>;tag=-1436906198 to: <sip:86940160@sipip> via: sip/2.0/udp localip:52216;rport;branch=z9hg4bk-3230-c5512f65af9890c0798f1e5757ef1c57 max-forwards: 70 content-type: application/sdp contact: <sip:username@localip:52216;transport=udp> authorization: digest username="username",realm="xyz.com",nonce="6ac7ff34",uri="sip:sipip",algorithm=md5,response="53231e4a2cd26604d8d2bc867b0e43e9" content-length: 211 v=0 o=user1 795808818 480847547 in ip4 localip s=- c=in ip4 localip t=0 0 m=audio 8000 rtp/avp 0 8 101 a=rtpmap:0 pcmu/8000 a=rtpmap:8 pcma/8000 a=rtpmap:101 telephone-event/8000 a=sendrecv request optionsreceived @ stackwith server transaction idnull request optionsreceived @ stackwith server transaction idnull ................cseqcseq: 3 invite .....countinviteresponse: 1 ...... sip/2.0 401 unauthorized via: sip/2.0/udp localip:52216;branch=z9hg4bk-3230-c5512f65af9890c0798f1e5757ef1c57;received=localip;rport=52216 from: <sip:username@sipip>;tag=-1436906198 to: <sip:86940160@sipip>;tag=as43ee81b7 call-id: cdf4ef0fa1a145b1b07b632797f53fdb@localip cseq: 3 invite server: asterisk pbx 10.5.1 allow: invite,ack,cancel,options,bye,refer,subscribe,notify,info,publish supported: replaces,timer www-authenticate: digest algorithm=md5,realm="xyz.com",nonce="6ad3bcf0" content-length: 0
response after removing tag:
------------------invite 401-------------------- /ninvite sip:86940160@sipip:5060;maddr=sipip sip/2.0 call-id: 7128d7fc7109b0c1293dce9f6b2b9c7b@localip.131 cseq: 4 invite from: <sip:username@sipip>;tag=-515507368 to: <sip:86940160@sipip> via: sip/2.0/udp localip.131:52216;rport;branch=z9hg4bk-313339-453b043ba57ac51c5293cf17f8823071 max-forwards: 70 content-type: application/sdp contact: <sip:username@localip.131:52216;transport=udp> authorization: digest username="username",realm="xyz.com",nonce="3cdd5016",uri="sip:86940160@sipip:5060;maddr=sipip",response="38b50cad69f74ba89fd63f78a1968acc",algorithm=md5 content-length: 211 v=0 o=user1 795808818 480847547 in ip4 localip.131 s=- c=in ip4 localip.131 t=0 0 m=audio 8000 rtp/avp 0 8 101 a=rtpmap:0 pcmu/8000 a=rtpmap:8 pcma/8000 a=rtpmap:101 telephone-event/8000 a=sendrecv request optionsreceived @ stackwith server transaction idnull ................cseqcseq: 4 invite .....countinviteresponse: 2 ...... sip/2.0 401 unauthorized via: sip/2.0/udp localip.131:52216;branch=z9hg4bk-313339-453b043ba57ac51c5293cf17f8823071;received=localip.131;rport=52216 from: <sip:username@sipip>;tag=-515507368 to: <sip:86940160@sipip>;tag=as5f585fd4 call-id: 7128d7fc7109b0c1293dce9f6b2b9c7b@localip.131 cseq: 4 invite server: asterisk pbx 10.5.1 allow: invite,ack,cancel,options,bye,refer,subscribe,notify,info,publish supported: replaces,timer www-authenticate: digest algorithm=md5,realm="xyz.com",nonce="6aa75c9f" content-length: 0 ------------------invite 401-------------------- /ninvite sip:86940160@sipip:5060;maddr=sipip sip/2.0 call-id: 7128d7fc7109b0c1293dce9f6b2b9c7b@localip.131 cseq: 5 invite from: <sip:username@sipip>;tag=-515507368 to: <sip:86940160@sipip> via: sip/2.0/udp localip.131:52216;rport;branch=z9hg4bk-313339-5ed3ab3afc3b791c0af19470ae58813d max-forwards: 70 content-type: application/sdp contact: <sip:username@localip.131:52216;transport=udp> authorization: digest username="username",realm="xyz.com",nonce="6aa75c9f",uri="sip:86940160@sipip:5060;maddr=sipip",response="092fc083a66f19b5b995c8cbc8d5a8bc",algorithm=md5 content-length: 211 v=0 o=user1 795808818 480847547 in ip4 localip.131 s=- c=in ip4 localip.131 t=0 0 m=audio 8000 rtp/avp 0 8 101 a=rtpmap:0 pcmu/8000 a=rtpmap:8 pcma/8000 a=rtpmap:101 telephone-event/8000 a=sendrecv ................cseqcseq: 5 invite .....countinviteresponse: 3 ...... ------------------- status code: 100-------------------------- sip/2.0 100 trying via: sip/2.0/udp localip.131:52216;branch=z9hg4bk-313339-5ed3ab3afc3b791c0af19470ae58813d;received=localip.131;rport=52216 from: <sip:username@sipip>;tag=-515507368 to: <sip:86940160@sipip> call-id: 7128d7fc7109b0c1293dce9f6b2b9c7b@localip.131 cseq: 5 invite server: asterisk pbx 10.5.1 allow: invite,ack,cancel,options,bye,refer,subscribe,notify,info,publish supported: replaces,timer contact: <sip:86940160@sipip:5060> content-length: 0 ................cseqcseq: 5 invite .....countinviteresponse: 4 ...... ------------------- status code: 183-------------------------- sip/2.0 183 session progress via: sip/2.0/udp localip.131:52216;branch=z9hg4bk-313339-5ed3ab3afc3b791c0af19470ae58813d;received=localip.131;rport=52216 from: <sip:username@sipip>;tag=-515507368 to: <sip:86940160@sipip>;tag=as7fc2a07d call-id: 7128d7fc7109b0c1293dce9f6b2b9c7b@localip.131 cseq: 5 invite server: asterisk pbx 10.5.1 allow: invite,ack,cancel,options,bye,refer,subscribe,notify,info,publish supported: replaces,timer contact: <sip:86940160@sipip:5060> content-type: application/sdp content-length: 256 v=0 o=root 163700690 163700690 in ip4 sipip s=asterisk pbx 10.5.1 c=in ip4 sipip t=0 0 m=audio 25036 rtp/avp 8 101 a=rtpmap:8 pcma/8000 a=rtpmap:101 telephone-event/8000 a=fmtp:101 0-16 a=silencesupp:off - - - - a=ptime:20 a=sendrecv invite id:z9hg4bk-313339-5ed3ab3afc3b791c0af19470ae58813d cancel sip:86940160@sipip:5060;maddr=sipip sip/2.0 call-id: 7128d7fc7109b0c1293dce9f6b2b9c7b@localip.131 to: <sip:86940160@sipip> cseq: 5 cancel from: <sip:username@sipip>;tag=-515507368 via: sip/2.0/udp localip.131:52216;rport;branch=z9hg4bk-313339-5ed3ab3afc3b791c0af19470ae58813d max-forwards: 70 content-length: 0 ................cseqcseq: 5 cancel -------------------status code: 481 ---------------------- sip/2.0 481 call/transaction not exist via: sip/2.0/udp localip.131:52216;branch=z9hg4bk-313339-5ed3ab3afc3b791c0af19470ae58813d;received=localip.131;rport=52216 from: <sip:username@sipip>;tag=-515507368 to: <sip:86940160@sipip>;tag=as23aeabf5 call-id: 7128d7fc7109b0c1293dce9f6b2b9c7b@localip.131 cseq: 5 cancel server: asterisk pbx 10.5.1 allow: invite,ack,cancel,options,bye,refer,subscribe,notify,info,publish supported: replaces,timer content-length: 0
the invite transaction id different cancel transaction id. invite
z9hg4bk-353435-03dc537ef691bb00e1e5512b034ea431
and cancel
z9hg4bk-353435-32ce00c8a1c2efc96caa44a14bf3b910
they should same.
the right way cancel use original invite transaction , use provider
cancelrequest = inviteclienttransaction.createcancel(); sipprovider.sendrequest(cancelrequest);
Comments
Post a Comment